Module:DefinitionCategory

From ProofWiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:DefinitionCategory/doc

local p = {}

local priv = {}

--Private functions
function priv.isNull( arg )
  return ( arg == nil or arg == "" )
end

function priv.defPage( def, cat )
  local defPage
  
  if priv.isNull( def ) then
    defPage = "Definition:" .. cat
  elseif ( string.match( def, ":" ) == nil ) then
    defPage = "Definition:" .. def
  else
    defPage = def
  end
  
  return defPage
end

function priv.defDisp( def, disp, cat )
  local defDisp
  
  if not priv.isNull( disp ) then
    defDisp = disp
  else
    defDisp = cat
  end
  
  return defDisp
end

function priv.defCat( cat, sortkey )
  return "[[Category:Definitions/" .. cat .."|" .. sortkey .. "]]"
end

function priv.initVars( frame )
  local vars = {}
  
  local fullTitle = mw.title.getCurrentTitle().text
  
  vars.category   = string.gsub( fullTitle, '^[^/]*/' , "" )
  vars.tpl_args   = frame:getParent().args
  --Support passing nocat/notrans at the invoke level as well (for DefinitionCategoryNocat/Notrans)
  vars.tpl_args.nocat   = vars.tpl_args.nocat or frame.args.nocat
  vars.tpl_args.notrans = vars.tpl_args.notrans or frame.args.notrans
  
  return vars
end

--Public functions (that is, main entry points)
function p.main( frame )
  local vars = priv.initVars( frame )
  local tpl_args = vars.tpl_args
  local category = vars.category
  
  --[=[ defPage: Definition page link.
    def contains a ":"   "<def>"
    def contains no ":"  "Definition:<def>"
    Default:             "Definition:<category name>" 
  ]=]
  local defPage = priv.defPage( tpl_args.def, category )
  
  --[=[ defDisp: Display part of definition page link
    disp exists:  <disp>
    def exists:   part of <def> after ":", if any
    Default:      <category>
  ]=]
  local defDisp = priv.defDisp( tpl_args.def, tpl_args.disp, category )
  
  --[=[ defLinkWikitext: Display of definition page link
  ]=]
  local defLinkWikitext = "[[" .. defPage .. "|" .. defDisp .. "]]"
  
  --[=[ contextText: Display of context addendum "in the context of ..."
    contextdisp exists: "[[Definition:<context>|<contextdisp>]]"
    context exists:     "[[Definition:<context>|<context>]]"
    Default:            No addendum
  ]=]
  local contextText
  if priv.isNull( tpl_args.context ) then
    contextText = ""
  else
    contextText = " in the context of [[Definition:" .. tpl_args.context .. "|"
    if priv.isNull( tpl_args.contextdisp ) then
      contextText = contextText .. tpl_args.context .. "]]"
    else
      contextText = contextText .. tpl_args.contextdisp .. "]]"
    end
  end
  
  --[=[ resultCatText: Display of link to associated results category
    nocat exists: No display
    Default:      {{ProofsLink|<category>}}
  ]=]
  local resultCatText = ""
  if priv.isNull( tpl_args.nocat ) then
    resultCatText = frame:expandTemplate{ title = 'ProofsLink', args = { category } }
  end
  
  
  --[=[ transcludeText: Transclusion, if applicable, of the page linked to with <defPage>
    notrans exists: No display
    Default:        {{<defPage>}}
  ]=]
  local transcludeText = ""
  if priv.isNull( tpl_args.notrans ) then
    --Lua pcall construct to present graceful message when the to-be-transcluded page does not exist
    local exists, val = pcall( frame.expandTemplate, frame, { title = defPage } )
    if exists then
      transcludeText = "\n" .. val
    else
      transcludeText = "\nTransclusion of [[" .. defPage .. "]] failed."
    end
  end
  
  --[=[ categoriesText: Adding the categories passed as numbered parameters
    See priv.defCat
  ]=]
  local categoriesText = ""
  local i = 1
  while not priv.isNull( tpl_args[i] ) do
    categoriesText = categoriesText .. priv.defCat( tpl_args[i], category )
    i = i + 1
  end
  if i == 1 then
    categoriesText = "[[Category:Definitions by Topic|" .. category .. "]]"
  end
  
  local result = mw.html.create()
  result
   :wikitext( 'This category contains definitions related to ' )
   :wikitext( defLinkWikitext )
   :wikitext( contextText )
   :wikitext( '.<br/>' )
   :wikitext( resultCatText )
   :wikitext( '__NOTOC__\n' )
   :wikitext( transcludeText )
   :wikitext( categoriesText )
   
  return result
end

return p