跳转到内容

模組:Link with book title mark/sandbox

维基百科,自由的百科全书
local z={}

	function main(frame, mode)
	    if frame.args.parent then
	        args = frame:getParent().args
	    else
	        args = frame.args
	    end
		local a = args[1]
		local _b = args[2]

		if a == nil then
			return ''
		end
		
		local pagename = frame:preprocess('{{PAGENAME:' .. a .. '}}')
		local section = a:gsub('^.-#', '')
		local hasSection = 1
		if section == a then
			hasSection = 0
		end

		local custom = 0
		if _b == nil then
			b = a
		elseif _b == '' then
			b = pagename
			if hasSection == 1 then
				b = pagename .. '#' .. section
			end
		else
			custom = 1
			b = ""
			hasSection = 0
		end
		
		b = b:gsub('#(.*)$', '')
		b = b:gsub("《", "〈"):gsub("》", "〉")

		if mode == 'dot' then
			b = b:gsub("/", "·")
		end
		
		if custom == 1 then
			b = _b
		end

		local result
		if hasSection == 0 then
			b = b:gsub("《", "〈"):gsub("》", "〉")
			result = '《[[:' .. a .. '|' .. b .. ']]》'
		elseif hasSection == 1 then
			b = b:gsub("《", "〈"):gsub("》", "〉")
			result = '[[:' .. a .. '|《' .. b .. '》§' .. section .. ']]'
		end
		return result
	end

	
	function z.main(frame)
		local out

		out = main(frame, 'plain')
		return out
	end

	function z.dot(frame)
		local out

		out = main(frame, 'dot')
		return out
	end
return z