跳转到内容

模組:SchläfliSymbol

维基百科,自由的百科全书
文档图示 模块文档[创建]
local p={}
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local supb_table={	['ⁱ']='i',['ⁿ']='n',['⁽']='(',['⁾']=')',['⁼']='=',['⁻']='-',['⁺']='+',['⁰']='0',['¹']='1',['²']='2',['³']='3',['⁴']='4',['⁵']='5',['⁶']='6',['⁷']='7',['⁸']='8',['⁹']='9',['’']=',',
					['ₐ']='a',['ₑ']='e',['ₕ']='h',['ₖ']='k',['ₗ']='l',['ₘ']='m',['ₙ']='n',['ₒ']='o',['ₚ']='p',['ₛ']='s',['ₜ']='t',['ₓ']='x',['ₔ']='ə',['₍']='(',['₎']=')',['₌']='=',['₋']='-',['₊']='+',['₀']='0',['₁']='1',['₂']='2',['₃']='3',['₄']='4',['₅']='5',['₆']='6',['₇']='7',['₈']='8',['₉']='9',['⏨']='10'}
local sup_table={	['i']='ⁱ',['n']='ⁿ',['(']='⁽',[')']='⁾',['=']='⁼',['-']='⁻',['+']='⁺',['0']='⁰',['1']='¹',['2']='²',['3']='³',['4']='⁴',['5']='⁵',['6']='⁶',['7']='⁷',['8']='⁸',['9']='⁹',[',']='’'}
local sub_table={	['a']='ₐ',['e']='ₑ',['h']='ₕ',['k']='ₖ',['l']='ₗ',['m']='ₘ',['n']='ₙ',['o']='ₒ',['p']='ₚ',['s']='ₛ',['t']='ₜ',['x']='ₓ',['ə']='ₔ',['(']='₍',[')']='₎',['=']='₌',['-']='₋',['+']='₊',['0']='₀',['1']='₁',['2']='₂',['3']='₃',['4']='₄',['5']='₅',['6']='₆',['7']='₇',['8']='₈',['9']='₉',['10']='⏨'}
local sup_list = 'ⁱⁿ⁽⁾⁼⁻⁺⁰¹²³⁴⁵⁶⁷⁸⁹’'
local sub_list = 'ₐₑₕₖₗₘₙₒₚₛₜₓₔ₍₎₌₋₊₀₁₂₃₄₅₆₇₈₉⏨'
local aliasesP = {
	coord                   = "P625",
	-----------------------
	image                   = "P18",
	author                  = "P50",
	publisher               = "P123",
	importedFrom            = "P143",
	statedIn                = "P248",
	pages                   = "P304",
	language                = "P407",
	hasPart                 = "P527",
	publicationDate         = "P577",
	startTime               = "P580",
	endTime                 = "P582",
	chapter                 = "P792",
	retrieved               = "P813",
	referenceURL            = "P854",
	sectionVerseOrParagraph = "P958",
	archiveURL              = "P1065",
	title                   = "P1476",
	formatterURL            = "P1630",
	quote                   = "P1683",
	shortName               = "P1813",
	definingFormula         = "P2534",
	archiveDate             = "P2960",
	inferredFrom            = "P3452",
	typeOfReference         = "P3865",
	column                  = "P3903"
}

function set_esc(text)
    	local result = mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(text,'%%','%%%%'),'<\/([^>]+)>','<%%\\%1>'),'<([^>]+)\/>','<%1%%\\>')
    	result = mw.ustring.gsub(result,'([^\/]+)/([^\/]+)','<sup>%1</sup>/<sub>%2</sub>')
    	return mw.ustring.gsub(mw.ustring.gsub(result,'%%\\','/'),'%%(.)','%1')
    end

function p._wd_text(text)
	local result = p._check_sub(text)
	result = p._check_sup(result)
	return mw.ustring.gsub(result,'[^%{%}|%[%],​]+',function(text) return set_esc(text) end)
end
function p._to_sub(text)
	local result = mw.ustring.gsub(text,'[ '..sub_list..']',function (chars) 
		return supb_table[chars] or chars
	end)
	return mw.ustring.gsub(result,'.',function (chars) 
		return sub_table[chars] or chars
	end)
end
function p._to_sup(text)
	local result = mw.ustring.gsub(text,'[ '..sub_list..']',function (chars) 
		return supb_table[chars] or chars
	end)
	return mw.ustring.gsub(result,'.',function (chars) 
		return sup_table[chars] or chars
	end)
end
function p._check_sub(text)
	return mw.ustring.gsub(text,'[ '..sub_list..']+',function (str) 
		return '<sub>'..mw.ustring.gsub(str,'['..sub_list..']',function (chars) 
			return supb_table[chars] or chars
		end)..'</sub>'
	end)
end
function p._check_sup(text)
	return mw.ustring.gsub(text,'[ '..sup_list..']+',function (str) 
		return '<sup>'..mw.ustring.gsub(str,'['..sup_list..']',function (chars) 
			return supb_table[chars] or chars
		end)..'</sup>'
	end)
end
function p.to_sub(frame)
    local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        args = getArgs(frame, {parentFirst=true}) --frame.args
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local result = p._to_sub(args[1] or args['1'] or '')
    return result
end
function p.to_sup(frame)
    local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        args = getArgs(frame, {parentFirst=true}) --frame.args
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local result = p._to_sup(args[1] or args['1'] or '')
    return result
end
function p.to_html(frame)
    local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        args = getArgs(frame, {parentFirst=true}) --frame.args
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local result = p._wd_text(args[1] or args['1'] or '')
    return result
