Modul:Matematik för årskurs 7-9/Numrering

Från Wikibooks

Documentation for Modul:Matematik för årskurs 7-9/Numrering

This module accepts a piece of text or the name of a page. It acts on angle-bracket tags in the text to simulate an extension of the html/wiki markup set vaguely per WP:Lua requests, the <inc> tag is the first example, replaced with a number that increases each count. You can set or reset the count and rate at any time by <inc i j> where i=the number of that tag and j is the difference to succeeding tags.

The function markup replaces <inc> for an increasing number. The first <inc> translates to 1, the second to 2 and so on. Should be extended to handle multi variable numbering for multiple sets of <inc> tags or <inc> tags of different depth. This could be used to have different numbering for exercises and examples as well as a higher ranking numbering for chapters. But as this won't be used until this book is near complete and a "real" book is to be put together with multiple pages in one this will have to wait. Might be worth doing in two steps depending on how the multiple page book is to be put together.

Warning: does not handle <nowiki> tags at all.


 ---- This module accepts a piece of text or the name of a page
 ---- It acts on angle-bracket tags in the text to simulate an extension of the html/wiki markup set
 ---- Vaguely per [[WP:Lua requests]], the <inc> tag is the first example, replaced with a number 
 ---- that increases each count.  You can set or reset the count and rate at any time by <inc i j>
 ---- where i=the number of that tag and j is the difference to succeeding tags.
 
local p={}

--[[
This function removes <noinclude> tags and their content.
Should be extended to also handle <nowiki> tags
]]
function extrude(content)
    content, n = mw.ustring.gsub(content,"<noinclude>.-</noinclude>","") -- remove noincludes and their content
    content, a = mw.ustring.gsub(content,"<noinclude>.*","") -- removes noinclude without an end
    return content
end

--[[
This function replaces <inc> for an increasing number. The first <inc> translates to 1, the second to 2 and so on.
Should be extended to handle multi variable numbering for multiple sets of <inc> tags or <inc> tags of different depth.
This could be used to have different numbering for exercises and examples as well as a higher ranking numbering for chapters.
But as this won't be used until this book is near complete and a "real" book is to be put together with multiple pages in one this will have to wait.
Might be worth doing in two steps depending on how the multiple page book is to be put together.
]]
function p.markup(frame)
   local args = frame.args
   local parent = frame.getParent(frame) or {}
   pargs = parent.args or {}
   local page = args[1] or pargs[1]
   if page=="" then
       title = mw.title.getCurrentTitle()
       assert(title.getContent, "Bug: [[Module:TextMarkup]] failed to get content of the current page")
       page = title.fullText
   else title = mw.title.new(page)
       assert(title.getContent, "Error: [[Module:TextMarkup]] failed to get content of "..page)
   end -- one way or another, we have a working getContent method
   text=title.getContent(title)
   if not text then text = "" end -- now we certanly have text
   local counter = 0 -- set counter to zero before encountering an <inc> tag
   local increment = 1 -- default increment
   text = extrude(text) -- Removes <noinclude>
   local prowl = mw.ustring.gmatch(text, "<inc>") -- iterator function to get all the tags
   local output = text -- copy to do substitutions on
   repeat
       local inccontent = prowl()
       if not inccontent then break end -- loop exit
       counter = counter + increment
       output = mw.ustring.gsub(output, "<inc>", tostring(counter)..". ", 1) -- change one instance according to the one instance found
   until false
   output = frame:preprocess(output) -- Expands templates...
   return output
end

--[=[
--[[
Backup
]]
function p.markup(frame)
   local args=frame.args
   local parent=frame.getParent(frame) or {}
   pargs=parent.args or {}
   local text=args.text or pargs.text
   local page=args.page or pargs.page
   if (not text and not page) then
       if not args[1] then return "" end
       if mw.ustring.match(args[1],"<inc>") then text=args[1] else page=args[1] end
   end
   if not text then
       page=mw.ustring.match(page,"%s*(.*%S)%s*") or ""
       if page=="" then
           title=mw.title.getCurrentTitle()
           assert(title.getContent, "Bug: [[Module:TextMarkup]] failed to get content of the current page")
           page=title.fullText
       else title=mw.title.new(page)
           assert(title.getContent, "Error: [[Module:TextMarkup]] failed to get content of "..page)
       end -- one way or another, we have a working getContent method
       text=title.getContent(title)
   end -- (not text) -- now we have text
   if not text then text = "" end
   local counter=0 -- set counter to zero before encountering an <inc> tag
   local increment=1 -- default increment
   text = extrude(text) -- Removes <noinclude>
   local prowl=mw.ustring.gmatch(text,"<inc([^>]*)>") -- iterator function to get all the tags
   local output=text -- copy to do substitutions on
   repeat
       local inccontent=prowl()
       if not inccontent then break end -- loop exit
       local n1,n2 = mw.ustring.match(text,"%D-(%d+)%D-(%d+)")
       if n2 then increment=tonumber(n2) end
       counter=counter+increment
       if n1 then counter=tonumber(n1) end
       output=mw.ustring.gsub(output,"<inc([^>]*)>",tostring(counter)..". ",1) -- change one instance according to the one instance found
   until false
   output = frame:preprocess(output) -- Expands templates...
   return output
end
]=]
return p