Module:Chapterpage

-- Assist in building header template and block in main namespace --

local p = {};

--Return chapter as number p.chapter = function(frame) mw.log(frame.args.chapter) --local c = string.sub(frame.args.chapter, 8) c = frame.args.chapter c = tonumber(c) return c end

--A generalized function to loop over a table function p.list_iter(t) local i = 0 local n = table.getn(t) return function i = i + 1 if i <= n then return t[i] end end end

p.page = function(frame, chapter) local tdFunc = require 'Module:Sandbox/Tabular data' --Page in commons.wikimedia.org/wiki/Data: that has tabular data local tdPath = frame.args.data local page = tdFunc._cell{output_row=chapter, output_column='page', tdPath} page = tonumber(page) return page end

--Return first page in page range as a number --Use as argument to "from" attribute in block p.from = function (frame) local pageoffset = tonumber(frame.args.pageoffset) local chapter = tonumber(frame.args.chapter) page = p.page(frame, chapter) + pageoffset return page end

--Return last page in page range as a number --Use as argument to "to" attribute in block p.to = function (frame) local pageoffset = tonumber(frame.args.pageoffset) local chapter = tonumber(frame.args.chapter) page = p.page(frame, chapter+1) + pageoffset - 1 return page end

-- Take chapter number and produce wikilink p.chapterlink = function (frame, add) c = tonumber(frame.args.chapter)+add wikilink = [=[../Chapter chapternro/]=] wikilink = string.gsub(wikilink, 'chapternro', c)   return wikilink end

--Return wikilink to previous chapter --Use as argument to "previous" parameter in header template p.prev = function (frame) return p.chapterlink(frame, -1) end

--Return wikilink to next chapter --Use as argument to "next" parameter in header template p.next = function (frame) return p.chapterlink(frame, 1) end

--Produce header template with filled parameters p.header = function(frame) local chapter = tonumber(frame.args.chapter) local prev = chapter - 1 local nxt = chapter + 1 return frame:expandTemplate{title='header', args={ title   = frame.args.title, author  = frame.args.author, section = 'Chapter ' .. chapter, previous = '../Chapter ' .. prev .. '/', next    = '../Chapter ' .. nxt .. '/', notes   = frame.args.notes }} end

--Produce header template with filled parameters p.headerOld = function (frame) local chapter = tonumber(frame.args.chapter) local prev = chapter - 1 local next = chapter + 1

local output = [=[ ]=]

output = string.gsub(output, "prevchapter", prev) output = string.gsub(output, "nextchapter", next) output = string.gsub(output, "frompage", from) output = string.gsub(output, "topage", to) output = tostring(output) output = frame:preprocess(output) return output end

--Return block using string manipulation functions p.pagesOld = function (frame) local chapter = tonumber(frame.args.chapter) local from = p.from(frame, chapter) local to = p.to(frame, chapter) local output = [=[  ]=]	output = string.gsub(output, "frompage", from) output = string.gsub(output, "topage", to) output = string.gsub(output, "indexPage", frame.args.index) output = frame:preprocess(output) return output end

--Return block using dom manipulation function p.pages = function(frame) local chapter = tonumber(frame.args.chapter) local from = p.from(frame, chapter) local to = p.to(frame, chapter) local pgs = mw.html.create('pages', {selfClosing=true}) pgs = pgs :attr('index', frame.args.index) :attr('from', from) :attr('to', to)

pgs = tostring(pgs) pgs = frame:preprocess(pgs) return pgs end

p.transclude = function(frame) return p.header(frame) .. p.pages(frame) end

return p