跳转到内容

模組:Chembox CASNo

被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由A2569875留言 | 贡献2020年10月29日 (四) 15:21编辑。这可能和当前版本存在着巨大的差异。

local p,lib_arg ={},{}
function p._print_chembox(frame)
-- For calling from #invoke.
    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.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame) --frame.args
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
    end
    working_frame = mw.getCurrentFrame()
	local chembox_str = working_frame:preprocess('{{Chembox CASNo}}')
	local start=mw.ustring.find(chembox_str, '|', 1+({mw.ustring.find(chembox_str, '\n')})[1] )
	local chembox_start, chembox_end = mw.ustring.sub(chembox_str,1,start), mw.ustring.sub(chembox_str,start+1,-1)
	
	local casData = {}
	for k,v in pairs(args) do
		if mw.ustring.find(k,'CAS') then
			local check_id = mw.ustring.gsub(k, '%d+', function(str) check_id = check_id or tonumber(str) end )
			local check_k = mw.ustring.lower(k)
			local norm_k = check_k
			local argtype = 'number'
			
		
			
			if mw.ustring.find(check_k,'ref') then argtype = 'ref'
				norm_k = mw.ustring.gsub(norm_k,'[_%s]-ref','')
			elseif mw.ustring.find(check_k,'comment') or mw.ustring.find(check_k,'cmt') or mw.ustring.find(check_k,'supplemental') then argtype = 'comment'
				norm_k = mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(norm_k,'[_%s]-comment',''),'[_%s]-cmt',''),'[_%s]-supplemental','')
			elseif mw.ustring.find(check_k,'ur[li]') then argtype = 'url'
				norm_k = mw.ustring.gsub(norm_k,'[_%s]-ur[li]','')
			else
			end
			casData[norm_k] = casData[norm_k] or {}
			if argtype == 'number' or argtype == 'url' then
				casData[norm_k][argtype] = casData[norm_k][argtype] or v
			else
				local casData_temp = casData[norm_k][argtype]
				if casData_temp then casData_temp = casData_temp .. '、' .. v
				else casData_temp = v
				end
				casData[norm_k][argtype] = casData_temp
			end
			--mw.log('name='..k..', norm='..norm_k)
		end
	end
	local count = 0
	local body = ''
	local last = nil
	local no_ref=false
	local no_index=false
	
	for k,v in pairs(casData) do
		local check_k = mw.ustring.lower(k)
		if mw.ustring.find(check_k,'nos') or mw.ustring.find(check_k,'other') then
			if body~='' then body = body .. '<br/>' end
			body = body .. v.number .. (v.comment and ' ' .. v.comment or '') .. (v.ref and ' ' .. v.ref or '')
			no_index = true
		else
			if v.number then
				if not v.ref then no_ref=true end
				count = count + 1
				body = body .. require('Module:Template invocation').invocation('Chembox CASCheck', {
					count=count,
					value=v.number,
					value_url=v.url,
					value_comment=v.comment,
					value_ref=v.ref,
					value0=last
				})
				last=v.number
			end
		end
	end

	if mw.title.getCurrentTitle().namespace == 0 then
		if no_ref then body=body..'[[Category:未提供參考文獻的CAS號]]' end
		if no_index then body=body..'[[Category:含有未被索引CAS號的條目]]' end
	else
		body = require('Module:Delcat').delete_category(working_frame:preprocess(body),{})
	end
	if require('Module:Yesno')(args.inChembox) then
		body = chembox_start..body..chembox_end
	end

	return working_frame:preprocess(body)
	
end
return p