Module:Documentation

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

This module displays a green box containing documentation for templates, Lua modules, or other pages. The {{documentation}} template invokes it.

Normal usage

For most uses, you should use the {{documentation}} template; please see that template's page for its usage instructions and parameters.

Use in other modules

To use this module from another Lua module, first load it with require:

local documentation = require('Module:Documentation').main

Then you can simply call it using a table of arguments.

documentation{content = 'Some documentation', ['link box'] = 'My custom link box'}

Please refer to the template documentation for usage instructions and a list of parameters.

Porting to other wikis

The module has a configuration file at Module:Documentation/config which is intended to allow easy translation and porting to other wikis. Please see the code comments in the config page for instructions. If you have any questions, or you need a feature which is not currently implemented, please leave a message at Template talk:Documentation to get the attention of a developer.

The messages that need to be customized to display a documentation template/module at the top of module pages are MediaWiki:Scribunto-doc-page-show and MediaWiki:Scribunto-doc-page-does-not-exist.


--This module implements {{Documentation}}.
--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, '<div style="clear:both />')
        table.insert(corps, frame:preprocess('{{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('<nowiki>' .. table.concat(corps) .. '</nowiki>\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 = {'<div class="template-documentation"'}
 
    if args.color then
        table.insert(header, ' style="background:')
        table.insert(header, args.color .. '"')
    end
 
    table.insert(header, '><div style="margin-bottom:1ex; border-bottom:1px solid #aaa; padding-bottom:3px;">')
    table.insert(header, '[[File:Template-info.png|50px|alt=Template documentation|link=]]')
    table.insert(header, '<span style="font-weight:bold; font-size:125%">&nbsp;')
 
    if args.heading then table.insert(header, args.heading)
    else table.insert(header, 'Template documentation') end
    table.insert(header, '</span>')
 
    if not args.content then
        table.insert(header, '<span class="mw-editsection plainlinks">&#91;[')
        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, '&#93;&#32;&#91;[')
            table.insert(header, arg:fullUrl('action=edit') .. ' edit]')
            table.insert(header, '&#93;&#32;&#91;[')
            table.insert(header, arg:fullUrl('action=history') .. ' history]')
            table.insert(header, '&#93;&#32;&#91;[')
            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, '&#93;</span>')
    end
 
    table.insert(header, '</div>')
    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('<nowiki />'))
        table.insert(content, '\n')
        if args[1] and p.ifexist(args[1]) or p.ifexist(doc) then
            table.insert(content, frame:preprocess('{{' .. arg .. '}}'))
        end
    end
 
    table.insert(content, '\n')
    table.insert(content, frame:preprocess('<nowiki />'))
    table.insert(content, '<div style="clear:both" />\n')
    return table.concat(content)
end
 
function p.footer(page)
    local footer = {}
    local arg = mw.title.new(args[1] or doc)
 
    table.insert(footer, '</div><div class="template-documentation plainlinks" ')
    table.insert(footer, 'style="font-style:italic; margin:2px 0px 0px; padding: 0.35em 0.9em')
 
    if args.color then
        table.insert(footer, '; background:')
        table.insert(footer, args.color)
    end
    table.insert(footer, ';">\n')
 
    if args.content then
        if args["link box"] then
            table.insert(footer, args["link box"])
        else
            table.insert(footer, 'This documentation is directly included in this page.')
        end
    else
        if args[1] and p.ifexist(args[1]) or p.ifexist(doc) then
            table.insert(footer, 'The above [[Wikipedia:Template documentation|documentation]] is ')
            table.insert(footer, '[[Wikipedia:Transclusion|transcluded]] from [[')
            table.insert(footer, tostring(arg)) 
            table.insert(footer, ']]&nbsp;<span style="font-size:89%; font-style:normal;">([')
            table.insert(footer, arg:fullUrl('action=edit'))
            table.insert(footer, ' edit]&nbsp;|&nbsp;[')
            table.insert(footer, arg:fullUrl('action=history') .. ' history])</span>.<br />')
        end
        table.insert(footer, 'Editors can experiment in this templates ')
 
        local sandbox = arg.subjectNsText .. ":" .. arg.baseText .. "/sandbox"
        local argsandbox = mw.title.new(sandbox)
        if p.ifexist(sandbox) then
            table.insert(footer, '[[' .. sandbox .. '|sandbox]]')
            table.insert(footer, '&nbsp;<span style="font-size:89%; font-style:normal;">([')
            table.insert(footer, argsandbox:fullUrl('action=edit'))
            table.insert(footer, ' edit])</span>')
        else
            table.insert(footer, 'sandbox&nbsp;<span style="font-size:89%; font-style:normal;">([')
            table.insert(footer, argsandbox:fullUrl({["action"]="edit", ["preload"]="Template:Documentation/preload-sandbox"}))
            table.insert(footer, ' create])</span>')
        end
        table.insert(footer, ' and ')
 
        local test = arg.subjectNsText .. ":" .. arg.baseText .. "/testcases"
        local argtest = mw.title.new(test)
        if p.ifexist(test) then
            table.insert(footer, '[[' .. test .. '|testcases]]')
            table.insert(footer, '&nbsp;<span style="font-size:89%; font-style:normal;">([')
            table.insert(footer, argtest:fullUrl('action=edit'))
            table.insert(footer, ' edit])</span>')
        else
            table.insert(footer, 'testcases&nbsp;<span style="font-size:89%; font-style:normal;">([')
            table.insert(footer, argtest:fullUrl({["action"]="edit", ["preload"]="Template:Documentation/preload-testcases"}))
            table.insert(footer, ' create])</span>')
        end
 
        table.insert(footer, ' pages.<br />Please add categories to the ')
        table.insert(footer, '<span class="plainlinks">[')
 
        if args[1] and p.ifexist(args[1]) or p.ifexist(doc) then
            table.insert(footer, arg:fullUrl('action=edit'))
        else
            table.insert(footer, arg:fullUrl({["action"]="edit", ["preload"]="Template:Documentation/preload"}))
        end
 
        table.insert(footer, ' /doc] subpage.')
    end
    table.insert(footer, '</div>')
    return table.concat(footer)
end
 
return p