Jump to content

Module:Header

From Wikisource

Documentation for this module may be created atModule:Header/doc

--[=[
This is a module to implement logic for [[Template:Header]] and [[Template:Translation header]]

TODO:
- centuries are defined as starting on XX01, but WS categorizes them as starting on XX00
-- check whether that's a considered policy choice
]=]

require('strict')

localp={}--p stands for package

localyesno=require('Module:Yesno')
localgetArgs=require('Module:Arguments').getArgs
localtableTools=require('Module:TableTools')
localISO_639_language_name=require('Module:ISO 639').language_name
localparent_links=require('Module:Auto parents')._parent_links

localconstruct_header=require('Module:Header structure').construct_header
localheaderAttributions=require('Module:Header/attribution')
localconstruct_defaultsort=require('Module:Header/sort')._construct_defaultsort
localconstruct_year=require('Module:Header/year').construct_year

localcurrent_title=mw.title.getCurrentTitle()

--[=[
Wrap stylesheet in noexport div
]=]
localfunctionget_noexport_stylesheet(template)
returntostring(mw.html.create('div'):addClass('ws-noexport'):wikitext(mw.getCurrentFrame():extensionTag('templatestyles','',{src=template..'/styles.css'})))
end

--[=[
Get badge if any
]=]
localfunctionbadge()
returnrequire('Module:Edition').badge({args={category='1',indicator='1'}})
end

--[=[
Construct the [[Help:Microformat]] for the page.

This is in the form:
<div id= "ws-data"...>
<span id= "ws-title" >Title here...</span>
...
<div>
]=]
localfunctionconstruct_microformat(args)
localmf_div=mw.html.create('div')
:addClass('ws-noexport')
:attr('id','ws-data')
:css({speak='none'})

-- override to show the microformat
ifyesno(args['show-microformat'])then
mf_div:addClass('ws-data-show')
end

-- collect the MF values here
localmf={};

mf['ws-article-id']=current_title.id

-- add the title
ifargs['title']then
mf['ws-title']=args['title']

-- append section if there is one
ifargs['section']then
mf['ws-title']=mf['ws-title'].."—"..args['section']
end
end

localauthor=args['override-section-author']orargs['section-author']orargs['override-author']orargs['author']
ifauthorthen
mf['ws-author']=author
end

localtranslator=args['override-translator']orargs['translator']
iftranslatorthen
mf['ws-translator']=translator
end

localyear=args['year']
ifyearthen
mf['ws-year']=year
end

ifargs['cover']then
mf['ws-cover']=args['cover']
end

fork,vinpairs(mf)do
mf_div:tag('span'):attr('id',k):wikitext(v)
end

returntostring(mf_div)
end

--[=[
Detect explicit formatting in fields like "section" and "title"
]=]
localfunctionexplicit_formatting(str)
returnstring.match(str,"'''?")
orstring.match(str,"<%s*/?%s*[iIbB]%s*>")
-- add more cases here or come up with a less silly way to do things
end

localfunctioncheck_non_existent_author_pages(args,param,categories)
ifargs[param]then
-- some pages expect an invalid author
locallower_arg=string.lower(args[param])
localattr_data=headerAttributions.attr_data[param]orheaderAttributions.attr_data[string.gsub(param,'section-','')]
ifnotattr_dataornotattr_data['special_cases'][lower_arg]then
localtarget=mw.title.makeTitle("Author",args[param])
-- expensive function!
ifnottargetornottarget.existsthen
table.insert(categories,"Works with non-existent author pages")
end
end
end
end

--[=[
Construct the automatic categories for the header
]=]
localfunctionconstruct_categories(args)
localcategories={}

ifargs['override-author']then
table.insert(categories,"Pages with override author")
end

ifcurrent_title:inNamespaces(0)orargs.testingthen
check_non_existent_author_pages(args,'author',categories)
check_non_existent_author_pages(args,'editor',categories)
check_non_existent_author_pages(args,'translator',categories)
check_non_existent_author_pages(args,'section-translator',categories)
check_non_existent_author_pages(args,'section-author',categories)
end

ifargs['section-author']then
table.insert(categories,'Pages with contributor')
end
ifargs['override-section-author']then
table.insert(categories,'Pages with override contributor')
end

