모듈:Pipeline

리버티게임, 모두가 만들어가는 자유로운 게임
Hsl0 (토론 | 기여)님의 2023년 11월 26일 (일) 14:33 판


모듈 설명문서[보기] [편집] [역사] [새로 고침]

이전 값에 종속되는 여러 틀을 연속적으로 실행합니다. 이를 통해 복잡한 로직을 함수형으로 구현하거나 중첩된 틀을 가독성 좋게 바꿀 수 있습니다.

사용법

code_blocks 코드
{{#invoke:Pipeline|pipe |{{#invoke:pipeline|pass|9|a=10}}11 |<nowiki>{{#expr:{{{1}}}+{{{a}}}+{{RETURNED}}}}</nowiki> }}
code
낙서장에서 확인
description 결과
수식 오류: +의 피연산자가 없습니다.

이전에 실행된 함수의 값을 RETURNED 상수를 통해 통째로 다음 함수에 넘겨줍니다. pass 함수를 활용하여 여러 값을 넘겨줄 수 있습니다.


local p = {}
local tf = require('모듈:TemplateFunction')
local passSuffix = 'pipe-passed-data'
local passSuffixPattern = 'pipe%-passed%-data'

function p.pipe(frame)
	local returned = ''
	local passed = nil
	
	for _, source in ipairs(frame.args) do
		local title = mw.title.new(source, 'Template')
		source = mw.text.unstripNoWiki(mw.text.decode(source)):gsub('{{RETURNED}}', function(default)
			return returned or ''
		end)
		if title and title.exists then
			returned = tf.load(source):parse(passed)
		else
			returned = tf.create(source):parse(passed)
			--returned = returned:gsub('<includeonly ' .. passSuffixPattern .. '>.-</includeonly ' .. passSuffixPattern .. '>', '')
			passed = source:match('<includeonly ' .. passSuffixPattern .. '>(.-)</includeonly ' .. passSuffixPattern.. '>')
			passed = passed and mw.text.jsonDecode(passed)
		end
	end
	
	return returned
end

function p.pass(frame)
	return '<noinclude ' .. passSuffix .. '>'
		.. mw.text.jsonEncode(frame.args)
		.. '</noinclude ' .. passSuffix .. '>'
end

return p