Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Citation: Difference between revisions

From ProleWiki, the proletarian encyclopedia
m (Attempting to fix line-break)
(Adopting automatic usage of reference to library work based exclusively on title name, if library work is available. URL parameter is ignored in case there is a library work available.)
 
(40 intermediate revisions by 2 users not shown)
Line 3: Line 3:
local function is_set (var)
local function is_set (var)
return not (var == nil or var == '');
return not (var == nil or var == '');
end
function is_lib_work(s)
return mw.getCurrentFrame():callParserFunction('#ifexist',"Library:" .. s,true) == "1"
end
end


local function italics (var)
local function italics (var)
return "''" .. var .. "''"
return "<i>" .. var .. "</i>"
end
end


local function parenthesis (var)
local function parenthesis (var)
return " (" .. var .. ")"
return "(" .. var .. ")"
end
end


function p.cite( frame )
function p.cite( frame )
local pframe = frame:getParent()
local pframe = frame:getParent()
local Author = pframe.args.author
local Author = pframe.args.author
local Year = pframe.args.year
local Year = pframe.args.year or pframe.args['date']
local Chapter = pframe.args.chapter
local Chapter = pframe.args.chapter
local ChapterURL = pframe.args['chapter-url']
local ChapterURL = pframe.args['chapter-url']
local Section = pframe.args.section
local Title = pframe.args.title
local Title = pframe.args.title
local TitleURL = pframe.args['title-url']
local TitleURL = pframe.args['title-url']
local LibraryTitle = pframe.args['library-title']
local Volume = pframe.args.volume
local Series = pframe.args.series or pframe.args.journal
local Trans = pframe.args['trans-title']
local Trans = pframe.args['trans-title']
local TransLang = pframe.args['trans-lang']
local TransLang = pframe.args['trans-lang']
local Page = pframe.args.page
local Page = pframe.args.page or pframe.args.pages
local PDF = pframe.args.pdf
local PDF = pframe.args.pdf
local City = pframe.args.city
local City = pframe.args.city
local Publisher = pframe.args.publisher
local Publisher = pframe.args.publisher or pframe.args.via
local ISBN = pframe.args.isbn
local ISBN = pframe.args.isbn or pframe.args.issn
local ISSN = false
if is_set(pframe.args.issn) then
ISSN = true
end
local DOI = pframe.args.doi
local DOI = pframe.args.doi
local LG = pframe.args.lg
local LG = pframe.args.lg
local MIA = pframe.args.mia
local MIA = pframe.args.mia
local Quote = pframe.args.quote
local Quote = pframe.args.quote
local Image = pframe.args.image
local ImageCaption = pframe.args['image-caption']
local Dotless = pframe.args.dotless


if is_set (Author) then
if is_set (Author) then
if is_set (Year) then
if is_set (Year) then
first_part = Author .. parenthesis (Year) .. ". "
first_part = Author .. " " .. parenthesis (Year) .. "."
else
else
first_part = Author .. "."
first_part = Author .. "."
end
end
else
else
first_part = ""
first_part = ''
end
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)
first_part = italics(tostring(div)) .. tostring(br) .. first_part
end
if is_set (Image) then
local span = mw.html.create ( 'span' )
span
:wikitext('[[File:' .. Image .. '|center|300px|frameless]]')
if is_set(ImageCaption) then
local caption = mw.html.create ('div')
caption
:attr("style", "text-align:center;font-size:85%;")
:wikitext(ImageCaption)
first_part = tostring(span) .. tostring(caption) .. first_part
else
first_part = tostring(span) .. first_part
end
end
-- second part: Title(URL), Series, Volume, Chapter(URL), Year, Page, Trans(Lang), PDF
local second_part = ''


