Module:Documentation

From FIThydrowiki
Revision as of 10:55, 16 August 2013 by what>Hlm Z.
Jump to navigation Jump to search

--This module implements Script error: The function "main" does not exist.. --TODO: Implement a class 'ConcatBuilder' to reduce redundancy --'table.insert(<MyTable>, <MyString>)' and 'table.concat(<MyTable>)'

local p = {}

function p.corps(frame)

   args = frame:getParent().args
   local page = mw.title.getCurrentTitle()
   doc = p.docname(page)
   local corps = {}

   if page.subpageText == 'sandbox' then

table.insert(corps, '

')

       table.insert(corps, frame:preprocess('Template:Template sandbox notice'))
   end

   table.insert(corps, p.header(page))
   table.insert(corps, p.content(frame, page))
   table.insert(corps, p.footer(page))

   if args.raw then
       return frame:preprocess('' .. table.concat(corps) .. '\n:' .. os.clock())
   end

   return table.concat(corps)

end

function p.docname(page)

   if not page.isSubpage then return page.subjectNsText .. ":" .. page.text .. "/doc" end

   if page.subpageText == 'doc'
   or page.subpageText == 'sandbox'
   or page.subpageText == 'testcases' then
       return page.subjectNsText .. ":" .. page.baseText .. "/doc"
   else
       return page.subjectNsText .. ":" .. page.text .. "/doc"
   end

end

function p.ifexist(page)

   if not page then return false end
   if mw.title.new(page).exists then return true end
   return false

end

function p.header(page)

local header = {'

')
   table.insert(header, '50px')
   table.insert(header, ' ')

   if args.heading then table.insert(header, args.heading)
   else table.insert(header, 'Template documentation') end
   table.insert(header, '')

   if not args.content then
       table.insert(header, '[[')
       local arg = mw.title.new(args[1] or doc)

       if args[1] and p.ifexist(args[1]) or p.ifexist(doc) then
           table.insert(header, arg:fullUrl('action=view') .. ' view]')
           table.insert(header, '] [[')
           table.insert(header, arg:fullUrl('action=edit') .. ' edit]')
           table.insert(header, '] [[')
           table.insert(header, arg:fullUrl('action=history') .. ' history]')
           table.insert(header, '] [[')
           table.insert(header, page:fullUrl('action=purge') .. ' purge]')
       else
           table.insert(header, arg:fullUrl({["action"]="edit", ["preload"]="Template:Documentation/preload"}))
           table.insert(header, ' create]')
       end
       table.insert(header, ']')
   end

table.insert(header, '
')
   return table.concat(header)

end

function p.content(frame, page)

   local content = {}
   local arg = args[1] or doc
   if args.content then
       table.insert(content, '\n')
       table.insert(content, args.content)
   else
       table.insert(content, frame:preprocess(''))
       table.insert(content, '\n')
       if args[1] and p.ifexist(args[1]) or p.ifexist(doc) then
           table.insert(content, frame:preprocess('Template:' .. arg .. ''))
       end
   end

   table.insert(content, '\n')
   table.insert(content, frame:preprocess(''))
table.insert(content, '
\n')
   return table.concat(content)

end

function p.footer(page)

   local footer = {}
   local arg = mw.title.new(args[1] or doc)

table.insert(footer, '

')

   return table.concat(footer)

end

return p