Module:NoteTransclude

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 15:26, 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 sectionExtractor = require('Module:SectionExtractor')

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 sectionContent = sectionExtractor.extractSection(content, section)
    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)
        return transcriptContent or "Error: Transcript not found"
    elseif field == "Translation" then
        local translationStart = mw.ustring.find(noteContent, "|%s*Translation%s*=%s*")
        if translationStart then
            local translationEnd = findEndOfNoteContent(noteContent, translationStart)
            local translationContent = mw.ustring.sub(noteContent, translationStart, translationEnd)
            translationContent = mw.ustring.match(translationContent, "|%s*Translation%s*=%s*(.-)%s*")
            return translationContent or "Error: Translation not found"
        else
            return "Error: Translation field not found"
        end
    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