Module:NoteTransclude

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 14:57, 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, "}}%s*$", 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 notePattern = "{{Note.-}}"
    local noteContent = mw.ustring.match(sectionContent, notePattern)
    if not noteContent then
        return "Error: Note template not found"
    end

    local noteStart, noteEnd = mw.ustring.find(sectionContent, "{{Note")
    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)
        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)
        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