Module:User:Wyang/link

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This is a private module sandbox of Wyang, for their own experimentation. Items in this module may be added and removed at Wyang's discretion; do not rely on this module's stability.


local export = {}

function export.link(frame)
	args = args or frame:getParent().args
	pagename = pagename or mw.title.getCurrentTitle().text
	forced_tr = ''
	if args[2] and mw.ustring.match(args[2], '[一-龯㐀-䶵]') then
		gloss = args[4] or ''
		tr = args[3] or ''
		text = args[1] .. '/' .. args[2]
	else
		text = args[1] or ''
		if not args['gloss'] or args['gloss'] == '' then
			if args[2] and (mw.ustring.match(args[2], '[āōēīūǖáóéíúǘǎǒěǐǔǚàòèìùǜâêîôû̍ⁿ]') or mw.ustring.match(args[2], '[bcdfghjklmnpqrstwz]h?y?[aeiou][aeiou]?[iumnptk]?g?[1-9]')) then
				tr = args[2] or ''
				gloss = args[3] or ''
			else
				tr = ''
				gloss = args[2] or ''
			end
		else
			tr = args[2] or ''
			gloss = args['gloss']
		end
	end
	if args['tr'] and args['tr'] ~= '' then
		forced_tr = args['tr']
		tr = args['tr']
		gloss = gloss or args[2] or ''
	end
	if text ~= '' and not text:match('[%[%]]') then
		word = mw.text.split(text,"/",true)
		if word[1] and mw.title.new(word[1]).exists and text ~= pagename then
			content = mw.title.new(word[1]):getContent()
			content = mw.ustring.gsub(content, ",([^ ])", ";%1")
			template = mw.ustring.match(content, "{{zh%-pron[^}]*|m=([^};|\n]+)")
			if template and template ~= "" then
				if cmn_pron == nil then
				   cmn_pron = require("Module:cmn-pron")
				end
				new_tr = cmn_pron.str_analysis(template, 'link')
			end
			if new_tr == tr then
				tr = ''
			end
		end
	else
		word = { text }
	end
	return '{{zh-l|' .. table.concat(word,"/") .. ((tr and tr ~= '') and '|' .. (forced_tr ~= '' and 'tr=' or '') .. tr or '') .. ((gloss and gloss ~= '') and '|' .. gloss or '') .. '}}'
end

return export