Module:NoteTransclude

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 15:09, 6 August 2024 by Vish (talk | contribs)
Jump to navigation Jump to search

Documentation for this module may be created at Module:NoteTransclude/doc

local p = {}

local function findEndOfNoteContent(content, startPos)
    local endPos = mw.ustring.find(content, "}}", startPos)
    if endPos then
        return endPos + 2
    else
        return #content
    end
end

local function getNoteContent(content, section, field)
    local sectionPattern = "==" .. section .. "==%s*(.-)%s*=="
    local sectionContent = mw.ustring.match(content, sectionPattern)
    if not sectionContent then
        return "Error: Section not found"
    end

    local noteStart, noteEnd = mw.ustring.find(sectionContent, "{{Note")
    if not noteStart then
        return "Error: Note template not found"
    end
    noteEnd = findEndOfNoteContent(sectionContent, noteStart)

    local noteContent = sectionContent:sub(noteStart, noteEnd)
    
    if field == "Transcript" then
        local transcriptPattern = "|%s*Transcript%s*=%s*(.-)%s*|%s*Translation"
        local transcriptContent = mw.ustring.match(noteContent, transcriptPattern)
        if not transcriptContent then
            transcriptPattern = "|%s*Transcript%s*=%s*(.-)%s*}}"
            transcriptContent = mw.ustring.match(noteContent, transcriptPattern)
        end
        return transcriptContent or "Error: Transcript not found"
    elseif field == "Translation" then
        local translationPattern = "|%s*Translation%s*=%s*(.-)%s*}}"
        local translationContent = mw.ustring.match(noteContent, translationPattern)
        if not translationContent then
            local translationStart = mw.ustring.find(noteContent, "|%s*Translation%s*=%s*")
            if translationStart then
                translationContent = mw.ustring.sub(noteContent, translationStart, noteEnd)
                translationContent = mw.ustring.match(translationContent, "|%s*Translation%s*=%s*(.-)%s*}}")
            end
        end
        return translationContent or "Error: Translation not found"
    else
        return "Error: Invalid field. Use 'Transcript' or 'Translation'."
    end
end

function p.transclude(frame)
    local args = frame:getParent().args
    local page = args[1]
    local section = args[2]
    local field = args[3]

    if not (page and section and field) then
        return "Error: Missing parameters. Usage: {{NoteTransclude|Page|Section|Field}}"
    end

    local content = mw.title.new(page):getContent()
    if not content then
        return "Error: Page not found"
    end

    return frame:preprocess(getNoteContent(content, section, field))
end

return p