模組:Vertical

此序允用縱書文字。如VtextVH1VH2同效。

Vertical之用 Vtext之用
{{#invoke:Vertical|vertical|

== 常娥 ==
=== 作者李商隱 ===

雲母屏風燭影深
長河漸落曉星沈
常娥應悔偷靈藥
碧海青天夜夜心}}

{{VH1|常娥 }}

{{VH2|作者李商隱 }}
{{Vtext|雲母屏風燭影深}}
{{Vtext|長河漸落曉星沈}}
{{Vtext|常娥應悔偷靈藥}}
{{Vtext|碧海青天夜夜心}}
{{clr}}

常娥
作者李商隱
雲母屏風燭影深
長河漸落曉星沈
常娥應悔偷靈藥
碧海青天夜夜心

local function explode(s, sep)
    local sep, fields = sep or "\n", {}
    local pattern = string.format("([^%s]+)", sep)
    s:gsub(pattern, function(c) fields[#fields+1] = c end)
    
    return fields
end

local function convert_line(s)
    -- {{VH2|...}}
    header = s:match("===%s*(.+)%s*===s*")
    if header ~= nil then
        return '<div style="font-size:x-large;font-weight:bold;line-height:1em;width:1.5em; float:right; cursor: vertical-text;">' .. header .. '</div>'
    end
    
    -- {{VH1|...}}
    header = s:match("==%s*(.+)%s*==s*")
    if header ~= nil then
        return '<div style="font-size:xx-large;font-weight:bold;line-height:1em;width:1.5em; float:right; cursor: vertical-text;">' .. header .. ' </div>'
    end
    
    -- 有人欲用單乎?
    list_item = s:match("\*%s+(.+)%s*")
    if list_item ~= nil then
        return '<div style="width:1.5em; float:right; cursor: vertical-text;">• ' .. list_item .. ' </div>'
    end
    
    -- {{Vtext|...}}
    return '<div style="width:1.5em; float:right; cursor: vertical-text;">' .. s .. ' </div>'
end

local function trimstr(s)
  return s:gsub("^%s*(.-)%s*$", "%1")
end

local function vertical_to_templates(s, add_clr)
    local r = {}
    local strings = explode(s, "\n")
    
    local skip_first = True
    
    for k, v in pairs(strings) do
        if skip_first then
            skip_first = trimstr(v) == ''
        end
        
        if not skip_first then
            r[#r + 1] = convert_line(v)
        end
    end

    if add_clr then
        -- {{clr}}
        r[#r + 1] = '<br clear="all" />'
    end

    return table.concat(r, "\n")
end


return {
    vertical = function (frame)
        return vertical_to_templates(frame.args[1], False)
    end,
    
    vertical_without_clr = function (frame)
        return vertical_to_templates(frame.args[1], True)
    end
}