模組:沙盒/a2569875/Test3

本页使用了标题或全文手工转换
维基百科,自由的百科全书

123

文档图示 模块文档[查看] [编辑] [历史] [清除缓存]

require('Module:Module wikitext')._addText('123')
local p={}
local encode_table={}
local libencode = require('Module:EncoderUtil')
local libqrcode = libencode._get_libqrcode()
local lib_arg={}

--{{#invoke:沙盒/a2569875/Test3|qr_graph|123|width=250}}
function p.qr_graph(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, {parentFirst=true}) --frame
        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()
    end
    local ec_level_list={l=1,m=2,q=3,h=4,[0]=1,[1]=1,[2]=2,[3]=3,[4]=4,
		low=1,medium=2,quartile=3,high=4
    }
    local qr_message =args.text or args.message or args.msg or args['string'] or args[1] or args['1'] or 'QR Code'
    local ec_level = ec_level_list[args.eclevel or'']or 1
	local pxsize = tonumber(args.width or args.size or args.height or 120)
	
	local fun_light = args.fun_light or args.light or '#fff'
	local message_light = args.message_light or args.light or '#fff'
	local fun_dark = args.fun_dark or args.dark or '#000'
	local message_dark = args.message_dark or args.dark or '#000'
	
	local success,result = libqrcode.qrcode(qr_message,ec_level,nil,tonumber(args.version or args.ver))
	local w_count = #result
	local cell_width = pxsize / (w_count+2)
	local graph_data = {
		width=pxsize,
		height=pxsize,
		data={},
		padding={left=cell_width,top=cell_width,bottom=cell_width,right=cell_width},
		scales={
		    {name="x",type="linear",domain={0,w_count},range="width"},
		    {name="y",type="linear",domain={0,w_count},range="height"},
		    {name="qr",type="ordinal",domain={-2,-1,0,1,2},range={fun_light,message_light,message_light,message_dark,fun_dark}}
		},
		marks={
		    {
		      type="rect",
		      from={data="qrcode"},
		      properties={
		        enter={
		          x={scale="x",field="0"},y={scale="y",field="1"},
		          width={value=cell_width},height={value=cell_width},
		          stroke={scale="qr",field="2"},
		          fill={scale="qr",field="2"}
		        }
		      }
		    }
		 }
	}

	local qr_data = {name="qrcode",values={}}
	for x=1,w_count do
		for y=1,#(result[x]) do
			qr_data.values[#(qr_data.values)+1] = {x-1,#(result[x])-y+1,result[x][y]}
		end
	end
	graph_data.data[#(graph_data.data)+1] = qr_data
	local body = working_frame:callParserFunction{ name = '#tag:graph', args = {
    	mw.text.jsonEncode(graph_data)
	} }
	return body
end

--{{#invoke:沙盒/a2569875/Test3|checkHeader}}
function p.checkHeader(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
    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 magic_str='{{subst:void|僅能透過模板內部呼叫}}'
    local input_str = working_frame.args[1] or working_frame.args['1']
    local arg_str = working_frame.args[2] or working_frame.args['2']
    if input_str then
    	local start = mw.ustring.len(magic_str) + 1
    	local check_start = mw.ustring.lower(mw.ustring.sub(input_str,1,start-1))
    	if mw.ustring.lower(check_start) ~= mw.ustring.lower(magic_str) then
    		if arg_str then mw.addWarning( '錯誤! 參數 \''.. arg_str ..'\' 僅能由模板呼叫。' ) end
    		require('Module:TrackingCategory').append(working_frame,'CAS號參數誤植的條目')
    		require('Module:TrackingCategory').append(working_frame,'含有未被索引CAS號的條目')
    		return input_str
    	else return mw.ustring.sub(input_str,start,-1)
    	end
    else return magic_str
    end
end


--{{#invoke:沙盒/a2569875/Test3|findArgs}}
function p.findArgs(frame)
	-- For calling from #invoke.
    local args
    --是否存在有效的frame可供呼叫
    local can_call_parse = false
    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 = frame.args
        can_call_parse = true
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
    end
    local out_frame = mw.getCurrentFrame():getParent() --模板外層
    local out_args = {} --紀錄來自模板外層提供的參數
    local body = ''
    local to_find={}
    local default=''
    
    if out_frame then out_args = out_frame.args or out_args end
    for key,val in pairs(args) do
    	local parse_name=tonumber(key)
    	if parse_name then to_find[#to_find+1]=val end
    end
    default = args.default or default
    local find_result={}
    local is_find=false
    for key,val in pairs(out_args) do
    	for i=1,#to_find do
	    	if mw.ustring.lower(mw.text.trim(val)) == mw.ustring.lower(mw.text.trim(to_find[i])) then
	    		is_find=true
	    		find_result[mw.text.trim(key)]=is_find;
	    	end
    	end
    end
    if is_find then
    	local body = ''
    	for key,val in pairs(find_result) do 
    		if body~='' then body=body..','end
    		body=body..key
    	end
    	return body
    else
    	return default
    end
end

return p