모듈:DemoTemplate

From 더 스토리즈 대피소
Jump to navigation Jump to search

이 모듈은 틀의 코드 및 그 결과를 쉽게 출력하는 것을 도와줍니다. 틀 사용시 틀의 이름 앞에 #invoke:DemoTemplate|를 붙이기만 하세요.

코드: {{#invoke:DemoTemplate|OCLC|22239204}}
결과: {{OCLC|22239204}}OCLC 22239204

이름이 있는 변수와 순서 상의 변수를 모두 지원합니다. 또한 값 내에 등호 기호가 있어도 작동됩니다.

코드: {{#invoke:DemoTemplate|인용|저자=홍길동|제목=길동이의 모험}}
결과: {{인용|제목=길동이의 모험|저자=홍길동}}홍길동, 《길동이의 모험》 
코드: {{#invoke:DemoTemplate|Remove first word|1=첫 번째 단어 없애기}}
결과: {{Remove first word|첫 번째 단어 없애기}} → 번째 단어 없애기

그러나 파이프 문자나 중괄호를 이용하면 정상적으로 작동하지 않습니다.

코드: {{#invoke:DemoTemplate|순환|4{{!}}n}}
결과: {{순환|4|n}} → nnnn

require('Module:No globals')

local newBuffer = require('Module:OutputBuffer')
local mt = {}

function mt.__index(t, title)
	return function(frame)
		local getBuffer, print, printf = newBuffer()
		printf('{{%s', title)
		local ipairsArgs = {}
		for k,v in ipairs(frame.args) do
			if string.find(v, '=', 1, true) then
				break
			end
			ipairsArgs[k] = true
			printf('|%s', v)
		end
		for k,v in pairs(frame.args) do
			if not ipairsArgs[k] then
				printf('|%s=%s', string.gsub(k, '=', '{{=}}'), v)
			end
		end
		print('}}')
		local buffer = getBuffer()
		-- rather than calling expandTemplate with the title and args we have, call preprocess, so that our code example will always match our output, even in the cases of pipes or other things we should have escaped but didn't
		return string.format('<code>%s</code> &rarr; %s', mw.text.nowiki(buffer), frame:preprocess(buffer))
	end
end

return setmetatable({}, mt)