模組:Header

出自微國家百科

可在模組:Header/doc建立此模組的說明文件

local p = {}
local timesTable = {
	['夏'] = '夏朝',
	['夏代'] = '夏朝',
	['商'] = '商朝',
	['商代'] = '商朝',
	['周'] = '周朝',
	['周代'] = '周朝',
	['秦'] = '秦朝',
	['秦代'] = '秦朝',
	['漢'] = '漢朝',
	['汉'] = '漢朝',
	['漢代'] = '漢朝',
	['汉代'] = '漢朝',
	['晉'] = '晉朝',
	['晋'] = '晉朝',
	['晉代'] = '晉朝',
	['晋代'] = '晉朝',
	['南朝宋'] = '劉宋',
	['隋'] = '隋朝',
	['隋代'] = '隋朝',
	['唐'] = '唐朝',
	['唐代'] = '唐朝',
	['宋'] = '宋朝',
	['宋代'] = '宋朝',
	['遼'] = '遼朝',
	['遼代'] = '遼朝',
	['金'] = '金朝',
	['金代'] = '金朝',
	['元'] = '元朝',
	['元代'] = '元朝',
	['明'] = '明朝',
	['明代'] = '明朝',
	['清'] = '清朝',
	['淸'] = '清朝',
	['清代'] = '清朝',
	['民國'] = '中華民國',
	['民国'] = '中華民國',
	['阮'] = '阮朝',
	['阮代'] = '阮朝'
}

local function addCategoryIf(text, condition, category)
	local text2 = text
	if condition and category ~= nil and category ~= '' then
		text2 = text2 .. '[[Category:' .. category .. ']]'
	end
	return text2
end

local function isempty(arg)
	return arg == nil or arg == ''
end

local function ROC(year, base)
	if (year == '1' or year == '元') and base == 0 then
		return '元'
	elseif year == '元' and base == 1911 then
		return '1912'
	else
		return tostring(tonumber(year) + base) -- 0 or 1911
	end
end

local function CategoryYMD(year, month, day)
	text = ''
	if not isempty(year) then
		text = '[[Category:' .. year .. '年]]'
		if not isempty(month) then
			text = text .. '[[Category:' .. year .. '年' .. month .. '月]]'
			if not isempty(day) then
				text = text .. '[[Category:' .. year .. '年' .. month .. '月' .. day .. '日]]'
			end
		end
	end
	return text
end

