跳转到内容

Module:Header annotation

維基文庫,自由的圖書館

require('strict')

local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs
local error_message = require('Module:Error')['error']
local if_vertical = require('Module:If vertical')._main
local getTemplatesByName = require('Module:Transcluder more').getTemplatesByName
local getTemplateParameterByName = require('Module:Transcluder more').getTemplateParameterByName

-- invoke the functions
local function makeInvokeFunc(funcName)
	return function (frame)
		local args = getArgs(frame, {
			trim = true,
			removeBlanks = false,
			--frameOnly = true
		})
		return p[funcName](args)
	end
end

p.main = makeInvokeFunc('_main')

-- Helper function to parse a min-max range or number of page
-- Modified from Module:Transcluder: https://en.wikipedia.org/wiki/Module:Transcluder
-- Authors of Module:Transcluder: User:Sophivorus, User:Certes & others
-- License of Module:Transcluder: CC-BY-SA-3.0

local function parsePage(value)
	local first_page = 0
	local last_page = 0

	if type(value) == 'number' then
		first_page = value
		last_page = value

	elseif type(value) == 'string' then
		local range = mw.text.trim(value)
			local min, max = mw.ustring.match(range, '^(%d+)%s*[-–—]%s*(%d+)$') -- '3-5' to min=3 max=5
			if not max then min, max = string.match(range, '^((%d+))$') end -- '1' to min=1 max=1
			if max then
				first_page = min
				last_page = max
			end
		end
	return first_page, last_page
end
	
-- main function
function p._main(args)
	-- decide the writing direction
	local direction = if_vertical({'v','h'})

	-- color
	local color = args.color or ''
	-- classes
	local class = args.class or ''

	-- container
	local annotation_box = mw.html.create( 'span' )
	annotation_box:attr( 'class', 'wst-header-annotation-box' )
	if color ~= '' then
		annotation_box:css( 'color', color )
	end
	if class ~= '' then
		class = mw.ustring.gsub(class, '%s', '')
		annotation_box:addClass('wst-header-annotation-s-' .. class )
	end

	if direction == 'h' then
		annotation_box:addClass('wst-sidenote' )
		annotation_box:addClass('wst-sidenote-right' )
	end

	-- left delimiter
	local left_delimiter = annotation_box:tag('span')
	left_delimiter:attr('class', 'wst-header-delimiter')
	left_delimiter:wikitext('【')
	
	-- annotation
	local page_break = args.mul
	local annotation_inner = annotation_box:tag('span')
	annotation_inner:attr( 'class', 'wst-header-annotation-inner' )
	annotation_inner:wikitext(args[1])
	if direction == 'h' then
		annotation_inner:addClass('wst-sidenote-inner' )
	end
	local file_name = args.file
	local frame = mw.getCurrentFrame()
	local page_title = mw.title.getCurrentTitle()
	if file_name and not page_title:inNamespace(104) then
		-- transclude
		local pages = args.pages or ''
		if pages == '' then
			error('參數錯誤:缺少嵌入的頁碼')
		end
		local first_page, last_page = parsePage(pages)
		local frame = mw.getCurrentFrame()
		-- transclusion by getTemplatesByName
		for i = first_page, last_page do
			local remote_title_name = 'Page:' .. file_name .. '/' .. i
			local remote_title = mw.title.new(remote_title_name)
			local remote_text = remote_title:getContent()
			local transcluder_text = getTemplateParameterByName(remote_text, 'Header annotation',1) --only the first header annotation on each page is transcluded
			if #transcluder_text > 0 then
				local transcluder_text_first = transcluder_text[1][1] or ''
				local processed_text = frame:preprocess(transcluder_text_first)
				annotation_inner:wikitext(processed_text)
			end
		end
	end	

	-- right delimiter
	local right_delimiter = annotation_box:tag('span')
	right_delimiter:attr('class', 'wst-header-delimiter')
	right_delimiter:wikitext('】')

	local full_annotation = tostring( annotation_box )

	if direction == 'h' then
		local template_styles = mw.getCurrentFrame():extensionTag{ 
			name = "templatestyles", args = { src = 'Template:Sidenote/styles.css' }
		}
		full_annotation = template_styles .. full_annotation
	end
	
	return full_annotation
end


return p