コンテンツにスキップ

「モジュール:SignalinstWiki」の版間の差分

提供:信号機Wiki
結合文字修正
編集の要約なし
 
(同じ利用者による、間の4版が非表示)
27行目: 27行目:
if propertySplit == nil or propertySplit == '' then propertySplit = "、" end
if propertySplit == nil or propertySplit == '' then propertySplit = "、" end
-- Valueを分割
    -- 元の propertyValue に「など」が含まれているかチェック
-- 分割された各要素を一時的に保存するテーブル
    local hasNado = false
    if string.find(propertyValue, "など") then
        hasNado = true
    end
 
    -- Valueを分割
local splitValues = {}
local splitValues = {}
 
local pattern = "(.-)" .. propertySplit
local pattern = "(.-)" .. propertySplit -- gmatchのパターン。'%' はエスケープ不要でリテラルの '%' を指す。
-- propertyValue の末尾に propertySplit を追加することで、最後の要素が正しく取得されるようにする
-- propertyValue の末尾に propertySplit を追加することで、最後の要素が正しく取得されるようにする
38行目: 42行目:
         -- 各要素の前後の空白をトリム(取り除く)
         -- 各要素の前後の空白をトリム(取り除く)
         local trimmedPart = part:match("^%s*(.-)%s*$")
         local trimmedPart = part:match("^%s*(.-)%s*$")
       
        -- ここで「など」を空文字列に置き換える処理
        -- 各部分から「など」を削除してからリストに追加する
        trimmedPart = string.gsub(trimmedPart, "など", "")
       
         if trimmedPart ~= '' then -- 空文字列でない場合のみ追加
         if trimmedPart ~= '' then -- 空文字列でない場合のみ追加
             table.insert(splitValues, trimmedPart)
             table.insert(splitValues, trimmedPart)
49行目: 58行目:
     end
     end
      
      
     -- 各プロパティを改行で連結して返す
     -- ここで最終的な文字列を構築
     return table.concat(smwProperties, propertiesSplit)
     local finalOutput = table.concat(smwProperties, propertySplit)
   
    -- もし元の propertyValue に「など」が含まれていた場合、最終出力の末尾に追加
    if hasNado then
        -- 結合された文字列が空でない場合のみ、区切り文字を付けて「など」を追加
        if finalOutput ~= '' then
            finalOutput = finalOutput .. "など"
        else
            -- 全くプロパティが生成されなかった場合でも「など」だけ返す
            finalOutput = "など"
        end
    end
 
    return finalOutput
end
end


return p
return p

2025年12月12日 (金) 15:04時点における最新版

このモジュールについての説明文ページを モジュール:SignalinstWiki/doc に作成できます

-- 万能系のLuaメソッドをモジュールとして定義する
local p = {} -- モジュール用パッケージを用意

--[[
	テスト用関数
]]
function p.test(frame)
	return "Hello World!"
end

--[[
	指定した区切り文字で区切られたリストを認識し、その区切り文字で分解。
	区切った後にその中身をSemantic Mediawikiのプロパティとして使用可能な
	文字列にして返します。
	
	例:「メーカー」というプロパティに「小糸工業、京三製作所、日本信号」と登録する場合
	{{#invoke:SignalinstWiki|getProperties|メーカー|小糸工業、京三製作所、日本信号|、}}
]]
function p.getProperties(frame)
	local propertyName = frame.args[1]
	local propertyValue = frame.args[2]
	local propertySplit = frame.args[3]
	
	-- デフォルト値を指定
	if propertyName == nil or propertyName == '' then return nil end
	if propertyValue == nil or propertyValue == '' then return nil end
	if propertySplit == nil or propertySplit == '' then propertySplit = "、" end
	
    -- 元の propertyValue に「など」が含まれているかチェック
    local hasNado = false
    if string.find(propertyValue, "など") then
        hasNado = true
    end

    -- Valueを分割
	local splitValues = {}
	
	local pattern = "(.-)" .. propertySplit
	
	-- propertyValue の末尾に propertySplit を追加することで、最後の要素が正しく取得されるようにする
    for part in string.gmatch(propertyValue .. propertySplit, pattern) do
        -- 各要素の前後の空白をトリム(取り除く)
        local trimmedPart = part:match("^%s*(.-)%s*$")
        
        -- ここで「など」を空文字列に置き換える処理
        -- 各部分から「など」を削除してからリストに追加する
        trimmedPart = string.gsub(trimmedPart, "など", "")
        
        if trimmedPart ~= '' then -- 空文字列でない場合のみ追加
            table.insert(splitValues, trimmedPart)
        end
    end
    
    -- Semantic Mediawiki のプロパティ形式に変換して連結する
    local smwProperties = {}
    for _, value in ipairs(splitValues) do
        table.insert(smwProperties, "[[" .. propertyName .. "::" .. value .. "]]")
    end
    
    -- ここで最終的な文字列を構築
    local finalOutput = table.concat(smwProperties, propertySplit)
    
    -- もし元の propertyValue に「など」が含まれていた場合、最終出力の末尾に追加
    if hasNado then
        -- 結合された文字列が空でない場合のみ、区切り文字を付けて「など」を追加
        if finalOutput ~= '' then
            finalOutput = finalOutput .. "など"
        else
            -- 全くプロパティが生成されなかった場合でも「など」だけ返す
            finalOutput = "など"
        end
    end

    return finalOutput
end

return p