Module:Reference: Difference between revisions

From Ediaqi Wiki
(Created page with "local p = {} function p.get_reference(frame) local name = frame.args.name local reading_list_page = "Reading List" -- Change if your page is named differently if not name or name == "" then return '<strong class="error">Error: Reference name not provided.</strong>' end local success, content = pcall(mw.title.new(reading_list_page).getContent, mw.title.new(reading_list_page)) if not success then return '<strong class="error">Error: Could not load Reading List...")
 
mNo edit summary
 
Line 1: Line 1:
local p = {}
local p = {}


function p.get_reference(frame)
function p.get_reference_number(frame)
local name = frame.args.name
    local name = frame.args.name
local reading_list_page = "Reading List" -- Change if your page is named differently
    local reading_list_page = "Reading List" -- Change if your page is named differently


if not name or name == "" then
    if not name or name == "" then
return '<strong class="error">Error: Reference name not provided.</strong>'
        return '<strong class="error">Error: Reference name not provided.</strong>'
end
    end


local success, content = pcall(mw.title.new(reading_list_page).getContent, mw.title.new(reading_list_page))
    local success, content = pcall(mw.title.new(reading_list_page).getContent, mw.title.new(reading_list_page))
if not success then
    if not success then
return '<strong class="error">Error: Could not load Reading List page.</strong>'
        return '<strong class="error">Error: Could not load Reading List page.</strong>'
end
    end


local ref_start = string.find(content, '<div id="' .. name .. '"')
    local ref_start = string.find(content, '<div id="' .. name .. '"')
if not ref_start then
    if not ref_start then
return '<strong class="error">Error: Reference "' .. name .. '" not found in Reading List.</strong>'
        return '<strong class="error">Error: Reference "' .. name .. '" not found in Reading List.</strong>'
end
    end


local ref_end = string.find(content, "</div>", ref_start)
    local ref_end = string.find(content, "</div>", ref_start)
local ref_content = string.sub(content, ref_start, ref_end + 5) -- +5 to include "</div>"
    local ref_content = string.sub(content, ref_start, ref_end + 5) -- +5 to include "</div>"


     -- Extract the content within the div
     -- Extract the content within the div
Line 26: Line 26:
     local text_end = string.find(ref_content, "</div>", 1, true) - 1
     local text_end = string.find(ref_content, "</div>", 1, true) - 1
     local text = string.sub(ref_content, text_start, text_end)
     local text = string.sub(ref_content, text_start, text_end)
      
 
     -- Find the position of the first closing square bracket to identify the superscript number
    local bracket_pos = string.find(text, "%]")
    if bracket_pos then
        -- Extract the reference number
        local ref_number = string.sub(text, 2, bracket_pos - 1)
        -- Construct the final formatted reference text
        return string.format("<sup>[%s]</sup>", ref_number)
    else
        -- If no closing square bracket is found, return an error message or handle it differently
        return '<strong class="error">Error: No reference number found in "' .. name .. '".</strong>'
    end
end
 
function p.get_reference_content(frame)
    local name = frame.args.name
    local reading_list_page = "Reading List" -- Change if your page is named differently
 
    if not name or name == "" then
        return '<strong class="error">Error: Reference name not provided.</strong>'
    end
 
    local success, content = pcall(mw.title.new(reading_list_page).getContent, mw.title.new(reading_list_page))
    if not success then
        return '<strong class="error">Error: Could not load Reading List page.</strong>'
    end
 
    local ref_start = string.find(content, '<div id="' .. name .. '"')
    if not ref_start then
        return '<strong class="error">Error: Reference "' .. name .. '" not found in Reading List.</strong>'
    end
 
    local ref_end = string.find(content, "</div>", ref_start)
    local ref_content = string.sub(content, ref_start, ref_end + 5) -- +5 to include "</div>"
 
    -- Extract the content within the div
    local text_start = string.find(ref_content, ">", 1, true) + 1
    local text_end = string.find(ref_content, "</div>", 1, true) - 1
    local text = string.sub(ref_content, text_start, text_end)
 
     -- Find the position of the first closing square bracket to identify the superscript number
     -- Find the position of the first closing square bracket to identify the superscript number
     local bracket_pos = string.find(text, "%]")
     local bracket_pos = string.find(text, "%]")