localauthor=args['override-author']orargs['author']
ifauthorand(string.lower(author)=='unknown')then
ifargs.template_name=='Translation header'then
table.insert(categories,'Translations of anonymous works')
else
table.insert(categories,"Anonymous texts")
end
end

localeditor=args['override-editor']orargs['editor']
ifeditorthen
editor=string.lower(editor)
ifeditor=='unknown'oreditor=='?'then
table.insert(categories,"Works with unknown editors")
elseifeditor=='not mentioned'then
table.insert(categories,"Works with unmentioned editors")
end
end

localtranslator=args['override-translator']orargs['translator']
iftranslatorthen
translator=string.lower(translator)
iftranslator=='unknown'ortranslator=='not mentioned'ortranslator=='?'then
table.insert(categories,'Translations without translator information specified')
end
end

ifargs["shortcut"]then
table.insert(categories,'Pages with shortcuts')
end

ifargs['noyear']then
table.insert(categories,"Pages with noyear")
end
ifyesno(args['noyearcat'])then
table.insert(categories,"Pages with noyearcat")
end

ifargs['cover']then
table.insert(categories,"Pages with an export cover")
end

-- sanity/maintenance checks on various parameters

-- allow_explicit_formatting parameter suppresses this check
-- used by, for example, [[Template:Versions]]
ifnotyesno(args['allow-explicit-formatting'])then
ifargs['title']andexplicit_formatting(args['title'])then
table.insert(categories,"Pages with explicit formatting in header fields")
end
ifargs['section']andexplicit_formatting(args['section'])then
table.insert(categories,"Pages with explicit formatting in header fields")
end
end

-- translation header categories
ifargs.template_name=='Translation header'then
ifargs.languagethen
table.insert(categories,'Works originally in '..(args.language_nameor'an undefined language'))
else
table.insert(categories,'Wikisource translations with no original language')
end

ifnotargs.originalthen
table.insert(categories,'Wikisource translations with no original source')
end

ifnotcurrent_title.isSubpagethen
table.insert(categories,'Wikisource translations')
end
end

localcategory_links={}
fork,vinpairs(categories)do
table.insert(category_links,'[[Category:'..v..']]')
end
returntable.concat(category_links)
end