if is_set (Title) then
if is_set (Title) then
if is_set (TitleURL) then
if is_lib_work(Title) then
Title = "[[Library:" .. Title .. "|" .. Title .. "]]"
elseif is_set (TitleURL) then
Title = "[" .. TitleURL .. " " .. Title .. "]"
Title = "[" .. TitleURL .. " " .. Title .. "]"
end
Title = italics(Title)
if not is_set (Series) then
if is_set (Volume) then
Title = Title .. ", vol. " .. Volume
end
end
end
second_part = Title
if is_set (Chapter) then
if is_set (Chapter) then
if is_set (ChapterURL) then
if is_set (ChapterURL) then
Chapter = "[" .. ChapterURL .. " " .. Chapter .. "]"
Chapter = "[" .. ChapterURL .. " " .. Chapter .. "]"
end
end
second_part = "'" .. Chapter .. "'" .. " in " .. "''" .. Title .. "''"
if is_set (Section) then
else
Chapter = Chapter .. "; " .. Section
second_part = italics(Title)
end
if not is_set (Author) then
second_part = second_part .. ": " .. "'" .. Chapter .. "'"
if is_set (Year) then
end
second_part = second_part .. " (" .. Year .. ")"
end
if not is_set (Author) then
end
if is_set (Year) then
second_part = second_part .. " " .. parenthesis (Year)
end
end
end
if is_set (Trans) then
if is_set (Trans) then
if is_set (TransLang) then
if is_set (TransLang) then
second_part = second_part .. " (" .. TransLang .. ": " .. Trans .. ")"
second_part = second_part .. " " .. "(" .. TransLang .. ": " .. Trans .. ")"
else
else
second_part = second_part .. " (" .. Trans .. ")"
second_part = second_part .. " " .. parenthesis (Trans)
end
end
end
if is_set (Page) and not is_set (Volume) then
if tonumber(Page) ~= nil then
Page = "p." .. " " .. Page
else
Page = "pp." .. " " .. Page
end
second_part = second_part .. " " .. parenthesis (Page) .. "."
else
second_part = second_part .. "."
end
end
if is_set (Series) then
if is_set (Volume) then
Series = Series .. ", vol." .. Volume
end
end


if is_set (Page) then
if is_set (Page) and is_set (Volume) and is_set (Series) then
if tonumber(Page) ~= nil then
if tonumber(Page) ~= nil then
Page = "p. " .. Page
Page = "p." .. " " .. Page
else
else
Page = "pp. " .. Page
Page = "pp." .. " " .. Page
end
end
second_part = second_part .. " (" .. Page .. ")."
second_part = second_part .. " " .. Series  .. " " .. parenthesis (Page) .. "."
else
else
second_part = second_part .. "."
second_part = second_part .. " " .. Series .. "."
end
end
end
end
Line 92: Line 164:
second_part = second_part .. " " .. tostring(span)
second_part = second_part .. " " .. tostring(span)
end
end
-- third part: City, Publisher
if is_set (City) then
if is_set (City) then
Line 98: Line 172:
else
else
third_part = City .. "."
third_part = City .. "."
end
end
elseif is_set(Publisher) then
end
 
if not is_set(City) then
if is_set (Publisher) then
third_part = Publisher .. "."
third_part = Publisher .. "."
else
else
third_part = ""
third_part = ''
end
end
end
-- fourth part: ISBN, DOI, Library Genesis, Marxists Internet Archive


fourth_part = ""
fourth_part = ''


if is_set (ISBN) then
if is_set (ISBN) then
local span = mw.html.create ( 'span' )
local span = mw.html.create ( 'span' )
span
span
:attr( 'title', 'WorldCat')
:addClass('plainlinks')
:addClass('plainlinks')
:tag( 'small' )
if (ISSN) then
:wikitext('[' .. "https://www.worldcat.org/search?qt=worldcat_org_all&q=" .. ISBN .. " " .. "ISBN " .. ISBN .. ']')
span
:tag( 'small' )
:attr( 'title', 'The ISSN Portal')
:wikitext('[' .. "https://portal.issn.org/resource/ISSN/" .. ISBN .. " " .. "ISSN " .. ISBN .. ']')
else
span
:tag( 'small' )
:attr( 'title', 'WorldCat')
:wikitext('[' .. "https://www.worldcat.org/search?qt=worldcat_org_all&q=" .. ISBN .. " " .. "ISBN " .. ISBN .. ']')
end
fourth_part = fourth_part .. tostring(span)
fourth_part = fourth_part .. tostring(span)
end
end


if is_set (DOI) then
if is_set (DOI) then
fourth_part = fourth_part .. " doi:" .. DOI
local doispan = mw.html.create ( 'span' )
doispan
:tag('small')
:wikitext("doi: " .. DOI)
fourth_part = fourth_part .. " " .. tostring(doispan)
local span = mw.html.create ( 'span' )
local span = mw.html.create ( 'span' )
span
span
Line 147: Line 239:
fourth_part = fourth_part .." " .. tostring(span)
fourth_part = fourth_part .." " .. tostring(span)
end
end
if is_set (Quote) then
if is_set (second_part) then
local div = mw.html.create ('div')
second_part = " " .. second_part
div
:attr("style", "width:80%; margin-left:10%;")
:wikitext(Quote)
local br = mw.html.create(br, selfClosing)
first_part = italics(tostring(div)) .. br .. first_part
end
end
 
