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

Module:Cite book: Difference between revisions

From Wings of Fire Wiki
create lua module- I haven't tested this at all so don't use it on the template yet
 
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
local error = require('Module:error')


function p.main(frame)
function p.main(frame)
local args = frame.args
local args = (frame.args.book ~= nil and frame.args[1] ~= nil and frame.args) or frame:getParent().args
local result = ''
local result = ''
if not args.book then
if not args.book then
return '{{error|<code>book</code> parameter is required}}'
return error.error({
message = '<code>book</code> parameter is required'
})
else  
else  
result = formatBookTitle(args.book)
result = formatBookTitle(args.book)
end
end
if args.chapter then
if args.chapter then
if tonumber(chapter, 10) then
result = result .. ', ' .. formatChapter(args.chapter)
result = result .. ', chapter ' .. args.chapter
end
else
if args.location then
result = result .. ', ' .. args.chapter
result = result .. ', ' .. args.location
end
end
end
if args.page then
if args.page then
result = result .. ', page ' .. args.page  
result = result .. ', page ' .. args.page  
elseif args.pages then
result = result .. ', pages ' .. args.pages
elseif args.paragraph then
elseif args.paragraph then
result = result .. ', paragraph ' .. args.paragraph  
if tonumber(args.paragraph, 10) then
result = result .. ', paragraph ' .. args.paragraph  
else
return error.error({
message = '<code>paragraph</code> parameter must be an integer'
})
end
end
end
return result
return result

Latest revision as of 18:19, 19 January 2025

Documentation for this module may be created at Module:Cite book/doc

local p = {}

local error = require('Module:error')

function p.main(frame)
	local args = (frame.args.book ~= nil and frame.args[1] ~= nil and frame.args) or frame:getParent().args
	local result = ''
	if not args.book then
		return error.error({
			message = '<code>book</code> parameter is required'
		})
	else 
		result = formatBookTitle(args.book)
	end
	if args.chapter then
		result = result .. ', ' .. formatChapter(args.chapter)
	end
	if args.location then
		result = result .. ', ' .. args.location
	end
	if args.page then
		result = result .. ', page ' .. args.page 
	elseif args.pages then 
		result = result .. ', pages ' .. args.pages
	elseif args.paragraph then
		if tonumber(args.paragraph, 10) then
			result = result .. ', paragraph ' .. args.paragraph 
		else
			return error.error({
				message = '<code>paragraph</code> parameter must be an integer'
			})
		end
	end
	return result
end

function formatChapter(chapter)
	if tonumber(chapter, 10) then
		return 'chapter ' .. chapter
	else
		return chapter
	end
end

function formatBookTitle(title)
	local page = mw.title.new(title, 0)
	if page and page.exists then
		return '\'\'[[' .. title .. ']]\'\''
	else
		return '\'\'' .. title .. '\'\''
	end
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.