跳转到内容

Module:Side 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

-- 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')
p.start = makeInvokeFunc('_start')
p.mid = makeInvokeFunc('_mid')
p.ending = makeInvokeFunc('_end')

-- main function
function p._main(args)

	local new_line_if_vertical = if_vertical({'<br>',''})
	
	-- row number
	local parameter_count = 0
	for i,line in ipairs(args) do
		parameter_count = parameter_count + 1
	end
	local row_number = math.floor((parameter_count + 1)/ 2) 
	
	-- color
	local color = args.color or ''
	-- classes
	local class = args.class or ''
	
	-- container
	local container = mw.html.create( 'span' )
	container:attr( 'class', 'wst-side-container' )
	
	-- text rows
	local last_row
	for i = 1, row_number do
		local new_row = container:tag('span')
		new_row:attr( 'class', 'wst-side-text-row' )
		new_row:wikitext(args[i])
		if i == row_number then
			last_row = new_row	
		else
			container:wikitext(new_line_if_vertical)
		end
	end 
	
	local annotation_box = last_row:tag('span')
	annotation_box:attr( 'class', 'wst-side-annotation-box' )
	if color ~= '' then
		annotation_box:css( 'color', color )
	end
	if class ~= '' then
		class = mw.ustring.gsub(class, '%s', '')
		annotation_box:addClass('wst-side-annotation-s-' .. class )
	end	
	
	-- left delimiter
	local left_delimiter = annotation_box:tag('span')
	left_delimiter:attr('class', 'wst-side-delimiter')
	left_delimiter:wikitext('〈')
	
	-- annotations
	for i = 1, row_number do
		local new_annotation_row = annotation_box:tag('span')
		new_annotation_row:attr( 'class', 'wst-side-annotation-row' )
		new_annotation_row:wikitext(args[row_number+i])
	end	

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

	local full_annotation = tostring( container )

	--error(full_annotation)
	return full_annotation
end

-- the function '_start'
function p._start(args)
	
	local new_line_if_vertical = if_vertical({'<br>',''})
	
	-- args count
	local argument_count = 0
	for i,line in ipairs(args) do
		argument_count = argument_count + 1
	end
	local row_number = math.floor((argument_count + 1)/ 2) 
	
	-- use at the start of the second page
	if argument_count == 0 then
		return '<span class="wst-side-container">'
	end
	
	-- color
	local color = args.color or ''
	-- classes
	local class = args.class or ''
	
	-- container
	local container = mw.html.create( 'span' )
	container:attr( 'class', 'wst-side-container' )
	
	-- text rows
	local last_row
	for i = 1, row_number - 1 do
		local new_row = container:tag('span')
		new_row:attr( 'class', 'wst-side-text-row' )
		new_row:wikitext(args[i])
		if i < row_number - 1 then
			container:wikitext(new_line_if_vertical)
		end
	end 
	
	-- last row
	local last_row_text = '<span class="wst-side-text-row">' .. args[row_number]
	
	-- transclusion
	local file_name = args.file or ''
	local page_name = args.page or ''
	--local section_name = args.section or ''
	if file_name == '' or page_name == '' then
		error('參數錯誤:缺少嵌入的頁面名或頁碼')
	end
	local frame = mw.getCurrentFrame()
	local page_title = mw.title.getCurrentTitle()
	local remote_title_name = 'Page:' .. file_name .. '/' .. page_name
	local remote_title = mw.title.new(remote_title_name)

	-- transclusion by getTemplatesByName
	local remote_text = remote_title:getContent()
	local transcluder_text = getTemplatesByName(remote_text, 'Side annotation/m', 1)
	local transcluder_text_first = transcluder_text[1] or ''
	local processed_text = frame:preprocess(transcluder_text_first)
	--error(processed_text)
	if not page_title:inNamespace(104) then
		last_row_text = last_row_text .. '</span>' .. new_line_if_vertical .. processed_text 
	end

	local annotation_box = mw.html.create( 'span' )
	annotation_box:attr( 'class', 'wst-side-annotation-box' )
	if color ~= '' then
		annotation_box:css( 'color', color )
	end
	if class ~= '' then
		class = mw.ustring.gsub(class, '%s', '')
		annotation_box:addClass('wst-side-annotation-s-' .. class )
	end
	
	-- left delimiter
	local left_delimiter = annotation_box:tag('span')
	left_delimiter:attr('class', 'wst-side-delimiter')
	left_delimiter:wikitext('〈')
	
	-- annotations
	for i = 1, row_number do
		local new_annotation_row = annotation_box:tag('span')
		new_annotation_row:attr( 'class', 'wst-side-annotation-row' )
		new_annotation_row:wikitext(args[row_number+i])
	end	

	-- output
	local full_annotation = tostring( container )
	local annotation_box_text = tostring(annotation_box)
	full_annotation =  mw.ustring.sub(full_annotation, 1, -8) .. last_row_text .. mw.ustring.sub(annotation_box_text, 1, -8)
	return full_annotation
end

-- the function '_end'
function p._end(args)
	-- args count
	local argument_count = 0
	for i,line in ipairs(args) do
		argument_count = argument_count + 1
	end
	
	-- use at the end of the first page
	if argument_count == 0 then
		return '<span class="wst-side-delimiter">〉</span></span></span></span>'
	end
	
	local annotation_box_text = ''
	
	local page_title = mw.title.getCurrentTitle()
	if page_title:inNamespace(104) then
		local annotation_box = mw.html.create( 'span' )
		annotation_box:attr( 'class', 'wst-side-annotation-box' )
		annotation_box_text = tostring(annotation_box)
		annotation_box_text =  mw.ustring.sub(annotation_box_text, 1, -8)
		-- left delimiter
		local left_delimiter =  mw.html.create('span')
		left_delimiter:attr('class', 'wst-side-delimiter')
		left_delimiter:wikitext('〈')
		annotation_box_text = annotation_box_text .. tostring(left_delimiter)
	end
	
	-- annotations
	for i = 1, argument_count do
		local new_annotation_row = mw.html.create('span')
		new_annotation_row:attr( 'class', 'wst-side-annotation-row' )
		new_annotation_row:wikitext(args[i])
		annotation_box_text = annotation_box_text .. tostring(new_annotation_row)
	end	

	-- right delimiter
	local right_delimiter = mw.html.create('span')
	right_delimiter:attr('class', 'wst-side-delimiter')
	right_delimiter:wikitext('〉')
	annotation_box_text = annotation_box_text .. tostring(right_delimiter)

	-- output
	local full_annotation = annotation_box_text .. '</span></span></span>'
	return full_annotation
end

-- the function '_mid'
function p._mid(args)
	-- args count
	local argument_count = 0
	for i,line in ipairs(args) do
		argument_count = argument_count + 1
	end
	
	local mid_text = ''	

	for i = 1, argument_count do
		mid_text = mid_text .. '<span class="wst-side-text-row">' .. args[i]
		if i < argument_count then
			mid_text = mid_text .. '</span>'
		end
	end
	
	return mid_text
end

return p