end
function p._title2entity(input_title)
    local entity_id = mw.wikibase.getEntityIdForTitle(input_title)
    if entity_id then return tostring(entity_id) end
    local title_obj = mw.title.new(input_title)
    if (title_obj or {}).exists == true then
    	title_obj = title_obj.redirectTarget and title_obj.redirectTarget or title_obj
    	if title_obj then entity_id = mw.wikibase.getEntityIdForTitle(title_obj.prefixedText) end
    	local ch_flag = mw.ustring.find(''..title_obj.fullText,'#')
    	if not ch_flag then return entity_id end
    end
    local start,tail = mw.ustring.find(input_title,'[qQ]%d+')
    if start then 
    	local in_id = 'Q' .. mw.ustring.sub(input_title,start+1,tail)
    	if mw.wikibase.getLabel(in_id) ~= nil then return in_id 
    	else return nil end
    end
    local try_parse = mw.ustring.gsub(input_title,'[.,]','')
    start,tail = mw.ustring.find(try_parse,'%d+')
    if start then 
    	local parseNum = tonumber(mw.ustring.sub(try_parse,start,tail) or '0') or 0
		if mw.wikibase.getLabel('Q'..parseNum) ~= nil then return 'Q'..parseNum
    	else return nil end
    end
    return nil
end
function p.wikidata(frame)
    local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        args = getArgs(frame, {parentFirst=yesno(frame.args.template or 'no')}) --frame.args
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_title = mw.text.trim(tostring(args['1'] or args[1] or mw.getCurrentFrame():getTitle() or ''))
    local edit = yesno(args.edit)
    local spliter = mw.text.trim(tostring(args['2'] or args[2] or '、'))
    local edit_at_end = yesno(args["@end"])
    local wikidata_id = args.wikidata
    if mw.text.trim(wikidata_id or '') == '' then
    	wikidata_id = p._title2entity(input_title)
    end
    local ret = ''
    if wikidata_id and mw.wikibase.entityExists( wikidata_id ) then
    	local prop = 'P3228'
		local entity = mw.wikibase.getEntity( wikidata_id )
		local symbol = entity:getAllStatements( prop )
		local symbols = {}
		for k,v in pairs(symbol) do
			symbols[#symbols + 1] = p._wd_text((((v or {}).mainsnak or {}).datavalue or {}).value or '')
		end
		ret = table.concat(symbols,spliter)
		if edit and mw.text.trim(ret) ~= '' then
			local i18n = require("Module:Wd/i18n").init(aliasesP)
			ret = ret .. (edit_at_end and '<span style="float:right">' or "") .. "[[File:OOjs UI icon edit-ltr-progressive.svg|frameless|text-top|10px|alt=" .. 
				i18n['info']['edit-on-wikidata'] .. "|link=https://www.wikidata.org/wiki/" .. 
				wikidata_id .. "?uselang=zh#" .. prop .. "|" .. i18n['info']['edit-on-wikidata'] .. "]][[Category:使用維基數據的施萊夫利符號]]" ..
				(edit_at_end and '</span>' or "")
		end
    end
    return mw.text.trim(ret)
end
function p.polyhedra(frame)
    local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        args = getArgs(frame, {parentFirst=true}) --frame.args
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local function set_split_str(text, mode, show)
    	return (mode==1) and	(show and ('<sub>' .. text .. '</sub>&#123;') or '&#123;') or (
    	(mode==2) and	(show and ('&#125;<sub>' .. text .. '</sub>&#123;') or ',') or (
    	(mode==3) and	'&#124;' or 
    					(show and ('&#125;<sub>' .. text .. '</sub>') or '&#125;')))
    end
    local p_p = args[1] or args['1']
    local p_q = args[3] or args['3']
    local p_r = args[5] or args['5']
    local p_help = yesno(args.help)
    p_p = (mw.text.trim(p_p or '')~='') and p_p or nil
    p_q = (mw.text.trim(p_q or '')~='') and p_q or nil
    p_r = (mw.text.trim(p_r or '')~='') and p_r or nil

    local data_p, flag_p = set_esc(args[2] or args['2'] or '')
    local data_q, flag_q = set_esc(args[4] or args['4'] or '')
    local data_h, flag_h = set_esc(args[6] or args['6'] or '')
    local data_pp, flag_pp = set_esc(args[7] or args['7'] or '')
    local need_show = (tonumber(p_p or 2) ~= 2) or  (tonumber(p_q or 2) ~= 2) or  (tonumber(p_r or 2) ~= 2)
    local p_h = mw.text.trim(data_h or '')~='' and data_h or nil
    local p_pp = mw.text.trim(data_pp or '')~='' and data_pp or nil
    p_h = (tonumber(p_h or 2) ~= 2)
    p_pp = (tonumber(p_pp or 2) ~= 2)
    local p_str = p_help and ('<abbr title="構成面的多邊形邊數">'.. data_p .. '</abbr>') or data_p
    local q_str = p_help and ('<abbr title="共用相同頂點的多邊形數">'.. data_q .. '</abbr>') or data_q
    local h_str = p_help and ('<abbr title="立體結構中孔洞的形狀的邊數">'.. data_h .. '</abbr>') or data_h
    local pp_str = p_help and ('<abbr title="赤道面上扭歪多邊形邊數">'.. data_pp .. '</abbr>') or data_pp
    pp_str = flag_pp and ('<sub>' .. (need_show and ('<sub>&nbsp;' .. pp_str .. '</sub>') or pp_str) .. '</sub>') or pp_str


    return set_split_str(p_p or '2', 1, need_show) .. p_str .. set_split_str(p_q or '2', 2, need_show) .. q_str
    ..(p_h and (set_split_str(p_p, 3, need_show) .. h_str) or '') .. set_split_str(p_r or '2', 4, need_show)
    ..(p_pp and ('<sub>' .. pp_str .. '</sub>') or '')
end
return p