Jump to content

Module:Kanbun

From Wikibooks, open books for an open world
local p = {}

local kunten = {
    ["レ"] = "㆑", -- 返り点には U+3191からU+319Fを使う。
    ["一"] = "㆒",
    ["二"] = "㆓",
    ["三"] = "㆔",
    ["四"] = "㆕",
    ["上"] = "㆖",
    ["中"] = "㆗",
    ["下"] = "㆘",
    ["甲"] = "㆙",
    ["乙"] = "㆚",
    ["丙"] = "㆛",
    ["丁"] = "㆜",
    ["天"] = "㆝",
    ["地"] = "㆞",
    ["人"] = "㆟",
    ["一レ"] = "㆒㆑",
    ["上レ"] = "㆖㆑",
    ["甲レ"] = "㆙㆑",
    ["天レ"] = "㆝㆑"
}

function p.kanbun(frame)
    local input_text = frame.args[1]
    local html_output =
        '<div style="writing-mode: vertical-rl; font-size: 150%; white-space: nowrap; font-family: serif">'
    local shift = 0
    local style = ""

    for char in string.gmatch(input_text, "%S+") do
        local kanzi = char:match("^(.-)[\\%[%{]") or char
        kanzi = kanzi:gsub("。", "")
        kanzi = kanzi:gsub("、", "")
        kanzi = kanzi:gsub("「", "")
        kanzi = kanzi:gsub("」", "")
        kanzi = kanzi:gsub("『", "")
        kanzi = kanzi:gsub("』", "")
        local okurigana = char:match("{(.-)}")
        local kaeriten = char:match("%[(.-)%]")
        local saidoku = char:match("%((.-)%)")
        local symbol = char:match("\\(begin)") or char:match("\\(end)") or char:match("\\(.)")

        if symbol == "O" then
            kanzi = " "
        end

        if symbol == "\\" then
            html_output = html_output .. "<br>"
            shift = 0
        elseif symbol == "begin" then
            style = style .. char:match("\\begin{(.-)}")
        elseif symbol == "end" then
            style = style:gsub(char:match("\\end{(.-)}"), "")
        else
            -- 漢字

            html_output = html_output .. string.format(
                '<span style="display: inline-block; position: relative; margin-left: 0.5em; margin-right: 0.5em; margin-top: 0.4em; margin-bottom: 0.5em; bottom: -%sem; %s">%s',
                shift, style, kanzi)

            if char:find("。」") then
                html_output = html_output ..
                                  [[<span style="position: absolute; bottom: -1em;">。</span><span style="position: absolute; bottom: -1.25em;">」</span>]]
            end
            if char:find("。』") then
                html_output = html_output ..
                                  [[<span style="position: absolute; bottom: -1em;">。</span><span style="position: absolute; bottom: -1.25em;">』</span>]]
            end
            if char:find("。") then
                html_output = html_output .. [[<span style="position: absolute; bottom: -1em;">。</span>]]
            end

            if char:find("、") then
                html_output = html_output .. [[<span style="position: absolute; bottom: -1em;">、</span>]]
            end

            if char:find("「") then
                html_output = html_output .. [[<span style="position: absolute; top: -1em;">「</span>]]
            end

            if char:find("」") then
                html_output = html_output .. [[<span style="position: absolute; bottom: -1em;">」</span>]]
            end

            if char:find("『") then
                html_output = html_output .. [[<span style="position: absolute; top: -1em;">『</span>]]
            end

            if char:find("』") then
                html_output = html_output .. [[<span style="position: absolute; bottom: -1em;">』</span>]]
            end

            -- 返り点
            if kaeriten then
                kaeriten = kunten[kaeriten] or kaeriten

                if symbol == "H" then
                    html_output = html_output ..
                                      string.format(
                            '<span style="position: absolute; font-size: 50%%; top: 1.8em; left: -0.25em;">%s</span>',
                            kaeriten)

                else
                    if kaeriten == "㆒㆑" then
                        html_output = html_output ..
                                          [[<span style="position: absolute; font-size: 50%; bottom: -1em; left: 0.25em;">㆒</span><span style="position: absolute; font-size: 50%; bottom: -1.35em; left: 0.25em;">㆑</span>]]

                    elseif kaeriten == "㆖㆑" then
                        html_output = html_output ..
                                          [[<span style="position: absolute; font-size: 50%; bottom: -1em; left: 0.25em; transform: scaleY(0.6);">㆖</span><span style="position: absolute; font-size: 50%; bottom: -1.45em; left: 0.25em; transform: scaleY(0.6);">㆑</span>]]

                    elseif kaeriten == "㆙㆑" then
                        html_output = html_output ..
                                          [[<span style="position: absolute; font-size: 50%; bottom: -1em; left: 0.25em; transform: scaleY(0.6);">㆙</span><span style="position: absolute; font-size: 50%; bottom: -1.4em; left: 0.25em; transform: scaleY(0.6);">㆑</span>]]

                    elseif kaeriten == "㆝㆑" then
                        html_output = html_output ..
                                          [[<span style="position: absolute; font-size: 50%; bottom: -1em; left: 0.25em; transform: scaleY(0.6);">㆝</span><span style="position: absolute; font-size: 50%; bottom: -1.4em; left: 0.25em; transform: scaleY(0.6);">㆑</span>]]

                    else
                        html_output = html_output ..
                                          string.format(
                                '<span style="position: absolute; font-size: 50%%; bottom: -1em; left: 0.25em;">%s</span>',
                                kaeriten)
                    end

                end

            end

            -- 送り仮名
            if okurigana then

                -- ひらがなとカタカナの個数を計算
                local hiraganumber = 0
                local katakanumber = 0

                local i = 1
                while i <= #okurigana do
                    local byte = string.byte(okurigana, i)
                    local second = string.byte(okurigana, i + 1)
                    local third = string.byte(okurigana, i + 2)

                    local codepoint = ((byte % 16) * 2 ^ 12) + ((second % 64) * 2 ^ 6) + (third % 64)

                    if codepoint >= 0x3041 and codepoint <= 0x309F then
                        hiraganumber = hiraganumber + 1
                    elseif codepoint >= 0x30A0 and codepoint <= 0x30FF then
                        katakanumber = katakanumber + 1
                    end

                    i = i + 3
                end

                -- 位置の調整
                local bottom = nil
                local top = nil

                if hiraganumber == 0 and katakanumber == 5 then
                    bottom = "-1"
                    local first_line = okurigana:sub(1, 9)
                    local second_line = okurigana:sub(10)

                    html_output = html_output .. string.format(
                        '<span style="position: absolute; font-size: 50%%; bottom: %sem; right: -1.5em; white-space: nowrap;">%s</span>',
                        bottom, first_line)

                    html_output = html_output .. string.format(
                        '<span style="position: absolute; font-size: 50%%; bottom: %sem; right: -0.5em; white-space: nowrap;">%s</span>',
                        bottom, second_line)

                else

                    if hiraganumber == 0 and katakanumber == 1 then
                        bottom = "-0.5"

                    elseif hiraganumber == 0 and katakanumber == 2 then
                        bottom = "-1"

                    elseif hiraganumber == 0 and katakanumber == 3 then
                        bottom = "-2"

                    elseif hiraganumber == 0 and katakanumber == 4 then
                        bottom = "-2.5"

                    elseif hiraganumber == 1 and katakanumber == 0 then
                        bottom = "0.5"

                    elseif hiraganumber == 2 and katakanumber == 0 then
                        bottom = "0"

                    elseif hiraganumber == 3 and katakanumber == 0 then
                        bottom = "-0.5"

                    elseif hiraganumber == 4 and katakanumber == 0 then
                        bottom = "-1"

                    elseif hiraganumber == 1 and katakanumber == 1 then
                        bottom = "-0.5"

                    elseif hiraganumber + katakanumber >= 5 then
                        top = "0"
                        shift = shift + 0.5 * (hiraganumber + katakanumber - 4)

                    else
                        bottom = "-1"
                    end

                    if bottom then
                        html_output = html_output .. string.format(
                            '<span style="position: absolute; font-size: 50%%; bottom: %sem; right: -0.5em; white-space: nowrap;">%s</span>',
                            bottom, okurigana)
                    else
                        html_output = html_output .. string.format(
                            '<span style="position: absolute; font-size: 50%%; top: %sem; right: -0.5em; white-space: nowrap;">%s</span>',
                            top, okurigana)
                    end
                end
            end

            if saidoku then
                html_output = html_output .. string.format(
                    '<span style="position: absolute; font-size: 50%%; bottom: 0em; left: -0.5em; white-space: nowrap;">%s</span>',
                    saidoku)
            end

            html_output = html_output .. "</span>"
        end

    end

    html_output = html_output .. "</div>"

    return html_output
end

return p