模块:Dictionary-Vocabulary.lua:修订间差异
来自「荏苒之境」
无编辑摘要 |
无编辑摘要 |
||
第1行: | 第1行: | ||
local csv = require("Module:Csv.lua") | local csv = require("Module:Csv.lua") | ||
local dict_utils = require("Module:Dictionary-Utils.lua") | |||
local dict_views = require("Module:Dictionary-Views.lua") | |||
local html = mw.html | local html = mw.html | ||
local cargo = mw.ext.cargo | local cargo = mw.ext.cargo | ||
local DICT_TABLE = dict_views.TABLE | local DICT_TABLE = dict_views.TABLE | ||
local vocabulary = {} | local vocabulary = {} | ||
local function format_conflict(prev_value, curr_value) | |||
return (prev_value or "").."<br/><i style=\"color: grey\">"..(curr_value or "").."</i>" | |||
end | |||
local function store_row(language, priority, row, extra_count) | |||
local r = cargo.query(DICT_TABLE, "Spelling,Definition,Extra", { | |||
where = string.format( | |||
[[Language="%s" AND Spelling="%s" AND Priority>=%d]], | |||
language, row[1], tonumber(priority)) | |||
})[1] | |||
if r and r.Priority > priority then | |||
local conflicting | |||
local prev_row = dict_utils.expand_row(r, extra_count) | |||
for i = 1, extra_count and extra_count + 2 or math.max(#row, #prev_row) do | |||
if prev_row[i] ~= row[i] then | |||
prev_row[i] = format_conflict(prev_row[i], row[i]) | |||
conflicting = true | |||
end | |||
end | |||
return conflicting and prev_row or nil | |||
end | |||
local extra = {} | |||
for i = 1, extra_count do | |||
extra[i] = row[i+2] | |||
end | |||
cargo.store(DICT_TABLE, { | |||
Language = language, | |||
Spelling = row.Spelling, | |||
Definition = row.Definition, | |||
Extra = csv.format_row(extra), | |||
Priority = priority | |||
}) | |||
end | |||
vocabulary.show = function(frame) | vocabulary.show = function(frame) | ||
第17行: | 第55行: | ||
local priority = args.priority | local priority = args.priority | ||
local content = args.content | local content = args.content | ||
local rows | local rows | ||
if format == "csv" then | if format == "csv" then | ||
rows = csv.parse( | rows = csv.parse(content, true) | ||
else | else | ||
mw.addWarning("无效的词典格式:"..tostring(format)) | mw.addWarning("无效的词典格式:"..tostring(format)) | ||
return | return | ||
end | end | ||
local headers = rows.headers | |||
local extra_count = #headers - 2 | |||
local conflicting_headers = store_row(language, -math.huge, headers) | |||
if conflicting_headers then | |||
return html.create("pre") | |||
:css("color", "red") | |||
:tag("p") | |||
:wikitext("检测到该词汇表的标题行(灰色文本)与之前储存的标题行冲突,请修复:") | |||
:done() | |||
:node(dict_views.vocabulary { conflicting_headers }) | |||
end | |||
local conflicting_rows = {} | local conflicting_rows = {} | ||
for i = 2, #rows do | |||
for i = | local conflicting_row = store_row(language, priority, rows[i], extra_count) | ||
local | if conflicting_row then | ||
conflicting_rows[i-1] = conflicting_row | |||
if | |||
end | end | ||
end | end |
2025年8月4日 (一) 18:38的版本
此模块的文档可以在模块:Dictionary-Vocabulary.lua/doc创建
local csv = require("Module:Csv.lua")
local dict_utils = require("Module:Dictionary-Utils.lua")
local dict_views = require("Module:Dictionary-Views.lua")
local html = mw.html
local cargo = mw.ext.cargo
local DICT_TABLE = dict_views.TABLE
local vocabulary = {}
local function format_conflict(prev_value, curr_value)
return (prev_value or "").."<br/><i style=\"color: grey\">"..(curr_value or "").."</i>"
end
local function store_row(language, priority, row, extra_count)
local r = cargo.query(DICT_TABLE, "Spelling,Definition,Extra", {
where = string.format(
[[Language="%s" AND Spelling="%s" AND Priority>=%d]],
language, row[1], tonumber(priority))
})[1]
if r and r.Priority > priority then
local conflicting
local prev_row = dict_utils.expand_row(r, extra_count)
for i = 1, extra_count and extra_count + 2 or math.max(#row, #prev_row) do
if prev_row[i] ~= row[i] then
prev_row[i] = format_conflict(prev_row[i], row[i])
conflicting = true
end
end
return conflicting and prev_row or nil
end
local extra = {}
for i = 1, extra_count do
extra[i] = row[i+2]
end
cargo.store(DICT_TABLE, {
Language = language,
Spelling = row.Spelling,
Definition = row.Definition,
Extra = csv.format_row(extra),
Priority = priority
})
end
vocabulary.show = function(frame)
local args = frame.args
local language = args.language
local format = args.format
local priority = args.priority
local content = args.content
local rows
if format == "csv" then
rows = csv.parse(content, true)
else
mw.addWarning("无效的词典格式:"..tostring(format))
return
end
local headers = rows.headers
local extra_count = #headers - 2
local conflicting_headers = store_row(language, -math.huge, headers)
if conflicting_headers then
return html.create("pre")
:css("color", "red")
:tag("p")
:wikitext("检测到该词汇表的标题行(灰色文本)与之前储存的标题行冲突,请修复:")
:done()
:node(dict_views.vocabulary { conflicting_headers })
end
local conflicting_rows = {}
for i = 2, #rows do
local conflicting_row = store_row(language, priority, rows[i], extra_count)
if conflicting_row then
conflicting_rows[i-1] = conflicting_row
end
end
local view = html.create("div")
if #conflicting_rows ~= 0 then
view:tag("pre")
:css("color", "red")
:tag("p")
:wikitext("检测到旧词汇与新词汇(灰色文本)内容冲突的情况,且二者具有相同优先级,请统一词汇信息:")
:done()
:node(dict_views.vocabulary(conflicting_rows))
end
if frame.args.show_view == "true" then
view:node(dict_views.vocabulary(rows))
end
return view
end
return vocabulary