function p.main(frame)
	-- 参数
    local args = frame.args
    -- 处理 Template:Header
    if not isempty(args["included_via_template"]) then
    	if args["included_via_template"] == "1" then
    		args = frame:getParent().args
    	end
    end
	local title = args['title']
	if isempty(title) then
		title = mw.title.getCurrentTitle().text
	end
	local notes = args['notes']
	if notes == nil then
		notes = ''
	end
	local times = {
		args['times'],
		args['times2']
	}
	local from = {
		args['from'],
		args['from2'],
		args['from3']
	}
	local author = {
		args["author"],
		args["author2"],
		args["author3"],
		args["author4"],
		args["author5"],
		args["author6"],
		args["author7"],
		args["author8"],
		args["author9"],
		args["author10"],
		args["author11"],
		args["author12"],
		args["author13"],
		args["author14"],
		args["author15"],
		args["author16"],
		args["author17"],
		args["author18"],
		args["author19"],
		args["author20"]
	}
    local author_link = args["author_link"]
    local sign = args["署名"]
	local articleNo = args['发文字号']
	if isempty(articleNo) then
		articleNo = args['發文字號']
	end
	if isempty(articleNo) then
		articleNo = args['文号']
	end
	if isempty(articleNo) then
		articleNo = args['文號']
	end
	local caseNo = args['案号']
	if isempty(caseNo) then
		caseNo = args['案號']
	end
	local section = args['section']
	local alias = args['alias']
	local lawmaker = args["lawmaker"]
	local seealso = {
		args['seealso'],
		args['seealso2'],
		args['seealso3'],
		args['seealso4'],
		args['seealso5']
	}
	local collector = {
		args['collector'],
		args['collector2'],
		args['collector3'],
		args['collector4'],
		args['collector5'],
		args['collector6'],
		args['collector7'],
		args['collector8']
	}
	local ROCyear = {
		args['民'],
		args['民2'],
		args['民3']
	}
	local ROCmonth = {
		args['月'],
		args['月2'],
		args['月3']
	}
	local ROCday = {
		args['日'],
		args['日2'],
		args['日3']
	}
    local previous = args["previous"]
    local next = args["next"]
    local prv = args["prv"]
    local nxt = args["nxt"]
    
    if mw.wikibase ~= nil then
		entityid = mw.wikibase.getEntityIdForCurrentPage()
	else
		entityid = nil
	end
	-- 输出
	local text = '<div id="headerContainer">\n';
	text = text .. '{| style="width:100%; margin-top:0px;'
	text = text .. 'margin-bottom:3px;'; --此处原Header模版有判断语句,由于Plain sister模版的使用可能无需处理。
	text = text .. 'border:1px solid #93A6C0; background-color: #F9F9F9; text-align:center;"\n'
	text = text .. '|-\n'
	text = text .. '| style="width:0; text-align:left; font-size:small;"|'

    if not (isempty(previous) and isempty(prv)) then 
        text = text .. '  ←'
    end

    text = text .. '\n| style="width:25%; text-align:left; font-size:small;"|'

	if not isempty(previous) then
        text = text .. previous
    elseif not isempty(prv) then
        text = text ..  '[[' .. mw.title.rootPageTitle .. '/' .. prv .. '|' .. prv .. ']]'
    end
    text = text .. '\n|style="width:50%;" |<b>'
    text = text .. title
    text = text .. '</b>'
    
    if not isempty(articleNo) then
    	text = text .. '<br /><b>' .. frame:expandTemplate{title="i", args={articleNo}} .. '</b>'
    end
    if not isempty(caseNo) then
    	text = text .. '<br /><div style="text-align:right; font-weight:bold">' .. caseNo .. '</div>'
    end
    if not isempty(alias) then
    	text = text .. '<br />又名:<b>' .. alias .. '</b>'
    end
    if not isempty(section) then
    	text = text .. '<br />' .. section
    end
    if not isempty(author[1]) then
    	text = text .. '<br /><span style="color:#999999">作者:</span>'
    	if not isempty(author_link) then
    		text = text .. '[[作者:' .. author_link .. '|' .. author[1] .. ']]'
    	else
    		text = text .. '[[作者:' .. author[1] .. '|' .. author[1] .. ']]'
    	end
    end
    if not isempty(sign) then
    	text = text .. '<br /><span style="color:#999999">(署名:</span>'
    	text = text .. '[[作者:' .. sign .. '|' .. sign .. ']]'
    	text = text .. '<span style="color:#999999">)</span>'
    end
    for i = 2, 20 do
    	if not isempty(author[i]) then
    		text = text .. ' [[作者:' .. author[i] .. '|' .. author[i] .. ']]'
    	end
    end
    if not isempty(times[1]) then
    	text = text .. ' <span style="color:#999999">' .. times[1] .. '</span>'
    end
    if not isempty(collector[1]) then
    	if not isempty(author[1]) then
    		text = text .. '<br />'
    	end
    	text = text .. ' <br /><span style="color:#999999">輯者:</span>[[作者:' .. collector[1] .. '|' .. collector[1] .. ']]'
    	for i = 2, 8 do
    		if not isempty(collector[i]) then
				text = text .. ' [[作者:' .. collector[i] .. '|' .. collector[i] .. ']]'
            end
    	end
    end
	if not isempty(times[2]) then
		text = text .. ' <span style="color:#999999">' .. times[2] .. '</span>'
	end
	if not isempty(args['noauthor']) then
		text = text .. '<br/>' .. args["noauthor"]
	end
	if not isempty(args['override_author']) then
		text = text .. '<br/>' .. args["override_author"]
	end
	if not isempty(args['override_author2']) then
		text = text .. '<br/>' .. args["override_author2"]
	end
    if not isempty(args["notimes"]) then
    	text = text .. '<br/><span style="color:#999999">' .. args["notimes"] .. '</span>'
    end
    if not isempty(lawmaker) then
    	text = text .. '<br /><span style="color:#999999">制定机关:</span>' .. lawmaker
    end
    if not isempty(args["紀元"]) then
    	text = text .. '<br /><span style="color:#999999"><b>' .. args["紀元"]
    	local year = args['紀年']
    	if not isempty(year) then
    		if year == "一" then
    			text = text .. "元"
    		else
    			text = text .. year
    		end
    		text = text .. "年"
    		if not isempty(args["紀月"]) then
    			text = text .. args["紀月"] .. "月"
    			if not isempty(args["紀日"]) then
    				text = text .. args["紀日"] .. "日"
    			end
    		else
    			text = text .. '年間'
    		end
    	end
    	text = text .. "</b></span>"
    end
    -- 民
    for i = 1, 3 do
	    if not isempty(ROCyear[i]) then
	    	text = text .. '<br /><span style="color:#999999"><b>中華民國'
	    	if ROCyear[i] == '1' or ROCyear[i] == '元' then
	    		text = text .. '元年(1912年)'
	    	else
	    		text = text .. ROCyear[i] .. '年(' .. ROC(ROCyear[i], 1911) .. '年)'
	    	end
	    	if not isempty(ROCmonth[i]) then
	    		text = text .. ROCmonth[i] .. '月'
	    		if not isempty(ROCday[i]) then
	    			text = text .. ROCday[i] .. '日'
	    		end
	    	end
	    	text = text .. '</b></span>'
	    end
    end
    -- 年月日
    if not isempty(args["年"]) then
    	text = text .. '<br /><span style="color:#999999">'
    	text = text .. args['年'] .. '年'
    	if not isempty(args['月']) then
    		text = text .. args['月'] .. '月'
    		if not isempty(args['日']) then
    			text = text .. args['日'] .. '日'
    		end
    	end
    	text = text .. '</span>'
    end
    -- year,month,day
    if not isempty(args["year"]) then
    	text = text .. '<br /><span style="color:#999999">'
    	text = text .. args['year'] .. '年'
    	if not isempty(args['month']) then
    		text = text .. args['month'] .. '月'
    		if not isempty(args['day']) then
    			text = text .. args['day'] .. '日'
    		end
    	end
    	text = text .. '</span>'
    end
    -- y,m,d
    if not isempty(args["y"]) then
    	text = text .. '<br /><span style="color:#999999">'
    	text = text .. args['y'] .. '年'
    	if not isempty(args['m']) then
    		text = text .. args['m'] .. '月'
    		if not isempty(args['d']) then
    			text = text .. args['d'] .. '日'
    		end
    	end
    	text = text .. '</span>'
    end
    -- date
    if not isempty(args["date"]) then
    	text = text .. '<br /><span style="color:#999999">' .. args["date"] .. '</span>'
    end
    -- loc
    if not isempty(args["loc"]) then
    	text = text .. '<span style="color:#999999">于' .. args["loc"] .. '</span>'
    end
    if not isempty(args["at"]) then
    	text = text .. '<br /><span style="color:#999999"><small>(在[[:Category:' .. args["at"] .. '|' .. args["at"] .. ']]上)</small></span>'
    end
    if not isempty(args["发布者"]) then
    	text = text .. '<br /><span style="color:#999999">发布机关:</span>' .. args["发布者"]
    end
    if not isempty(args["发布于"]) then
    	text = text .. '<br /><span style="color:#999999">发布于</span>' .. args["发布于"]
    end
    if not isempty(args["公布於"]) then
    	text = text .. '<br /><span style="color:#999999">公布於</span>' .. args["公布於"]
    end
    if not isempty(args["公布字號"]) then
    	text = text .. '<br />' .. args["公布字號"]
    elseif not isempty(args["公布字号"]) then
    	text = text .. '<br />' .. args["公布字号"]
    end
    if not isempty(args["生效日期"]) then
    	text = text .. '<br /><span style="color:#999999">有效期:' .. args["生效日期"]
    	if isempty(args["失效日期"]) then
    		text = text .. '至今</span>'
    	else
    		text = text .. '—' .. args["失效日期"] .. '<small>(不含本日)</small></span>'
    	end
    end
    if not isempty(args["translator"]) then
    	text = text .. '<br /><span style="color:#999999">譯者:</span>'
    	text = text .. '[[作者:' .. args["translator"] .. '|' .. args["translator"] .. ']]'
    end
    if not isempty(args["notranslator"]) then
    	text = text .. '<br /><span style="color:#999999">譯者:</span>'
    	text = text .. args["notranslator"]
    end
    if not isempty(args["translator2"]) then
    	text = text .. ' [[作者:' .. args["translator2"] .. '|' .. args["translator2"] .. ']]'
    end
    if not isempty(args["translator3"]) then
    	text = text .. ' [[作者:' .. args["translator3"] .. '|' .. args["translator3"] .. ']]'
    end
    
    text = text .. '\n| style="width:25%; text-align:right; font-size:small;"|'

	if not isempty(next) then
        text = text .. next
    elseif not isempty(nxt) then
        text = text ..  '[[' .. mw.title.rootPageTitle .. '/' .. nxt .. '|' .. nxt .. ']]'
    end
    
	text = text .. '\n| class="noprint" style="width:0; text-align:right; font-size:small;"|'

    if not (isempty(next) and isempty(nxt)) then 
        text = text .. '  →'
    end
    
    text = text .. '\n|}'
    
    text = text .. '\n{| style="width:100%; background:#F8FCFF; font-size:small; border-bottom:1px solid #93A6C0"'
    text = text .. '\n|-'
    text = text .. '\n|'
    
    if not isempty(from[1]) then
    	text = text .. '\n{| style="font-size:0.9em; text-align:left; background:#F9F9F9; float:right;"'
    	text = text .. '\n| style="color: #999999;" |'
    	text = text .. frame:preprocess("-{zh:本作品收錄於;zh-hant:本作品收錄於;zh-hans:本作品收录于}-《'''[[" .. from[1] .. "]]'''》")
    	if not isempty(from[2]) then
    		text = text .. frame:preprocess("和《'''[[" .. from[2] .. "]]'''》")
	    	if not isempty(from[3]) then
	    		text = text .. frame:preprocess("以及《'''[[" .. from[3] .. "]]'''》")
	    	end
    	end
    	text = text .. '\n|}\n'
    end
    
    text = text .. notes
    -- 维基跨语言链接自动生成。
	-- if page is not connected to Wikidata, only show plain sister if at least one of its parameters is filled in
	-- if page is connected to Wikidata, always show plain sister
    if not (entityid == nil and isempty(args["disambiguation"]) and isempty(args["edition"]) and isempty(args["portal"]) and isempty(args["related_author"]) and isempty(args["wikipedia"]) and isempty(args["commons"]) and isempty(args["commonscat"]) and isempty(args["wikiquote"]) and isempty(args["wikinews"]) and isempty(args["wiktionary"]) and isempty(args["wikibooks"]) and isempty(args["wikilivres"]) and isempty(args["wikidata"]) and isempty(args["wikivoyage"]) and isempty(args["wikiversity"]) and isempty(args["wikispecies"]) and isempty(args["meta"])) then
    	text = text .. frame:expandTemplate{title='Plain sister',args={disambiguation = args["disambiguation"], edition = args["edition"], portal = args["portal"], related_author = args["related_author"], wikipedia = args["wikipedia"], commons = args["commons"], commonscat = args["commonscat"], wikiquote = args["wikiquote"], wikinews = args["wikinews"], wiktionary = args["wiktionary"], wikibooks = args["wikibooks"], wikilivres = args["wikilivres"], wikidata = args["wikidata"], wikivoyage = args["wikivoyage"], wikiversity = args["wikiversity"], wikispecies = args["wikispecies"], meta = args["meta"]}}
    end
    text = text .. frame:expandTemplate{title='main subject',args={}}
    text = text .. '\n|}'
	text = text .. '</div>'
	
	for i = 1, 2 do
		if not isempty(times[i]) then
			if timesTable[times[i]] ~= nil then
				text = addCategoryIf(text, 1, timesTable[times[i]])
			else
				local times_object = mw.title.new('Category:' .. times[i] .. '作品')
				if times_object.exists then
					text = addCategoryIf(text, 1, times[i] .. '作品')
			else
					text = addCategoryIf(text, 1, times[i])
				end
			end
		end
	end
	if not isempty(author_link) then
		text = addCategoryIf(text, 1, author_link)
	else
		text = addCategoryIf(text, 1, author[1])
	end
	for i = 2, 20 do
		text = addCategoryIf(text, 1, author[i])
    end
	text = addCategoryIf(text, 1, args["translator"])
	text = addCategoryIf(text, 1, args["theme"])
	for i = 2, 7 do
		text = addCategoryIf(text, 1, args["theme" .. tostring(i)])
	end
	text = addCategoryIf(text, 1, args["at"])
	text = addCategoryIf(text, 1, args["loc"])
	if not isempty(args["loc"]) then
		if not isempty(args["y"]) then
			text = addCategoryIf(text, 1, args["y"] .. '年' .. args["loc"])
		end
		if not isempty(args["year"]) then
			text = addCategoryIf(text, 1, args["year"] .. '年' .. args["loc"])
		end
		if not isempty(args["年"]) then
			text = addCategoryIf(text, 1, args["年"] .. '年' .. args["loc"])
		end
		if not isempty(args["紀元"]) and not isempty(args["紀年"]) then
			text = addCategoryIf(text, 1, args["紀元"] .. args["紀年"] .. '年' .. args["loc"])
		end
		for i = 1, 3 do
			if not isempty(ROCyear[i]) then
				text = addCategoryIf(text, 1, '中華民國' .. ROC(ROCyear[i], 0) .. '年' .. args["loc"])
			end
		end
	end
	if not isempty(args["type"]) then
		if not isempty(args["y"]) then
			text = addCategoryIf(text, 1, args["y"] .. '年' .. args["type"])
		end
		if not isempty(args["year"]) then
			text = addCategoryIf(text, 1, args["year"] .. '年' .. args["type"])
		end
		if not isempty(args["年"]) then
			text = addCategoryIf(text, 1, args["年"] .. '年' .. args["type"])
		end
		for i = 1, 3 do
			if not isempty(ROCyear[i]) then
				text = addCategoryIf(text, 1, '中華民國' .. ROC(ROCyear[i], 0) .. '年' .. args["type"])
			end
		end
		if not isempty(args["紀元"]) and not isempty(args["紀年"]) then
			text = addCategoryIf(text, 1, args["紀元"] .. args["紀年"] .. '年' .. args["type"])
		end
		if args["type"] == "五言" or args["type"] == "四言" or args["type"] == "七言" then
			text = text .. '[[Category:' .. args["type"] .. '詩]]'
		else
			text = text .. '[[Category:' .. args["type"] .. ']]'
		end
	end
	for i = 2, 10 do
		local typei = args["type" .. tostring(i)]
		if not isempty(typei) then
			if not isempty(args["y"]) then
				text = addCategoryIf(text, 1, args["y"] .. '年' .. typei)
			end
			if not isempty(args["year"]) then
				text = addCategoryIf(text, 1, args["year"] .. '年' .. typei)
			end
			if not isempty(args["年"]) then
				text = addCategoryIf(text, 1, args["年"] .. '年' .. typei)
			end
			if not isempty(ROCyear[1]) then
				text = addCategoryIf(text, 1, '中華民國' .. ROC(ROCyear[1], 0) .. '年' .. typei)
			end
			text = text .. '[[Category:' .. typei .. ']]'
		end
	end
	for i = 1, 3 do
		if not isempty(from[i]) then
			local from_page = mw.text.split(from[i], "|")[1]
			local from_page_object = mw.title.new('Category:' .. from_page)
			if from_page_object ~= nil then
				if from_page_object.exists then
					text = text .. '[[Category:' .. from[i] .. ']]'
				end
			end
		end
	end
	text = text .. CategoryYMD(args["y"], args["m"], args["d"])
	text = text .. CategoryYMD(args["year"], args["month"], args["day"])
	text = text .. CategoryYMD(args["年"], args["月"], args["日"])
	for i = 1, 3 do
		if not isempty(ROCyear[i]) then
			text = text .. '[[Category:中華民國' .. ROC(ROCyear[i], 0) .. '年]]'
			if not isempty(ROCmonth[i]) then
				text = text .. '[[Category:中華民國' .. ROC(ROCyear[i], 0) .. '年' .. ROCmonth[i] .. '月]]'
				if not isempty(ROCday[i]) then
					text = text .. '[[Category:' .. ROC(ROCyear[i], 1911) .. '年' .. ROCmonth[i] .. '月' .. ROCday[i] .. '日]]'
				end
			end
		end
	end
	if not isempty(args["紀元"]) then
		if not isempty(args["紀年"]) then
			text = text .. '[[Category:' .. args["紀元"] .. args["紀年"] .. '年]]'
			if not isempty(args["月"]) then
				text = text .. '[[Category:' .. args["紀元"] .. args["紀年"] .. '年' .. args["月"] .. '月]]'
				if not isempty(args["日"]) then
					text = text .. '[[Category:' .. args["紀元"] .. args["紀年"] .. '年' .. args["月"] .. '月' .. args["日"] .. '日]]'
				end
			end
		else
			text = text .. '[[Category:' .. args["紀元"] .. ']]'
		end
	end
	
	text = addCategoryIf(text, 1, args["main"])
	if not isempty(args["is_main_of"]) then
		text = text .. '[[Category:' .. args["is_main_of"] .. '| ]]'
	end
	-- 通过categories参数添加分类,分类按照 A/B/C 形式输入,自动划分
	if not isempty(args["categories"]) then
		for i in mw.text.gsplit(args["categories"], "/", true) do
			text = addCategoryIf(text, 1, i)
		end
	end
	
	return text;
end

return p