Module:Print ref/Info

-- These functions require an exact match to printCode (and issueNumber). Matcher will need to handle variations -- italics display by default for titles and linked titles, hence noItalics -- printEntry and issueEntry cannot be accessed outside of this module

local p = {}

p.work = function(frame) local printWork = frame.args[1] local noItalics = frame.args['noitalics'] or nil local noLink = frame.args['nolink'] or nil local display = frame.args['display'] or nil local printDisplay = frame.args['printdisplay'] or nil local issueDisplay = frame.args['issuedisplay'] or nil local title = frame.args['title'] or nil local pagename = frame.args['pagename'] or nil local workType = frame.args['worktype'] or nil local issueNum = frame.args['issuenumber'] or nil local issuePage = frame.args['issuepage'] or nil local issueTitle = frame.args['issuetitle'] or nil printMatcher = require('Module:Print ref/Matcher') printCode, issueNumber = printMatcher.matchCode(printWork) if not isempty(display) then -- pick issue vs print display based on issueNumber if not isempty(issueNumber) then data = p.issueDisplay(printCode, issueNumber, noLink) else data = p.printDisplay(printCode, noLink) end elseif not isempty(printDisplay) then data = p.printDisplay(printCode, noLink) elseif not isempty(issueDisplay) then data = p.issueDisplay(printCode, issueNumber, noLink) elseif not isempty(title) then data = p.title(printCode, noItalics) elseif not isempty(pagename) then data = p.pagename(printCode) elseif not isempty(workType) then data = p.workType(printCode) elseif not isempty(issueNum) then data = issueNumber elseif not isempty(issuePage) then data = p.issuePage(printCode, issueNumber) elseif not isempty(issueTitle) then data = p.issueTitle(printCode, issueNumber) else data = printCode end return data end

-- retrieves the matching top-level entry in Print ref/Array printEntry = function(printCode) local printEntry local pra = mw.loadData('Module:Print ref/Array') if not isempty(pra[printCode]) then printEntry = pra[printCode] else printEntry = { ['title'] = 'Unknown printed work', }   end return printEntry end

-- the issue-level entry in Print ref/Array issueEntry = function(printCode, issueNumber) local issueEntry local printEntry = printEntry(printCode) if not isempty(printEntry['issues']) then issueEntry = printEntry['issues'][issueNumber] end if isempty(issueEntry) then issueEntry = { ['page'] = 'Invalid issue', }   end return issueEntry end

p.title = function(printCode, noItalics) if noItalics ~= nil and noItalics ~= false then noItalics = true else noItalics = false end local printEntry = printEntry(printCode) local title = printEntry['title'] if noItalics == false then title = "''" .. title .. "''"   end return title end

p.pagename = function(printCode) local pagename local printEntry = printEntry(printCode) if printEntry['pagename'] then pagename = printEntry['pagename'] else pagename = printEntry['title'] end return pagename end

p.workType = function(printCode) local printEntry = printEntry(printCode) local workType = printEntry['workType'] return workType end

-- linked and italicized output for a printed work p.printDisplay = function(printCode, noLink) if not isempty(noLink) then noLink = true end local printDisplay local pagename = p.pagename(printCode) local workType = p.workType(printCode) local title = p.title(printCode, '1') if pagename == "Unknown printed work" then printDisplay = pagename return printDisplay elseif workType == 'dnd' or noLink == true then printDisplay = p.title(printCode) return printDisplay elseif pagename ~= title then printDisplay = pagename .. '|' .. title else printDisplay = pagename end printDisplay = "''" ..  .. printDisplay ..  .."''" return printDisplay end

p.issuePage = function(printCode, issueNumber) local issuePage local pagename = p.pagename(printCode) local issueEntry = issueEntry(printCode, issueNumber) if not isempty(issueEntry['page']) then issuePage = issueEntry['page'] else issuePage = pagename .. ' ' .. issueNumber end return issuePage end

p.issueTitle = function(printCode, issueNumber) local issueEntry = issueEntry(printCode, issueNumber) local title = p.title(printCode) local issueTitle if not isempty(issueEntry['displayTitle']) then issueTitle = issueEntry['displayTitle'] else issueTitle = title .. ' #' .. issueNumber end return issueTitle end

-- linked and italicized output for a comic by issue number p.issueDisplay = function(printCode, issueNumber, noLink) if not isempty(noLink) then noLink = true end local issueDisplay local issuePage = p.issuePage(printCode, issueNumber) local issueTitle = p.issueTitle(printCode, issueNumber) if issuePage == 'Invalid issue' then issueDisplay = issuePage elseif noLink == true then issueDisplay = issueTitle else issueDisplay =  .. issueTitle ..  end return issueDisplay end

--function to determine if a string is empty function isempty(s) return s == nil or s == '' end

return p