Module:Documentation

来自滚动的天空Wiki
文档图示 模块文档[查看] [编辑] [查看历史] [清除缓存]

本模块从维基百科的模块:Documentation简化改编,用于{{文档}}模板。

此外,对于Module:CGroup的子页面,本模块会调用模块:CGroupViewer模块以显示其转换规则数据内容。

上述文档内容嵌入自Module:Documentation/doc编辑 | 历史
编者可以在本模块的沙盒创建 | 镜像和测试样例创建页面进行实验。
请将模块自身所属的分类添加在文档中。本模块的子页面
-- 本模块是从维基百科的相关模块简化的。

local p = {}

function p._main(args, f)
  -- args是不含元表的表
  -- f是框架,应该作为第二个参数传入
  f = f or mw.getCurrentFrame()
  args = args or {}
  
  local content = args.content
  -- 关于标题对象的处理
  local title=args.page and mw.title.new(args.page)
              or mw.title.getCurrentTitle()
  local T=tostring(title)
  
  local doctitle=args[1] and mw.title.new(args[1])
               or mw.title.new(T.."/doc")
  local D=tostring(doctitle)
  local sandboxtitle=mw.title.new(T.."/sandbox")
  local S=tostring(sandboxtitle)
  local testcasestitle=mw.title.new(T.."/testcases")
  local E=tostring(testcasestitle)
  local document, doc_exists
  local fullurl = require'模块:fullurl'._fullurl
  local pageName = require'模块:page name'.pageName(title, tonumber(args[variant]))
  
  if content then
    document = content
  else
    --先获取文档内容
    local doc_exists = doctitle.exists
    local state -- boolean
    if not doc_exists then
      document=('本%s还没有文档页面,你可以创建它。')
          :format(pageName)
    else
      state, document = pcall(function() 
        return f:expandTemplate{title=doctitle,args={isdoc=1}} 
      end)
      if not state then
        document='尝试调用文档内容时发生未知错误:'..document
      end
    end
  end
  
  local autogen = {}
  if title and title.namespace==828 then
    do
  		-- 尝试获取页面内容并识别重定向。
  		local successes, page_content = pcall(title.getContent, title)
  		if successes then
  			local target = nil  -- 模块可能重定向至的页面名称。
  			local match = page_content:match '%s*return%s+require%s*(.+)%;*%s*'
  			if match then
  				match = match:match '^%s*%((.-)%)%s*$' or match
  				target = match:match '^%s*"(.-)"%s*$' or match:match "^%s*'(.-)'%s*$"
  					or match:match "^%s*%[%[(.-)%]%]%s$"
  			end
  			if target then
  				autogen[#autogen + 1] = "本模块是一个重定向模块,重定向至[[" .. target .. "]]。"
  			end
  		else
  			mw.log("[Module:Documentation] 尝试获取页面内容时出现错误:" .. page_content)
  		end
  	end
    if title.isSubpage and title.rootText == 'CGroup' then
      local CGroupViewer = require('Module:CGroupViewer')
    	autogen[#autogen + 1] = [=[
<hr>

以下内容根据模块内容通过[[Module:CGroupViewer]]模块生成,仅供参考。

]=] .. CGroupViewer.main(f:newChild({args = {title.subpageText}}))
    end
  end

  
  local contents = {
    photo="[[File:Test Template Info-Icon - Version (2).svg|50px|link=|alt=文档图示]] "..pageName --不同名字空间显示不同名字
          .."-{zh-hans:文档;zh-hant:文件}-",
    edit=string.format("&#91;[[Special:EditPage/%s|编辑]]&#93;",D), -- 编辑按钮
    hist=string.format("&#91;[[Special:PageHistory/%s|查看历史]]&#93;",D), -- 查看历史按钮
    purge=string.format("&#91;[[Special:Purge/%s|清除-{zh-hans:缓存;zh-hant:快取}-]]&#93;",T), -- 清除缓存按钮
    set=string.format('您可以%s文档以让用户更好地理解本%s的用途。', fullurl{title=D,action='edit',text='创建',redlink='1'}, pageName),
    context=('上述文档内容由%s本身直接提供,请查看页面源代码。'):format(pageName),
    invtext=string.format('编者可以在本%s的沙盒<small>(%s)</small>和测试样例<small>(%s)</small>页面进行实验。<br>',pageName,
          sandboxtitle.exists and string.format("[[Special:EditPage/%s|编辑]]",S) or fullurl{title=S,action='edit',redlink='1',text='创建'} .. ' &#124; ' .. fullurl{title=S,action='edit',preload=T,text='镜像'}, --“沙盒”后括号内的文字
          testcasestitle.exists and string.format("[[Special:EditPage/%s|编辑]]",E) or fullurl{title=E,action='edit',redlink='1',text='创建'}
        )
  }
  
  local root=mw.html.create()
  root
    :tag'div'
      :addClass'template-documentation iezoomfix'
      :newline()
      :tag"div"
        :addClass 'template-documentation-header'
        :tag"span" --“模板文档”以及前的图片
          :css{
            ['font-size']='150%',
          }
          :wikitext(contents.photo)
        :done()
        :tag"span" --其后的编辑等按钮
          :addClass"mw-editsection-like"
          :wikitext(content and 
            contents.purge
            -- 使用content参数时的链接列表
            or 
            doctitle.exists and table.concat({
            string.format("&#91;[[%s|-{zh-cn:查看;zh-tw:檢視}-]]&#93;",D), -- 查看按钮
            contents.edit,
            contents.hist,
            contents.purge
          }, " "
          ) --有文档页面时的链接列表
          or string.format(
            "&#91;%s]",
            fullurl{title=D,action='edit',redlink='1',text='创建'}
          ) --文档页面不存在时的链接列表
          ) -- “:wikitext”的参数结束
        :done()
      :done()
      :newline()
      :tag 'div'
        :addClass 'template-documentation-body hantsect'
        :wikitext(string.sub(document,1,1)~='\n' and '\n' or nil)
        :wikitext(document)
        :wikitext(table.concat(autogen)) -- 自动生成的部分,可能为nil
        :wikitext(string.sub(document,-1,-1)~='\n' and '\n' or nil)
        :newline()
      :done()
      :tag"div"
        :addClass"template-documentation-foot"
        :wikitext(
          content and contents.context
          or doctitle.exists and string.format(
            '上述文档内容嵌入自[[%s]]。<small style="font-style:normal">([[Special:EditPage/%s|编辑]] &#124; [[Special:PageHistory/%s|历史]])</small>',
          D,
          D,
          D)
          or contents.set
        )
        :wikitext(#autogen > 0 and '部分内容为自动生成。' or nil)
        :wikitext '<br>'
        :wikitext(contents.invtext)
        :wikitext(('请将%s自身所属的分类添加在文档中。[[Special:前缀索引/%s/|本%s的子页面]]。'):format(pageName, T, pageName))
  return tostring(root)
end

function p.main(frame)
  local args={}
  for k,v in pairs(frame:getParent().args) do
    local v = mw.text.trim(v)
    args[k] = v~="" and v or nil -- 筛选掉空白参数
  end
  return p._main(args, frame)
end

return p