if is_set (third_part) then
if is_set (first_part) then
third_part = " " .. third_part
first_part = first_part .. " "
end
end
if is_set (second_part) then
if is_set (fourth_part) then
second_part = second_part .. " "
fourth_part = " " .. fourth_part
end
end
if is_set (third_part) then
local render = first_part .. second_part .. third_part .. fourth_part
third_part = third_part .. " "
-- trim render
render = string.gsub(render,"^%s*(.-)%s*$","%1")
if Dotless then
render = render:sub(1,-2)
end
end


return first_part .. second_part .. third_part .. fourth_part
return render
end
end
return p
return p

Latest revision as of 03:08, 28 April 2024

local p = {}

local function is_set (var)
	return not (var == nil or var == '');
end

function is_lib_work(s)
	return mw.getCurrentFrame():callParserFunction('#ifexist',"Library:" .. s,true) == "1"
end

local function italics (var)
	return "<i>" .. var .. "</i>"
end

local function parenthesis (var)
	return "(" .. var .. ")"
end

function p.cite( frame )
	local pframe = frame:getParent()
	local Author = pframe.args.author
	local Year = pframe.args.year or pframe.args['date']
	local Chapter = pframe.args.chapter
	local ChapterURL = pframe.args['chapter-url']
	local Section = pframe.args.section
	local Title = pframe.args.title
	local TitleURL = pframe.args['title-url']
	local LibraryTitle = pframe.args['library-title']
	local Volume = pframe.args.volume
	local Series = pframe.args.series or pframe.args.journal
	local Trans = pframe.args['trans-title']
	local TransLang = pframe.args['trans-lang']
	local Page = pframe.args.page or pframe.args.pages
	local PDF = pframe.args.pdf
	local City = pframe.args.city
	local Publisher = pframe.args.publisher or pframe.args.via
	local ISBN = pframe.args.isbn or pframe.args.issn
	local ISSN = false
	if is_set(pframe.args.issn) then
		ISSN = true
	end
	local DOI = pframe.args.doi
	local LG = pframe.args.lg
	local MIA = pframe.args.mia
	local Quote = pframe.args.quote
	local Image = pframe.args.image
	local ImageCaption = pframe.args['image-caption']
	local Dotless = pframe.args.dotless


	if is_set (Author) then
		if is_set (Year) then
			first_part = Author .. " " .. parenthesis (Year) .. "."
			else
				first_part = Author .. "."
		end
		else
			first_part = ''
	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)
		first_part = italics(tostring(div)) .. tostring(br) .. first_part
	end
	
	if is_set (Image) then
		local span = mw.html.create ( 'span' )
		span
		:wikitext('[[File:' .. Image .. '|center|300px|frameless]]')

		if is_set(ImageCaption) then 
			local caption = mw.html.create ('div')
			caption
			:attr("style", "text-align:center;font-size:85%;")
			:wikitext(ImageCaption)
			first_part = tostring(span) .. tostring(caption) .. first_part
			else
				first_part = tostring(span) .. first_part
		end
				
	end
	
	-- second part: Title(URL), Series, Volume, Chapter(URL), Year, Page, Trans(Lang), PDF
	local second_part = ''

	if is_set (Title) then
		if is_lib_work(Title) then
			Title = "[[Library:" .. Title .. "|" .. Title .. "]]"
			elseif is_set (TitleURL) then
			Title = "[" .. TitleURL .. " " .. Title .. "]"
		end
		Title = italics(Title)
		if not is_set (Series) then
			if is_set (Volume) then
				Title = Title .. ", vol. " .. Volume
			end
		end
		second_part = Title
		if is_set (Chapter) then
			if is_set (ChapterURL) then
				Chapter = "[" .. ChapterURL .. " " .. Chapter .. "]"
			end
			if is_set (Section) then
				Chapter = Chapter .. "; " .. Section
			end
			second_part = second_part .. ": " .. "'" .. Chapter .. "'"
		end
		
		if not is_set (Author) then
			if is_set (Year) then
				second_part = second_part .. " " .. parenthesis (Year)
			end
		end
		
		if is_set (Trans) then
			if is_set (TransLang) then
				second_part = second_part .. " " .. "(" .. TransLang .. ": " .. Trans .. ")"
				else
					second_part = second_part .. " " .. parenthesis (Trans)
			end
		end
		if is_set (Page) and not is_set (Volume) then
			if tonumber(Page) ~= nil then
				Page = "p." .. " " .. Page
				else
					Page = "pp." .. " " .. Page
			end
				second_part = second_part .. " " .. parenthesis (Page) .. "."
			else
				second_part = second_part .. "."
		end
	end
	
	if is_set (Series) then
		if is_set (Volume) then
			Series = Series .. ", vol." .. Volume
		end

		if is_set (Page) and is_set (Volume) and is_set (Series) then
			if tonumber(Page) ~= nil then
				Page = "p." .. " " .. Page
				else
					Page = "pp." .. " " .. Page
			end
				second_part = second_part .. " " .. Series  .. " " .. parenthesis (Page) .. "."
			else
				second_part = second_part .. " " .. Series .. "."
		end
	end
	
	if is_set (PDF) then
		local span = mw.html.create ( 'span' )
		span
		:attr( 'title', 'PDF download')
		:addClass('plainlinks')
		:tag( 'small' )
		:wikitext('[' .. PDF .. " " .. mw.text.nowiki('[PDF]') .. ']')
		second_part = second_part .. " " .. tostring(span)
	end
	
	-- third part: City, Publisher
	
	if is_set (City) then
		if is_set (Publisher) then
			third_part = City .. ": " .. Publisher .. "."
			else
				third_part = City .. "."
		end
	end

	if not is_set(City) then
		if is_set (Publisher) then
			third_part = Publisher .. "."
			else
				third_part = ''
		end
	end
	
	-- fourth part: ISBN, DOI, Library Genesis, Marxists Internet Archive

	fourth_part = ''

	if is_set (ISBN) then
		local span = mw.html.create ( 'span' )
		span
		:addClass('plainlinks')
		if (ISSN) then
			span
			:tag( 'small' )
			:attr( 'title', 'The ISSN Portal')
			:wikitext('[' .. "https://portal.issn.org/resource/ISSN/" .. ISBN .. " " .. "ISSN " .. ISBN .. ']')
			else
				span
				:tag( 'small' )
				:attr( 'title', 'WorldCat')
				:wikitext('[' .. "https://www.worldcat.org/search?qt=worldcat_org_all&q=" .. ISBN .. " " .. "ISBN " .. ISBN .. ']')
		end
		fourth_part = fourth_part .. tostring(span)
	end

	if is_set (DOI) then
		local doispan = mw.html.create ( 'span' )
		doispan
		:tag('small')
		:wikitext("doi: " .. DOI)
		fourth_part = fourth_part .. " " .. tostring(doispan)
		local span = mw.html.create ( 'span' )
		span
		:attr( 'title', 'Sci-Hub link')
		:addClass('plainlinks')
		:tag( 'small' )
		:wikitext('[' .. "https://sci-hub.se/" .. DOI .. " " .. mw.text.nowiki('[HUB]') .. ']')
		fourth_part = fourth_part .." " .. tostring(span)
	end
	
	if is_set (LG) then
		local span = mw.html.create ( 'span' )
		span
		:attr( 'title', 'Library Genesis link')
		:addClass('plainlinks')
		:tag( 'small' )
		:wikitext('[' .. LG .. " " .. mw.text.nowiki('[LG]') .. ']')
		fourth_part = fourth_part .." " .. tostring(span)
	end
	
	if is_set (MIA) then
		local span = mw.html.create ( 'span' )
		span
		:attr( 'title', 'Marxists Internet Archive link')
		:addClass('plainlinks')
		:tag( 'small' )
		:wikitext('[' .. MIA .. " " .. mw.text.nowiki('[MIA]') .. ']')
		fourth_part = fourth_part .." " .. tostring(span)
	end
		
	
	if is_set (second_part) then
		second_part = " " .. second_part
	end
	
	if is_set (third_part) then
		third_part = " " .. third_part
	end
	
	if is_set (fourth_part) then
		fourth_part = " " .. fourth_part
	end
	
	local render = first_part .. second_part .. third_part .. fourth_part
	
	-- trim render
	
	render = string.gsub(render,"^%s*(.-)%s*$","%1")
	
	if Dotless then
		render = render:sub(1,-2)
	end

	return render
end
return p