More languages
More actions
m Correctly output an error. Testing uncommenting require line... |
m Removed more useless functions. Not even sure why they were there. |
||
| (11 intermediate revisions by 2 users not shown) | |||
| Line 2: | Line 2: | ||
require("Module:Auxiliary functions") | require("Module:Auxiliary functions") | ||
function getDataFromTitle(title) | function getDataFromTitle(title) | ||
local rawString = mw.getCurrentFrame():callParserFunction("#dpl","namespace=Library","title=" .. title,"mode=userformat","include={Library work}:title:author:published_date:source") | local rawString = mw.getCurrentFrame():callParserFunction("#dpl","namespace=Library","title=" .. title,"mode=userformat","include={Library work}:title:author:published_date:source") | ||
local title,author,published_date,source = rawString:match("^(.-)%s|(.-)%s|(.-)%s|(.-)$") | local title,author,published_date,source = rawString:match("^(.-)%s|(.-)%s|(.-)%s|(.-)$") | ||
local year = published_date:match("%d%d%d%d") | local year = "" | ||
if is_set(published_date) then | |||
year = published_date:match("%d%d%d%d") | |||
end | |||
return title,author,year,source | return title,author,year,source | ||
end | end | ||
| Line 54: | Line 15: | ||
function getDataFromLink(link) | function getDataFromLink(link) | ||
-- In case it's in URL format | -- In case it's in URL format | ||
local str = link | local str = mw.uri.decode(link) | ||
local result = str:match("https?://[^/]+/wiki/(.*)") | local result = str:match("https?://[^/]+/wiki/(.*)") | ||
if result then | if result then | ||
| Line 89: | Line 50: | ||
local parentFrame = frame:getParent() | local parentFrame = frame:getParent() | ||
-- get arguments from "args" table pre-defined by MediaWiki/Scribunto | -- get arguments from "args" table pre-defined by MediaWiki/Scribunto | ||
local link = | local link = parentFrame.args.link | ||
local author = nil | local author = nil | ||
local quote = parentFrame.args.quote | |||
if is_set(link) then | if is_set(link) then | ||
title,author,year,source = getDataFromLink(link) | title,author,year,source = getDataFromLink(link) | ||
| Line 99: | Line 61: | ||
if is_set(author) then | if is_set(author) then | ||
render = render .. wikilinkFromAuthor(author) | render = render .. wikilinkFromAuthor(author) | ||
if not is_set(year) then | |||
render = render .. "." | |||
end | |||
end | end | ||
if is_set(year) then | if is_set(year) then | ||
| Line 117: | Line 82: | ||
:wikitext(quote) | :wikitext(quote) | ||
local br = mw.html.create ('br', selfClosing) | local br = mw.html.create ('br', selfClosing) | ||
render = italics(tostring(div)) .. tostring(br) .. | render = italics(tostring(div)) .. tostring(br) .. render | ||
end | end | ||
return render | return render | ||
end | end | ||
return p | return p | ||
Latest revision as of 03:35, 19 September 2025
local p = {}
require("Module:Auxiliary functions")
function getDataFromTitle(title)
local rawString = mw.getCurrentFrame():callParserFunction("#dpl","namespace=Library","title=" .. title,"mode=userformat","include={Library work}:title:author:published_date:source")
local title,author,published_date,source = rawString:match("^(.-)%s|(.-)%s|(.-)%s|(.-)$")
local year = ""
if is_set(published_date) then
year = published_date:match("%d%d%d%d")
end
return title,author,year,source
end
function getDataFromLink(link)
-- In case it's in URL format
local str = mw.uri.decode(link)
local result = str:match("https?://[^/]+/wiki/(.*)")
if result then
str = result -- Use the extracted part after /wiki/
end
-- Remove "Library:" prefix, if present
str = str:gsub("^Library:", "")
-- Replace underscores with spaces
str = str:gsub("_", " ")
-- Return data using the other function
return getDataFromTitle(str)
end
function wikilinkFromTitle(title)
local pagename = mw.getCurrentFrame():callParserFunction("#dpl","namespace=Library","titleregexp=" .. title .. "$","format=,%TITLE%")
if is_set(pagename) then
return "[[Library:" .. pagename .. "|" .. title .. "]]"
end
return title
end
local function wikilinkFromAuthor(author)
-- if page exists
if mw.getCurrentFrame():callParserFunction('#ifexist',author,true) == "1" then
return "[[" .. author .. "]]"
end
return author
end
function p.cite(frame)
-- Parent frame to get parameters from template calls instead of from {{#invoke: ...
local parentFrame = frame:getParent()
-- get arguments from "args" table pre-defined by MediaWiki/Scribunto
local link = parentFrame.args.link
local author = nil
local quote = parentFrame.args.quote
if is_set(link) then
title,author,year,source = getDataFromLink(link)
end
local render = ""
if is_set(author) then
render = render .. wikilinkFromAuthor(author)
if not is_set(year) then
render = render .. "."
end
end
if is_set(year) then
render = render .. " (" .. year .. ")."
end
if is_set(title) then
render = render .. " ''" .. wikilinkFromTitle(title) .. "''."
end
if is_set(source) then
render = render .. " " .. source .. "."
end
if is_set (quote) then
quote = string.gsub(quote,"\n","<br>")
quote = "“" .. quote .. "”"
local div = mw.html.create ('div')
div
:attr("style", "width:80%; margin-left:10%;")
:wikitext(quote)
local br = mw.html.create ('br', selfClosing)
render = italics(tostring(div)) .. tostring(br) .. render
end
return render
end
return p