模块:Dictionary/Vocabulary.lua:修订间差异
来自「荏苒之境」
无编辑摘要 |
无编辑摘要 |
||
第9行: | 第9行: | ||
:attr("id", "dictionary_"..language) | :attr("id", "dictionary_"..language) | ||
:node(html.create("div") | :node(html.create("div") | ||
:attr("class", "dictionary-header") | |||
:node(html.create("h1"):wikitext("词典:"..language | :node(html.create("h1"):wikitext("词典:"..language))) | ||
local words = html.create("table") | local words = html.create("table") | ||
第27行: | 第26行: | ||
words:node(word_row) | words:node(word_row) | ||
for j = 1, #row do | for j = 1, #row do | ||
word_row:node(html.create(" | local td = html.create("td") | ||
word_row:node(td) | |||
if j == 1 then | |||
td:node(html.create("b"):wikitext(row[j])) | |||
else | |||
td:node(html:wikitext(row[j])) | |||
end | |||
end | end | ||
end | end |
2025年8月2日 (六) 02:46的版本
此模块的文档可以在模块: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("h1"):wikitext("词典:"..language)))
local words = html.create("table")
view:node(words)
local word_header = html.create("tr")
words:node(word_header)
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 = html.create("tr")
words:node(word_row)
for j = 1, #row do
local td = html.create("td")
word_row:node(td)
if j == 1 then
td:node(html.create("b"):wikitext(row[j]))
else
td:node(html: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