モジュール:SectionSelect

半永久的に拡張半保護されているモジュール
モジュールの解説[作成]
local p = {}

--[[インクルードしたページから指定した節のみを取り出す

    {{#invoke:SectionSelect|select|page=「何の日」等の一覧記事|section=節名|title=節題を表示する|back=空の場合は節を遡る}}
    * page= 取り込む一覧記事を{{}}で指定して内容全文を入力する
    * section= 抜き出す節のタイトル
    * title= 0:節のタイトルを表示しない、1:表示する、3:表示したタイトルを内部リンクにする
    * back= 0:内容をそのまま表示する、1:内容が空の場合は節を上に遡る

    ex: Portal:災害/今日は何の日
        {{#invoke:SectionSelect|select|page={{Portal:災害/今日は何の日/{{#time:n月|+9 hours}}}}|section={{#time:n月j日|+9 hours}}|title=3|back=0}}
    ex: Template:季節の画像
        {{#invoke:SectionSelect|select|page={{Wikipedia:季節の画像 {{#time:n月|+9 hours}}}}|section={{#time:n月j日|+9 hours}}|title=0|back=1}}

    技術的制約・未修正の不具合
        * 各月1日(最初の節)には必ずデータを用意してください。
        * 節の行の後ろの「==」と改行の間に空白等があると誤動作します。
        * 節名と同じワードが本文中に含まれていると、うまく取り出せない可能性があります。
    ]]
 
function p.select(frame)
    local str = frame.args['page']
    local spl = frame.args['section']
    local back = p.split(str, spl)[1]
    local text = p.split(p.split(str, spl)[2], "\n==")[1]
 
    text = p.split(text, "==")[2]
    if text == "" then
        if frame.args['back'] == "1" then
            local tbl = p.split(back, "\n==")
            for i = #tbl - 1,1,-1 do
                if #p.split(tbl[i], "==\n") == 2 then
                    text = p.split(tbl[i], "==\n")[2]
                    break
                end
            end
        end
    end
 
    if frame.args['title'] == "1" then text = "== " .. frame.args['section'] .. " ==\n" .. text end
    if frame.args['title'] == "3" then text = "== [[" .. frame.args['section'] .. "]] ==\n" .. text end
    return text
end
 
function p.split(str, spl)
    m, nrep = str:gsub("%s*"..spl.."%s*", "")
    return { str:match((("%s*(.-)%s*"..spl.."%s*"):rep(nrep).."(.*)")) }
end
 
return p