Module:Print ref

local p = {};

p.ref = function (frame) local intro = frame.args.intro or '' local work = frame.args.title or nil local chapterInput = frame.args.chapter or nil local issueInput = frame.args.issue or nil local pageStart = string.gsub(frame.args.page1, '[^%w]+', '\–') or nil local pageEnd = frame.args.page2 or nil local more = frame.args.more or '' local cite = frame.args.cite or nil local pageIssue -- handle blank entry if isempty(work) then return 'Title missing or incorrectly entered .' end -- issue is calculated with work name if not isempty(issueInput) then work = work .. issueInput end local printMatcher = require('Module:Print ref/Matcher') local printCode, issueNumber = printMatcher.matchCode(work) local printInfo = require('Module:Print ref/Info') local workType = printInfo.workType(printCode) local display = printInfo.display(printCode, issueNumber)

if display == 'Unknown printed work' then workFormat = "''" .. work .. "''"   elseif display == 'Invalid issue' then workFormat = 'Invalid issue ' else workFormat = display end if not isempty(pageEnd) and not isempty(pageStart) then pageIssue = ', pp. ' .. pageStart .. '\–' .. pageEnd elseif not pageStart:match('%d') and not isempty(pageStart) then pageIssue = ', pp. ' .. pageStart elseif not isempty(pageStart) then pageIssue = ', p. ' .. pageStart elseif workType == 'comic' and isempty(issueNumber) then pageIssue = '' elseif workType == 'gn' and isempty(pageStart) then pageIssue = '' else pageIssue = '' end

if workType == 'book' and chapterInput:match('%d') then chapter = ', Chapter ' .. chapterInput elseif workType == 'book' and not isempty(chapterInput) then chapter = ', "' .. chapterInput .. '"' else chapter = '' end

if isempty(cite) then cite_needed = '' else cite_needed = '\{\{cite\}\}' end return intro .. ' ' .. workFormat .. chapter .. pageIssue .. '.' .. cite_needed .. ' ' .. more end

p.anchor = function(frame) local intro = frame.args.intro or '' local work = frame.args[1] or frame.args.title local custom = frame.args.custom or '' local chapter = frame.args.chapter or '' local issue = frame.args.issue or '' local page = frame.args.page1 or '' local pageEnd = frame.args.page2 or '' local more = frame.args.more or '' local anchor local printMatcher = require('Module:Print ref/Matcher') local printCode, issueNum = printMatcher.matchCode(work)

local printInfo = require('Module:Print ref/Info') local workType = printInfo.workType(printCode, issueNum) local title = printInfo.title(printCode, issueNum) local display = printInfo.display(printCode, issueNum) local issueNumber = issueNum if display == 'Unknown printed work' then display = work end if issueNumber == nil then issueNumber = '' end

if workType == 'book' or workType == 'dnd' or custom then anchor = string.gsub(display .. chapter .. page .. pageEnd .. intro .. more, '[^%w]', '') elseif workType == 'gn' then anchor = string.gsub(display .. intro .. more, '[^%w]', '') elseif workType == 'comic' then anchor = string.gsub(display .. issueNumber .. intro .. more, '[^%w]', '') end return anchor end

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

return p