Module:FacebookCite

-- -- This module implements Template:FacebookCite.

local p = {} local lang = mw.language.getCurrentLanguage

local function makeWikitextError(msg) local ret = '' ret = ret .. string.format(		' Template:FacebookCite error: %s. ',		msg	) if mw.title.getCurrentTitle.namespace == 0 then ret = ret .. makeCategoryLink('Pages with template parameter errors') end return ret end

local function formatDate(format, date) local success, date = pcall(lang.formatDate, lang, format, date) if success then return date end end

local function formatDateAndTime(date, time) date = formatDate('F j, Y', date) if not date then return nil end

if time then time = formatDate('g:i a', time) if not time then return nil end end

if time then time = string.format(', %s UTC', time) else time = '' end

return date .. time end

function p._main(args) -- Validate the input if not args.author or not args.dateposted or not args.description then return makeWikitextError(			"parameters author, dateposted and description " ..			"must be specified"		) elseif args.link and args.archive or not args.link and not args.archive then return makeWikitextError(			"either parameter link or archive must be specified"		) end -- This function simulates how arguments are expanded in wikitext. If the -- value is not present in the args table, the key is returned enclosed by -- three curly braces. local function expandArg(key) local val = args[key] if val then return val else return string.format('', key) end end

local ret = {}

-- Shortcut function for adding new strings local function add(val) ret[#ret + 1] = val end

-- Facebook icon add(' ') add(' ')

-- Start citation details. If a userpage was specified, we ignore the other -- citation details. if args.userpage then add(string.format( '%s on Facebook', args.userpage, args.description ))	else -- Link or archive link if args.link then add(string.format( '%s.', args.link, args.description ))		elseif args.archive then if archive:find('^http') then add(string.format('[%s %s].', args.archive, args.description)) else add(string.format( '%s.', args.archive, args.description ))			end end

-- Author add(' ') add(args.author)

-- Date and time posted do local date = formatDateAndTime(args.dateposted, args.timeposted) if date then add(string.format(' (%s).', date)) elseif not args.timeposted then return makeWikitextError(					"invalid date in parameter 'dateposted'"				) else return makeWikitextError(					"invalid date in parameters 'dateposted' or 'timeposted'"				) end end

-- Date and time accessed if args.dateaccessed then local date = formatDateAndTime(args.dateaccessed, args.timeaccessed) if date then add(string.format(' Retrieved on %s.', date)) elseif not args.timeaccessed then return makeWikitextError(					"invalid date in parameter 'dateaccessed'"				) else return makeWikitextError(					"invalid date in parameters 'dateaccessed' or 'timeaccessed'"				) end end

-- Quote if args.quote then add(string.format(' "%s"', args.quote)) end end

return table.concat(ret) end

function p.main(frame) local args = {} for k, v in pairs(frame:getParent.args) do		v = v:match('^%s*(.-)%s*$') -- trim whitespace if v ~= '' then args[k] = v		end end return p._main(args) end

return p

--