Difference between revisions of "Module:Documentation"

From Minetest Wiki
Jump to navigation Jump to search
m
m
(6 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
local HtmlBuilder = require('Module:HtmlBuilder')
 
local HtmlBuilder = require('Module:HtmlBuilder')
 +
local getLanguageCode = require('Module:Languages').getLanguageCode
 
local makeInvokeFunc = require('Module:Arguments').makeInvokeFunc
 
local makeInvokeFunc = require('Module:Arguments').makeInvokeFunc
  
Line 8: Line 9:
 
     local contentParam = args['content']
 
     local contentParam = args['content']
 
     local linkBoxParam = args['link box']
 
     local linkBoxParam = args['link box']
     local currentTitle, docPageName, docPageTitle
+
     local currentTitle = mw.title.getCurrentTitle()
    if not contentParam then
+
    local docPageName = args[1]
        currentTitle = mw.title.getCurrentTitle()
+
    if not docPageName then
        docPageName = args[1] or ((args['page'] or currentTitle.fullText) .. '/doc')
+
        local currentLangCode = getLanguageCode(currentTitle)
        docPageTitle = mw.title.new(docPageName) -- Expensive
+
        local pageName = args['page']
 +
        if pageName then
 +
            if currentLangCode == 'en' then
 +
                docPageName = pageName .. '/doc'
 +
            else
 +
                docPageName = pageName .. '/doc/' .. currentLangCode
 +
            end
 +
        else
 +
            if currentLangCode == 'en' then
 +
                docPageName = currentTitle.fullText .. '/doc'
 +
            else
 +
                local namespace = currentTitle.nsText
 +
                local basePageName = currentTitle.baseText
 +
                docPageName = namespace .. ':' .. basePageName  .. '/doc/' .. currentLangCode
 +
            end
 +
        end
 
     end
 
     end
 +
    local docPageTitle = mw.title.new(docPageName)
  
 
     local root = HtmlBuilder.create()
 
     local root = HtmlBuilder.create()
Line 30: Line 47:
 
             headingSpan:wikitext(headingParam)
 
             headingSpan:wikitext(headingParam)
 
         else
 
         else
             headingSpan:wikitext('[[File:Documentation icon.png|50px|link=]] Documentation')
+
             headingSpan:wikitext('[[File:Documentation icon.png|50px|link=]] ' .. args['default-heading'])
 
         end
 
         end
  
Line 48: Line 65:
 
             if docPageTitle.exists then
 
             if docPageTitle.exists then
 
                 format = '[%s] [%s] [%s] [%s]'
 
                 format = '[%s] [%s] [%s] [%s]'
                 local viewLink = '[[:' .. docPageName .. '|view]]'
+
                 local viewLink = '[[:' .. docPageName .. '|' .. args['view-link-display'] .. ']]'
                 local editLink = '[' .. docPageTitle:fullUrl{action = 'edit'} .. ' edit]'
+
                 local editLink = '[' .. docPageTitle:fullUrl{action = 'edit'} .. ' ' .. args['edit-link-display'] .. ']'
                 local historyLink = '[' .. docPageTitle:fullUrl{action = 'history'} .. ' history]'
+
                 local historyLink = '[' .. docPageTitle:fullUrl{action = 'history'} .. ' ' .. args['history-link-display'] .. ']'
                 local purgeLink = '[' .. currentTitle:fullUrl{action = 'purge'} .. ' purge]'
+
                 local purgeLink = '[' .. currentTitle:fullUrl{action = 'purge'} .. ' ' .. args['purge-link-display'] .. ']'
 
                 actionsSpan:wikitext(mw.ustring.format(escapeBrackets('[%s] [%s] [%s] [%s]'), viewLink, editLink, historyLink, purgeLink))
 
                 actionsSpan:wikitext(mw.ustring.format(escapeBrackets('[%s] [%s] [%s] [%s]'), viewLink, editLink, historyLink, purgeLink))
 
             else
 
             else
                 local createLink = '[' .. docPageTitle:fullUrl{action = 'edit'} .. ' create]'
+
                 local createLink = '[' .. docPageTitle:fullUrl{action = 'edit'} .. ' ' .. args['create-link-display'] .. ']'
 
                 actionsSpan:wikitext(mw.ustring.format(escapeBrackets('[%s]'), createLink))
 
                 actionsSpan:wikitext(mw.ustring.format(escapeBrackets('[%s]'), createLink))
 
             end
 
             end
Line 84: Line 101:
 
             endBoxNode:wikitext(linkBoxParam)
 
             endBoxNode:wikitext(linkBoxParam)
 
         else
 
         else
             endBoxNode:wikitext('The above [[wikipedia:Wikipedia:Template documentation|documentation]] is [[wikipedia:Transclusion|transcluded]] from [[:', docPageName, '|', docPageName, ']]. ')
+
             endBoxNode:wikitext(mw.ustring.format(args['transcluded-from-blurb'], '[[:' .. docPageName .. '|' .. docPageName .. ']]'), ' ')
  
 
             local editUrl = docPageTitle:fullUrl{action = 'edit'}
 
             local editUrl = docPageTitle:fullUrl{action = 'edit'}
Line 90: Line 107:
 
             local actionsNode = endBoxNode:tag('small')
 
             local actionsNode = endBoxNode:tag('small')
 
             actionsNode:cssText('font-style: normal')
 
             actionsNode:cssText('font-style: normal')
             actionsNode:wikitext('([', editUrl, ' edit] | [', historyUrl, ' history])')
+
             actionsNode:wikitext('([', editUrl, ' ' .. args['edit-link-display'] .. '] | [', historyUrl, ' ' .. args['history-link-display'] .. '])')
 
         end
 
         end
 
     end
 
     end

Revision as of 06:10, 12 May 2017

local p = {}

local HtmlBuilder = require('Module:HtmlBuilder') local getLanguageCode = require('Module:Languages').getLanguageCode local makeInvokeFunc = require('Module:Arguments').makeInvokeFunc

function p._main(frame, args)

   local headingParam = args['heading']
   local contentParam = args['content']
   local linkBoxParam = args['link box']
   local currentTitle = mw.title.getCurrentTitle()
   local docPageName = args[1]
   if not docPageName then
       local currentLangCode = getLanguageCode(currentTitle)
       local pageName = args['page']
       if pageName then
           if currentLangCode == 'en' then
               docPageName = pageName .. '/doc'
           else
               docPageName = pageName .. '/doc/' .. currentLangCode
           end
       else
           if currentLangCode == 'en' then
               docPageName = currentTitle.fullText .. '/doc'
           else
               local namespace = currentTitle.nsText
               local basePageName = currentTitle.baseText
               docPageName = namespace .. ':' .. basePageName  .. '/doc/' .. currentLangCode
           end
       end
   end
   local docPageTitle = mw.title.new(docPageName)
   local root = HtmlBuilder.create()
   local startBox = root:tag('div')
   startBox:cssText('clear: both; margin: 1em 0 0 0; border: 1px solid #aaa; background-color: #ecfcf4; padding: 12px')
   -- Add heading if non-empty
   if headingParam ~=  then
       local headingDiv = startBox:tag('div')
       headingDiv:cssText('padding-bottom: 3px; border-bottom: 1px solid #aaa; margin-bottom: 1ex')
       local headingSpan = headingDiv:tag('span')
       headingSpan:cssText('font-weight: bold; font-size: 125%')
       if headingParam then
           headingSpan:wikitext(headingParam)
       else
           headingSpan:wikitext('Documentation icon.png ' .. args['default-heading'])
       end
       -- Add action links if doc is not on template page
       if not contentParam then
           local actionsSpan = headingDiv:tag('span')
           actionsSpan:addClass('plainlinks')
           actionsSpan:cssText('font-size: small; font-weight: 400; margin-left: 1em; vertical-align: baseline; line-height: 1em; display: inline-block')
           local function escapeBrackets(s)
               -- Replace brackets with HTML entities
               s = s:gsub('%[', '[')
               s = s:gsub('%]', ']')
               return s
           end
           if docPageTitle.exists then
               format = '[%s] [%s] [%s] [%s]'
               local viewLink = '' .. args['view-link-display'] .. ''
               local editLink = '[' .. docPageTitle:fullUrl{action = 'edit'} .. ' ' .. args['edit-link-display'] .. ']'
               local historyLink = '[' .. docPageTitle:fullUrl{action = 'history'} .. ' ' .. args['history-link-display'] .. ']'
               local purgeLink = '[' .. currentTitle:fullUrl{action = 'purge'} .. ' ' .. args['purge-link-display'] .. ']'
               actionsSpan:wikitext(mw.ustring.format(escapeBrackets('[%s] [%s] [%s] [%s]'), viewLink, editLink, historyLink, purgeLink))
           else
               local createLink = '[' .. docPageTitle:fullUrl{action = 'edit'} .. ' ' .. args['create-link-display'] .. ']'
               actionsSpan:wikitext(mw.ustring.format(escapeBrackets('[%s]'), createLink))
           end
       end
   end
   -- Transclude doc contents
   local content = contentParam
   if not content and docPageTitle.exists then
       content = frame:expandTemplate{title = ':' .. docPageName}
   end
   if content then
       -- Add line breaks so headings at start and end are interpreted correctly
       startBox:wikitext('\n' .. content .. '\n')
   end
   -- Prevent floating items from sticking out of doc box
   local clear = startBox:tag('div')
   clear:css('clear', 'both')
   if not contentParam and linkBoxParam ~= 'off' and docPageTitle.exists then
       local endBox = root:tag('div')
       endBox:addClass('plainlinks')
       endBox:cssText('clear: both; width: 100%; border: 1px solid #aaa; margin: .2em 0; background-color: #ecfcf4')
       local endBoxNode = endBox:tag('div')
       endBoxNode:cssText('border: none; width: 100%; padding: .25em .9em; font-style: italic')
       if linkBoxParam then
           endBoxNode:wikitext(linkBoxParam)
       else
           endBoxNode:wikitext(mw.ustring.format(args['transcluded-from-blurb'], '' .. docPageName .. ''), ' ')
           local editUrl = docPageTitle:fullUrl{action = 'edit'}
           local historyUrl = docPageTitle:fullUrl{action = 'history'}
           local actionsNode = endBoxNode:tag('small')
           actionsNode:cssText('font-style: normal')
           actionsNode:wikitext('([', editUrl, ' ' .. args['edit-link-display'] .. '] | [', historyUrl, ' ' .. args['history-link-display'] .. '])')
       end
   end
   return tostring(root)

end

p.main = makeInvokeFunc(p._main, {passFrameParam = true, inherited = true})

return p