Module:Reference: Difference between revisions
 (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...")  | 
			
(No difference) 
 | 
Revision as of 15:25, 24 January 2025
Documentation for this module may be created at Module:Reference/doc
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 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>%s", ref_number, text)
    else
        -- If no closing square bracket is found, return the entire text
        return text
    end
end
return p