Module:Mscorewithpipes

From Wikisource
Jump to navigation Jump to search
Module documentation[create]
-- This is intended to be a clone of User:Ankry's Mscore module without the restrictions on page formatting imposed by that module.
--
-- The first example of its use is at [[Asleep in the Deep (1898)]].

local p = {}

function p.renderpagelist ( frame )
    local pagelist = frame.args
    local score_text = ''
    local structure_text = {}
    local layout_text = {}
    local lyric_text = {}
    local other_context_text = {}
    local context_types = {}
    for index, page in pairs( frame.args ) do
        local page_title = mw.title.makeTitle( 'Page', page )
        local page_text = page_title:getContent()
        local current_score = mw.ustring.match( page_text, "<score" .. ".*" .. "</score>" ) or ""
        local flags = {}
        for line in mw.ustring.gmatch(current_score, "(.-)\n") do
            if index == 1 and mw.ustring.match(line, "begin structure") then
                flags["structure"] = 1
            elseif index == 1 and mw.ustring.match(line, "end structure") then
                flags["structure"] = nil
            elseif index == 1 and mw.ustring.match(line, "begin layout") then
                flags["layout"] = 1
            elseif index == 1 and mw.ustring.match(line, "end layout") then
                flags["layout"] = nil
            elseif mw.ustring.match(line, "Lyrics.*begin context") then
                    for context_name, corresponding_part in mw.ustring.gmatch(line, '\context Lyrics = "([%u%l]+)" { \\lyricsto "([%u%l]+)"') do
                       flags["context"] = context_name
                       context_types[context_name] = { "Lyrics", corresponding_part }
                       if lyric_text[context_name] == nil then
                            lyric_text[context_name] = {}
                       end
                    end
            elseif mw.ustring.match(line, "begin context") then
                    for context_type, context_name in mw.ustring.gmatch(line, '\context ([%u%l]+) = "([%u%l]+)"') do
                        flags["context"] = context_name
                        context_types[context_name] = { context_type, "n/a" }
                        if other_context_text[context_name] == nil then
                            other_context_text[context_name] = {}
                        end
                    end
            elseif mw.ustring.match(line, "end context") then
                flags["context"] = nil
            else
                if index == 1 and flags["layout"] ~= nil then
                    table.insert(layout_text, line)
                elseif index == 1 and flags["structure"] ~= nil then
                    table.insert(structure_text, line)
                elseif flags["context"] ~= nil then
                    current_context_type = context_types[flags["context"]][1]
                    if current_context_type == 'Lyrics' then
                        table.insert(lyric_text[flags["context"]], line)
                    else
                        table.insert(other_context_text[flags["context"]], line)
                    end
                end
            end
        end
    end
    score_text = score_text .. "<<\n"
    score_text = score_text .. table.concat(structure_text, "\n") .. "\n"
    for name, text_table in pairs(other_context_text) do
        score_text = score_text .. "\\context " .. context_types[name][1] .. " = \"" .. name .. "\" {\n" .. table.concat(text_table, "\n") .. "\n}\n"
    end
    for name, text_table in pairs(lyric_text) do
        local corresponding_part = context_types[name][2]
        score_text = score_text .. "\\context Lyrics = \"" .. name .. "\" { \\lyricsto \"" .. corresponding_part .. "\" { \n" .. table.concat(text_table, "\n") .. "\n} }\n"
    end
    score_text = score_text .. ">>\n"
    score_text = score_text .. table.concat(layout_text, "\n")
    score_text = frame:extensionTag( 'score', score_text, { midi = "1", vorbis = "1" } )
    return score_text
end

return p