Line 33: Line 72:
         local ref_number = string.sub(text, 2, bracket_pos - 1)
         local ref_number = string.sub(text, 2, bracket_pos - 1)
         -- Construct the final formatted reference text
         -- Construct the final formatted reference text
         return string.format("<sup>[%s]</sup>%s", ref_number, text)
         return string.format("<sup>[%s]</sup>", ref_number)..text
     else
     else
        -- If no closing square bracket is found, return the entire text
         return text
         return text
     end
     end

Latest revision as of 15:39, 24 January 2025

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

local p = {}

function p.get_reference_number(frame)
    local name = frame.args.name
    local reading_list_page = "Reading List" -- Change if your page is named differently

    if not name or name == "" then
        return '<strong class="error">Error: Reference name not provided.</strong>'
    end

    local success, content = pcall(mw.title.new(reading_list_page).getContent, mw.title.new(reading_list_page))
    if not success then
        return '<strong class="error">Error: Could not load Reading List page.</strong>'
    end

    local ref_start = string.find(content, '<div id="' .. name .. '"')
    if not ref_start then
        return '<strong class="error">Error: Reference "' .. name .. '" not found in Reading List.</strong>'
    end

    local ref_end = string.find(content, "</div>", ref_start)
    local ref_content = string.sub(content, ref_start, ref_end + 5) -- +5 to include "</div>"

    -- Extract the content within the div
    local text_start = string.find(ref_content, ">", 1, true) + 1
    local text_end = string.find(ref_content, "</div>", 1, true) - 1
    local text = string.sub(ref_content, text_start, text_end)

    -- Find the position of the first closing square bracket to identify the superscript number
    local bracket_pos = string.find(text, "%]")
    if bracket_pos then
        -- Extract the reference number
        local ref_number = string.sub(text, 2, bracket_pos - 1)
        -- Construct the final formatted reference text
        return string.format("<sup>[%s]</sup>", ref_number)
    else
        -- If no closing square bracket is found, return an error message or handle it differently
        return '<strong class="error">Error: No reference number found in "' .. name .. '".</strong>'
    end
end

function p.get_reference_content(frame)
    local name = frame.args.name
    local reading_list_page = "Reading List" -- Change if your page is named differently

    if not name or name == "" then
        return '<strong class="error">Error: Reference name not provided.</strong>'
    end

    local success, content = pcall(mw.title.new(reading_list_page).getContent, mw.title.new(reading_list_page))
    if not success then
        return '<strong class="error">Error: Could not load Reading List page.</strong>'
    end

    local ref_start = string.find(content, '<div id="' .. name .. '"')
    if not ref_start then
        return '<strong class="error">Error: Reference "' .. name .. '" not found in Reading List.</strong>'
    end

    local ref_end = string.find(content, "</div>", ref_start)
    local ref_content = string.sub(content, ref_start, ref_end + 5) -- +5 to include "</div>"

    -- Extract the content within the div
    local text_start = string.find(ref_content, ">", 1, true) + 1
    local text_end = string.find(ref_content, "</div>", 1, true) - 1
    local text = string.sub(ref_content, text_start, text_end)

    -- Find the position of the first closing square bracket to identify the superscript number
    local bracket_pos = string.find(text, "%]")
    if bracket_pos then
        -- Extract the reference number
        local ref_number = string.sub(text, 2, bracket_pos - 1)
        -- Construct the final formatted reference text
        return string.format("<sup>[%s]</sup>", ref_number)..text
    else
        return text
    end
end

return p