Module:CitationLite: Difference between revisions

From Chalo Chatu, Zambia online encyclopedia
No edit summary
No edit summary
 
Line 19: Line 19:


local function list_from(s)
local function list_from(s)
-- Accept lists split by ";", "," or " and "
-- Accept lists split by ";", "," or ","
if isempty(s) then return nil end
if isempty(s) then return nil end
local items = {}
local items = {}
Line 25: Line 25:
table.insert(items, trim(part))
table.insert(items, trim(part))
end
end
-- If only one with commas, split on commas
if #items == 1 and items[1]:find(",") then
if #items == 1 and items[1]:find(",") then
items = {}
items = {}
for part in s:gmatch("[^,]+") do table.insert(items, trim(part)) end
for part in tostring(s):gmatch("[^,]+") do
table.insert(items, trim(part))
end
end
end
return (#items > 0) and items or nil
return (#items > 0) and items or nil
Line 64: Line 65:
local function render_archive(url, date)
local function render_archive(url, date)
if isempty(url) then return nil end
if isempty(url) then return nil end
local label = isempty(date) and "Archived" or ("Archived " .. date)
local label = isempty(date) and "Archived" or ("Archived " .. tostring(date))
return extlink(url, label)
return extlink(url, label)
end
end
Line 73: Line 74:
if not isempty(url) then table.insert(bits, extlink(url, "URL")) end
if not isempty(url) then table.insert(bits, extlink(url, "URL")) end
if not isempty(archive_url) then table.insert(bits, render_archive(archive_url, archive_date)) end
if not isempty(archive_url) then table.insert(bits, render_archive(archive_url, archive_date)) end
if not isempty(url_status) then table.insert(bits, "(" .. url_status .. ")") end
if not isempty(url_status) then table.insert(bits, "(" .. tostring(url_status) .. ")") end
return join(bits, " • ")
return join(bits, " • ")
end
end
Line 94: Line 95:
end
end
if #authors == 0 then return nil end
if #authors == 0 then return nil end
-- Join authors with ", " and " & " for last two if > 1
if #authors == 1 then
if #authors == 1 then
return authors[1]
return authors[1]
Line 107: Line 107:
local function render_lang(lang)
local function render_lang(lang)
if isempty(lang) then return nil end
if isempty(lang) then return nil end
return "(" .. lang .. ")"
return "(" .. tostring(lang) .. ")"
end
end


Line 118: Line 118:
end
end


-- New: safe prefix helper to avoid nil concatenation
-- Safe prefix helper to avoid nil concatenation
local function prefixed(prefix, v)
local function prefixed(prefix, v)
if isempty(v) then return nil end
if isempty(v) then return nil end
Line 126: Line 126:
-- ========== Core builders ==========
-- ========== Core builders ==========
local function cite_web_like(kind, args)
local function cite_web_like(kind, args)
-- Common for web/news
-- author. "Title". ''Website/Work''. Publisher. Date. URL-Block. Accessed access-date. Quote
-- author. "Title". ''Website/Work''. Publisher. Date. URL-Block. Accessed access-date. Quote
local author   = render_authors(args)
local author   = render_authors(args)
local title   = quoted(args.title or args.chapter or args.article)
local title     = quoted(args.title or args.chapter or args.article)
local work     = italic(args.website or args.work or (kind == "news" and args.newspaper) or nil)
local work     = italic(args.website or args.work or (kind == "news" and args.newspaper) or nil)
local publisher= args.publisher
local publisher = args.publisher
local date     = args.date or args["publication-date"]
local date     = args.date or args["publication-date"]
local urlblk   = render_url_block(args.url, args["archive-url"], args["archive-date"], args["url-status"])
local urlblk   = render_url_block(args.url, args["archive-url"], args["archive-date"], args["url-status"])
local access   = prefixed("Accessed ", args["access-date"])
local access   = prefixed("Accessed ", args["access-date"])
local lang     = render_lang(args.language or args.lang)
local lang     = render_lang(args.language or args.lang)
local quote   = isempty(args.quote) and nil or ("“" .. args.quote .. "”")
local quote     = isempty(args.quote) and nil or ("“" .. tostring(args.quote) .. "”")


local body = punct_concat({
local body = punct_concat({
Line 153: Line 152:
local function cite_book(args)
local function cite_book(args)
-- author. ''Title''. Edition. Publisher, Year. Place. ISBN. Pages. URL-Block. Accessed access-date.
-- author. ''Title''. Edition. Publisher, Year. Place. ISBN. Pages. URL-Block. Accessed access-date.
local author   = render_authors(args)
local author   = render_authors(args)
local title   = italic(args.title)
local title     = italic(args.title)
local edition = args.edition
local edition   = args.edition
local year     = args.year or args.date
local year     = args.year or args.date
local place   = args.place or args.location
local place     = args.place or args.location
local publisher= args.publisher
local publisher = args.publisher
local isbn     = args.isbn
local isbn     = args.isbn
local pages   = args.pages or args.page
local pages     = args.pages or args.page
local urlblk   = render_url_block(args.url, args["archive-url"], args["archive-date"], args["url-status"])
local urlblk   = render_url_block(args.url, args["archive-url"], args["archive-date"], args["url-status"])
local access   = prefixed("Accessed ", args["access-date"])
local access   = prefixed("Accessed ", args["access-date"])
local lang     = render_lang(args.language or args.lang)
local lang     = render_lang(args.language or args.lang)


local publine = nil
local publine
if not isempty(publisher) and not isempty(year) then
if not isempty(publisher) and not isempty(year) then
publine = publisher .. ", " .. year
publine = publisher .. ", " .. tostring(year)
else
else
publine = publisher or year
publine = publisher or year
Line 177: Line 176:
publine,
publine,
place,
place,
isbn and ("ISBN " .. isbn) or nil,
prefixed("ISBN ", isbn),
pages and ("pp. " .. pages) or nil,
pages and ("pp. " .. tostring(pages)) or nil,
urlblk,
urlblk,
access,
access,
Line 196: Line 195:
local pages    = args.pages or args.page
local pages    = args.pages or args.page
local year    = args.year or args.date
local year    = args.year or args.date
local doi      = isempty(args.doi) and nil or ("doi:" .. args.doi)
local doi      = prefixed("doi:", args.doi)
local urlblk  = render_url_block(args.url, args["archive-url"], args["archive-date"], args["url-status"])
local urlblk  = render_url_block(args.url, args["archive-url"], args["archive-date"], args["url-status"])
local access  = prefixed("Accessed ", args["access-date"])
local access  = prefixed("Accessed ", args["access-date"])
Line 203: Line 202:
local voliss
local voliss
if not isempty(volume) and not isempty(issue) then
if not isempty(volume) and not isempty(issue) then
voliss = volume .. " (" .. issue .. ")"
voliss = tostring(volume) .. " (" .. tostring(issue) .. ")"
else
else
voliss = volume or issue
voliss = volume or issue
end
end


local pagestr = not isempty(pages) and (pages) or nil  -- e.g., 12–34
local pagestr = not isempty(pages) and tostring(pages) or nil  -- e.g., 12–34


local after = join({
local after = join({