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...") |
mNo edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function 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 | -- 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> | return string.format("<sup>[%s]</sup>", ref_number)..text | ||
else | else | ||
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