(WIP)
 
편집 요약 없음
1번째 줄: 1번째 줄:
parse = require("Module:Yaml").parse
p = {}
p = {}
wiki = {}
function isList(t)
if t[1] == nil then
return false
else
return true
end
end
function size(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
function wiki.attr(t)
local str = ""
for k, v in pairs(t or {}) do
str = k .. "='" .. v .. "' " .. str
end
return str
end


function shallow_copy(t)
function wiki.td(str, attr)
  local t2 = {}
if attr ~= nil and size(attr) > 0 then
  for k,v in pairs(t) do
str = wiki.attr(attr) .. "| " .. str
    t2[k] = v
end
  end
return "\n| " .. str
  return t2
end
end


function p.parse(str)
function wiki.link(str)
local ctx = {}
return "[[" .. str .. "]]"
ctx.indent = -2
end
ctx.depth = -2
 
ctx.path = {}
function wiki.table(str, attr)
ctx.obj = {}
return "{| " .. wiki.attr(attr) .. str .. "\n|}"
result = {}
end
for line in str:gmatch("[^\r\n]+") do
 
indent, bar, text, colon = line:match("^(%s*)(-?)%s*([^:]+)(:?)%s*$")
function render(t, depth)
local result = ""
if ctx.indent > indent:len() then
for k, v in pairs(t) do
if indent:len() < ctx.depth then
if isList(v) then
result = {[ctx.path[#ctx.path]] = result}
result = result .. wiki.td(wiki.link(k)) .. wiki.td("[[" .. table.concat(v, "]] · [[") .. "]]") .. "\n|-"
ctx.depth = ctx.depth - 2
elseif type(v) == "table" then
else
result = result .. wiki.td(wiki.link(k), {rowspan = size(v)}) .. render(v, depth + 1)
result[ctx.path[#ctx.path]] = ctx.obj
ctx.depth = indent:len()
ctx.obj = {}
end
table.remove(ctx.path)
end
end
if colon ~= "" then
table.insert(ctx.path, text)
elseif bar ~= "" then
table.insert(ctx.obj, text)
end
ctx.indent = indent:len()
mw.logObject(ctx)
end
for i = #ctx.path, 1, -1 do
result[ctx.path[i]] = ctx.obj
ctx.obj = {}
table.remove(ctx.path)
mw.logObject(ctx)
end
end
return result
return result
end
end


function p.main(yaml)
return wiki.table(
render(parse(yaml), 0),
{class = "wikitable", style = "width: 100%;"}
)
end
p.parse = parse
return p
return p

2022년 8월 13일 (토) 13:58 판

이 모듈에 대한 설명문서는 모듈:Temflake/설명문서에서 만들 수 있습니다

parse = require("Module:Yaml").parse

p = {}
wiki = {}

function isList(t)
	if t[1] == nil then
		return false
	else
		return true
	end
end

function size(t)
	local count = 0
	for _ in pairs(t) do count = count + 1 end
	return count
end

function wiki.attr(t)
	local str = ""
	for k, v in pairs(t or {}) do
		str = k .. "='" .. v .. "' " .. str
	end
	return str
end

function wiki.td(str, attr)
	if attr ~= nil and size(attr) > 0 then
		str = wiki.attr(attr) .. "| " .. str
	end
	return "\n| " .. str
end

function wiki.link(str)
	return "[[" .. str .. "]]"
end

function wiki.table(str, attr)
	return "{| " .. wiki.attr(attr) .. str .. "\n|}"
end

function render(t, depth)
	local result = ""
	for k, v in pairs(t) do
		if isList(v) then
			result = result .. wiki.td(wiki.link(k)) .. wiki.td("[[" .. table.concat(v, "]] · [[") .. "]]") .. "\n|-"
		elseif type(v) == "table" then
			result = result .. wiki.td(wiki.link(k), {rowspan = size(v)}) .. render(v, depth + 1)
		end
	end
	return result
end

function p.main(yaml)
	return wiki.table(
		render(parse(yaml), 0),
		{class = "wikitable", style = "width: 100%;"}
	)
end

p.parse = parse
return p