模块:Dictionary/Vocabulary.lua:修订间差异
来自「荏苒之境」
无编辑摘要 |
无编辑摘要 |
||
第13行: | 第13行: | ||
:node(html.create("p"):wikitext("本页面包含的词汇如下:"))) | :node(html.create("p"):wikitext("本页面包含的词汇如下:"))) | ||
local words = | local words = view:tag("table") | ||
local word_header = | local word_header = words.tag("tr") | ||
for i = 1, #header do | for i = 1, #header do | ||
word_header:node(html.create("th"):wikitext(header[i])) | word_header:node(html.create("th"):wikitext(header[i])) | ||
第24行: | 第22行: | ||
for i = 1, #rows do | for i = 1, #rows do | ||
local row = rows[i] | local row = rows[i] | ||
local word_row = | local word_row = words.tag("tr") | ||
for j = 1, #row do | for j = 1, #row do | ||
local td = | local td = word_row.tag("td") | ||
if j == 1 then | if j == 1 then | ||
td:node(html.create("b"):wikitext(row[j])) | td:node(html.create("b"):wikitext(row[j])) |
2025年8月2日 (六) 02:58的版本
此模块的文档可以在模块:Dictionary/Vocabulary.lua/doc创建
local csv = require("Module:Csv.lua")
local cargo = mw.ext.cargo
local html = mw.html
local section = {}
local function render(language, header, rows)
local view = html.create("div")
:attr("id", "dictionary_"..language)
:node(html.create("div")
:attr("class", "dictionary-header")
:node(html.create("h2"):wikitext("词典:"..language))
:node(html.create("p"):wikitext("本页面包含的词汇如下:")))
local words = view:tag("table")
local word_header = words.tag("tr")
for i = 1, #header do
word_header:node(html.create("th"):wikitext(header[i]))
end
for i = 1, #rows do
local row = rows[i]
local word_row = words.tag("tr")
for j = 1, #row do
local td = word_row.tag("td")
if j == 1 then
td:node(html.create("b"):wikitext(row[j]))
else
td:wikitext(row[j])
end
end
end
return view
end
section.show = function(frame)
local args = frame.args
local language = args.language
local format = args.format
local content = args.content
local table_name = "Dict_"..language
local header
local rows
if format == "csv" then
rows = csv.parse(content, true)
header = rows.header
else
mw.addWarning("无效的词典格式:"..tostring(format))
return
end
local decl = { _table = table_name }
for i = 1, #header do
decl[header[i]] = "String";
end
cargo.declare(decl)
for i = 1, #rows do
local entry = {}
local row = rows[i]
for j = 1, #row do
entry[header[j]] = row[j]
end
cargo.store(table_name, entry)
end
return render(language, header, rows)
end
return section