--[=[
Check for numerical parameters (which shouldn't be used)
]=]
localfunctioncheck_for_numerical_arguments(args)
fork,vinpairs(args)do
iftype(k)=='number'then
return'[[Category:'..'Headers with numerical arguments'..']]'
end
end
return''
end

--[=[
Add categories from the categories parameter
]=]

localfunctionmanual_categories(args)
ifnotargs.categoriesthen
return''
end

-- Replace each string that ends in a slash with a category definition.
-- This does include the final one because we're adding a slash to the input string.
localcategories=mw.ustring.gsub(args.categories..'/','([^/]+)/*','[[Category:%1]]')

return'[[Category:'..'Works using categories parameter'..']]'..categories
end

--[=[
Categorize subpages
]=]
localfunctioncheck_subpages()
localtitle=current_title
localparent_exists=false
whiletitle.isSubpageandnotparent_existsdo
title=mw.title.new(title.baseText,title.nsText)
parent_exists=title.exists
end
ifparent_existsandtitle:inNamespaces(0)then
return'[[Category:'..'Subpages'..']]'
elseifparent_existsthen
return'[[Category:'..title.nsText..' subpages'..']]'
else
return''
end
end

--[=[
Assemble the title
]=]
localfunctionheader_title(args)
localtitle=args.titleor''
localtitleSpan=tostring(mw.html.create('span'):attr('id','header-title-text'):wikitext(title))

localyear=construct_year(args)
localattr=headerAttributions.construct_attributions(args)
localsection=headerAttributions.construct_section(args)

ifattr~=''andtitle~=''then
attr='<br id=\ "header-title=break\ "/>'..attr
end

returntable.concat({titleSpan,year,attr,section})
end

--[=[
[[Template:Header]]
]=]
functionp._header(args,argsWithBlanks)
argsWithBlanks=argsWithBlanksorargs

-- aliases
localdup_cat=''
localnewArgs={}

fork,vinpairs(args)do
localnewkey=string.lower(string.gsub(string.gsub(tostring(k),'_','-'),' ','-'))
ifnewkey~=tostring(k)then
ifargsWithBlanks[newkey]then
dup_cat='[[Category:'..'Pages using duplicate arguments in template calls'..']]'
end
ifnotargs[newkey]then
newArgs[newkey]=newArgs[newkey]orv
end
end
end
fork,vinpairs(newArgs)do
args[k]=v
argsWithBlanks[k]=v
end

newArgs={}
localaliases={
['section-author']='contributor',
['section-translator']='contributing%-translator'
}
forarg,aliasinpairs(aliases)do
fork,vinpairs(args)do
localnewkey=string.gsub(k,alias,arg)
ifnewkey~=tostring(k)then
ifargsWithBlanks[newkey]then
dup_cat='[[Category:'..'Pages using duplicate arguments in template calls'..']]'
end
ifnotargs[newkey]then
newArgs[newkey]=v
end
end
end
end
fork,vinpairs(newArgs)do
args[k]=v
argsWithBlanks[k]=v
end

args.sortkey=args.defaultsortorargs.sortkey

-- add aliases to argsWithBlanks
fork,vinpairs(args)do
ifnotargsWithBlanks[k]then
argsWithBlanks[k]=v
end
end

-- default values
args.template_name=args.template_nameor'Header'
args.testing=yesno(args.testingorcurrent_title.fullText=='Template:Header/testcases'orcurrent_title.fullText=='Template:Translation header/testcases')

-- default values for title and section (allow override by setting to blank)
ifnotargsWithBlanks['title']then
args['title']=parent_links({})
argsWithBlanks['title']=args['title']
end
ifnotargsWithBlanks['section']andcurrent_title.isSubpagethen
args['section']=current_title.subpageText
argsWithBlanks['section']=args['section']
end

-- header args

args.pre_container=badge()
args.header_class='wst-header ws-header ws-noexport noprint dynlayout-exempt '..(args.header_classor'')
args.main_class='headertemplate'
args.notes_id=args.notes_idor'navigationNotes'

-- title
args.main_title=header_title(args)

-- FIXME: just use Wikidata instead of interwiki links?
localinterwiki=''
ifargs.template_name=='Translation header'andargs.languagethen
interwiki=tostring(mw.html.create('span'):addClass('interwiki-info'):attr('id',args.language):attr('title','(original)'))
ifargs.originaland(args.language=='ang'orargs.language=='enm'orargs.language=='sco')then
-- cycle to mul.ws and back around to en.ws
interwiki=interwiki..'[['..args.language..':en:'..args.original..']]'
elseifargs.originalandargs.languageandargs.language_namethen
-- general interwiki link
interwiki=interwiki..'[['..args.language..':'..args.original..']]'
end
end

-- defaultsort tracking categories
args.equalsortcat='[[Category:'..'Headers with DefaultSort equal to page title'..']]'
args.diffsortcat='[[Category:'..'Headers applying DefaultSort key'..']]'

args.post_notes=table.concat({
construct_microformat(args),
check_subpages(),
manual_categories(args),
construct_categories(args),
check_for_numerical_arguments(argsWithBlanks),
construct_defaultsort(args),
dup_cat,
interwiki
})

returnget_noexport_stylesheet('Header')..construct_header(args)
end

functionp.header(frame)
returnp._header(
getArgs(frame),
getArgs(frame,{removeBlanks=false})
)
end

--[=[
[[Template:Translation header]]
]=]
functionp._translation_header(args,argsWithBlanks)
argsWithBlanks=argsWithBlanksorargs

args.header_class='wst-translation-header'
args.template_name='Translation header'
args.main_id='translationheadertemplate'
args.notes_id='translation-notes'
args.notes_class='header-notes'
ifargs.languagethen
args.language_name=ISO_639_language_name(args.language)
end

returnget_noexport_stylesheet('Translation header')..p._header(args,argsWithBlanks)
end

functionp.translation_header(frame)
returnp._translation_header(
getArgs(frame),
getArgs(frame,{removeBlanks=false})
)
end

returnp