Модуль:Сущность/data: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| (не показаны 3 промежуточные версии этого же участника) | |||
| Строка 14: | Строка 14: | ||
if not s then return s end | if not s then return s end | ||
return (s:gsub("^%s*(.-)%s*$", "%1")) | return (s:gsub("^%s*(.-)%s*$", "%1")) | ||
end | |||
local function each_csv_value(str, fn) | |||
if not str or str == "" then | |||
return | |||
end | |||
for item in string.gmatch(str, "[^,]+") do | |||
local value = trim(item) | |||
if value ~= "" then | |||
fn(value) | |||
end | |||
end | |||
end | end | ||
| Строка 162: | Строка 174: | ||
local function makeSourceLink(s) | local function makeSourceLink(s) | ||
local className = | local className = | ||
(s.name:sub(1,1):upper() .. s.name:sub(2)) .. | (s.name:sub(1, 1):upper() .. s.name:sub(2)) .. | ||
(s.kind and (s.kind:sub(1,1):upper() .. s.kind:sub(2)) or "") | (s.kind and (s.kind:sub(1, 1):upper() .. s.kind:sub(2)) or "") | ||
local tplLabel = "Template:" .. s.tplPath | local tplLabel = "Template:" .. s.tplPath | ||
| Строка 336: | Строка 348: | ||
local section = (callKey:find("_", 1, true)) and callKey:match("^([^_]+)") or "Сущность" | local section = (callKey:find("_", 1, true)) and callKey:match("^([^_]+)") or "Сущность" | ||
local isWhitelisted = cardFilter and matches_card_list(cardFilter.whitelist, callKey, compositeKey) or false | local isWhitelisted = cardFilter and matches_card_list(cardFilter.whitelist, callKey, compositeKey) or | ||
local isBlacklisted = cardFilter and matches_card_list(cardFilter.blacklist, callKey, compositeKey) or false | false | ||
local isBlacklisted = cardFilter and matches_card_list(cardFilter.blacklist, callKey, compositeKey) or | |||
false | |||
if isWhitelisted and content ~= "" then | if isWhitelisted and content ~= "" then | ||
rawContentParts[#rawContentParts + 1] = content | rawContentParts[#rawContentParts + 1] = content | ||
end | end | ||
local allowCardEntry = not isWhitelisted and not isBlacklisted and | |||
((not cardFilter) or (not cardFilter.hasWhitelist) or | |||
matches_card_list(cardFilter.cardWhitelist, callKey, compositeKey)) | |||
if allowCardEntry and (displayLabel ~= "" or content ~= "") then | |||
if not merged.sectionsMap[section] then | if not merged.sectionsMap[section] then | ||
merged.sectionsMap[section] = true | merged.sectionsMap[section] = true | ||
| Строка 368: | Строка 386: | ||
end | end | ||
if | if allowCardEntry and tagText ~= "" then | ||
if not merged.tagSet[tagText] then | if not merged.tagSet[tagText] then | ||
merged.tagSet[tagText] = true | merged.tagSet[tagText] = true | ||
| Строка 377: | Строка 395: | ||
end | end | ||
end | end | ||
each_csv_value(frame.args.cardTag or "", function(extraTag) | |||
if not merged.tagSet[extraTag] then | |||
merged.tagSet[extraTag] = true | |||
table.insert(merged.tags, extraTag) | |||
end | |||
end) | |||
local out = {} | local out = {} | ||
| Строка 388: | Строка 413: | ||
local hasLabel = false | local hasLabel = false | ||
for _, v in pairs(merged.labelOverrides or {}) do | for _, v in pairs(merged.labelOverrides or {}) do | ||
if v and v ~= "" then hasLabel = true break end | if v and v ~= "" then | ||
hasLabel = true | |||
break | |||
end | |||
end | end | ||
if not hasLabel then | if not hasLabel then | ||
for _, lst in pairs(merged.labelLists or {}) do | for _, lst in pairs(merged.labelLists or {}) do | ||
if #lst > 0 then hasLabel = true break end | if #lst > 0 then | ||
hasLabel = true | |||
break | |||
end | |||
end | end | ||
end | end | ||
| Строка 398: | Строка 429: | ||
local hasContent = false | local hasContent = false | ||
for _, v in pairs(merged.contentByKey or {}) do | for _, v in pairs(merged.contentByKey or {}) do | ||
if v and v ~= "" then hasContent = true break end | if v and v ~= "" then | ||
hasContent = true | |||
break | |||
end | |||
end | end | ||
| Строка 531: | Строка 565: | ||
break | break | ||
end | end | ||
end | |||
if not filter.hasWhitelist and filter.whitelist.cardContent and next(filter.whitelist.cardContent) ~= nil then | |||
filter.hasWhitelist = true | |||
end | end | ||
return filter | return filter | ||
| Строка 539: | Строка 576: | ||
noHeaders = false, | noHeaders = false, | ||
cardFilter = { | cardFilter = { | ||
hasWhitelist = filter.whitelist.cardContent and next(filter.whitelist.cardContent) ~= nil or false, | |||
cardWhitelist = filter.whitelist.card or {}, | |||
blacklist = filter.blacklist.cardContent or {}, | blacklist = filter.blacklist.cardContent or {}, | ||
whitelist = filter.whitelist.cardContent or {} | whitelist = filter.whitelist.cardContent or {} | ||
| Строка 547: | Строка 586: | ||
local function should_include_key(filter, sw, key) | local function should_include_key(filter, sw, key) | ||
if filter.hasWhitelist then | if filter.hasWhitelist then | ||
if filter.whitelist[sw] and filter.whitelist[sw][key] then | |||
return true | |||
end | |||
if sw == "card" and filter.whitelist.cardContent and filter.whitelist.cardContent[key] then | |||
return true | |||
end | |||
return false | |||
end | end | ||
return not (filter.blacklist[sw] and filter.blacklist[sw][key]) | return not (filter.blacklist[sw] and filter.blacklist[sw][key]) | ||
end | end | ||
| Строка 589: | Строка 622: | ||
local function collect_entity_sets(id, componentDefs, prototypeStoreDefs, | local function collect_entity_sets(id, componentDefs, prototypeStoreDefs, | ||
componentWhitelist, componentBlacklist, prototypeWhitelist, prototypeBlacklist) | |||
local foundComponents, foundPrototypes = {}, {} | local foundComponents, foundPrototypes = {}, {} | ||
| Строка 653: | Строка 686: | ||
flattenExtraCache[cacheKey] = extra | flattenExtraCache[cacheKey] = extra | ||
return extra | return extra | ||
end | |||
local function add_card_tag_value(tags, seen, value) | |||
value = trim(value or "") | |||
if value == "" or seen[value] then | |||
return | |||
end | |||
seen[value] = true | |||
tags[#tags + 1] = value | |||
end | |||
local function merge_card_tag_text(...) | |||
local tags = {} | |||
local seen = {} | |||
for i = 1, select("#", ...) do | |||
each_csv_value(select(i, ...), function(value) | |||
add_card_tag_value(tags, seen, value) | |||
end) | |||
end | |||
return table.concat(tags, ", ") | |||
end | end | ||
| Строка 685: | Строка 740: | ||
end | end | ||
local function | local function each_entity_data(frame, id, onEntity, onMissing) | ||
local componentWhitelist = frame.args.componentWhitelist or frame.args.componentwhitelist or "" | |||
local componentBlacklist = frame.args.componentBlacklist or frame.args.componentblacklist or "" | |||
local prototypeWhitelist = frame.args.prototypeWhitelist or frame.args.prototypewhitelist or "" | |||
local prototypeBlacklist = frame.args.prototypeBlacklist or frame.args.prototypeblacklist or "" | |||
local | |||
local componentBlacklist = args.componentBlacklist or args.componentblacklist or "" | |||
local prototypeWhitelist = args.prototypeWhitelist or args.prototypewhitelist or "" | |||
local prototypeBlacklist = args.prototypeBlacklist or args.prototypeblacklist or "" | |||
local componentDefs = load_module_data("component.json") | local componentDefs = load_module_data("component.json") | ||
local prototypeStoreDefs = load_module_data("prototype_store.json") | local prototypeStoreDefs = load_module_data("prototype_store.json") | ||
if not componentDefs or not prototypeStoreDefs then return | if not componentDefs or not prototypeStoreDefs then | ||
return false | |||
end | |||
local foundComponents, foundPrototypes = collect_entity_sets(id, componentDefs, prototypeStoreDefs, | local foundComponents, foundPrototypes = collect_entity_sets(id, componentDefs, prototypeStoreDefs, | ||
componentWhitelist, componentBlacklist, prototypeWhitelist, prototypeBlacklist) | componentWhitelist, componentBlacklist, prototypeWhitelist, prototypeBlacklist) | ||
local ok, dp = pcall(require, "Module:GetField") | local ok, dp = pcall(require, "Module:GetField") | ||
local function processEntity(kind, name, isStore) | local function processEntity(kind, name, isStore) | ||
| Строка 757: | Строка 766: | ||
local content = load_template_content(tplPath) | local content = load_template_content(tplPath) | ||
if not content then | if not content then | ||
if | if onMissing then | ||
onMissing(kind, name, isStore, tplPath) | |||
end | end | ||
return | return | ||
end | end | ||
| Строка 776: | Строка 784: | ||
end | end | ||
onEntity(parsed, { | |||
tplPath = tplPath, | tplPath = tplPath, | ||
id = id, | id = id, | ||
| Строка 782: | Строка 790: | ||
source = make_source(kind, name, pathName, tplPath), | source = make_source(kind, name, pathName, tplPath), | ||
priority = resolve_priority(parsed) | priority = resolve_priority(parsed) | ||
} | }) | ||
end | end | ||
| Строка 810: | Строка 818: | ||
end | end | ||
end | end | ||
return true | |||
end | |||
local function collect_card_tag_text(frame, args, id) | |||
local filter = build_key_filter(args) | |||
local cardFilter = build_render_options(filter).cardFilter | |||
local entries = {} | |||
local ok = each_entity_data(frame, id, function(parsed, ctx) | |||
local keys = parsed.card or {} | |||
if type(keys) == "table" then | |||
for _, key in ipairs(keys) do | |||
local compositeKey = (key:find("_", 1, true)) and key or ("Сущность_" .. key) | |||
local isWhitelisted = matches_card_list(cardFilter.whitelist, key, compositeKey) | |||
local isBlacklisted = matches_card_list(cardFilter.blacklist, key, compositeKey) | |||
local allowCardEntry = not isWhitelisted and not isBlacklisted and | |||
((not cardFilter.hasWhitelist) or matches_card_list(cardFilter.cardWhitelist, key, compositeKey)) | |||
if allowCardEntry then | |||
entries[#entries + 1] = { | |||
tplTag = makeTplCall(ctx.tplPath, "cardTag", key, ctx.id, ctx.extra), | |||
priority = ctx.priority, | |||
idx = #entries + 1 | |||
} | |||
end | |||
end | |||
end | |||
if parsed.cardTag and parsed.cardTag ~= "" then | |||
entries[#entries + 1] = { | |||
tplTag = makeTplCall(ctx.tplPath, "cardTag", "cardTag", ctx.id, ctx.extra), | |||
priority = ctx.priority, | |||
idx = #entries + 1 | |||
} | |||
end | |||
end) | |||
if not ok then | |||
return trim(args.tag or "") | |||
end | |||
sort_entries_by_priority(entries) | |||
local tags = {} | |||
local seen = {} | |||
for _, entry in ipairs(entries) do | |||
local tagText = trim(frame:preprocess(entry.tplTag or "") or "") | |||
add_card_tag_value(tags, seen, tagText) | |||
end | |||
return merge_card_tag_text(table.concat(tags, ", "), args.tag) | |||
end | |||
p.mergeCardTagText = merge_card_tag_text | |||
p.collectCardTagText = collect_card_tag_text | |||
local function build_missing_template_error(kind, name, isStore, tplPath) | |||
local baseType = (kind and (kind:sub(1, 1):upper() .. kind:sub(2)) or "") | |||
local classType = baseType | |||
if isStore then | |||
classType = classType .. "Store" | |||
end | |||
local className = name .. baseType | |||
local tplLabel = "Template:" .. tplPath | |||
return "{{сущность/infobox/base|тип=" .. classType .. "|название=" .. className .. "|ссылка=" .. tplLabel .. "}}" | |||
end | |||
local function renderBlocks(frame, state, renderOptions, entityId, showSource) | |||
local outLocal = {} | |||
local noHeaders = renderOptions and renderOptions.noHeaders | |||
local cardFilter = renderOptions and renderOptions.cardFilter | |||
for _, sw in ipairs(switchModeOrder) do | |||
local mode = switchModeRegistry[sw] or {} | |||
if mode.full then | |||
local outStr = "" | |||
if type(mode.render_full) == "function" then | |||
outStr = mode.render_full(frame, state.keyOrder[sw], state.keyToTemplates[sw], state.keySources[sw], | |||
entityId, noHeaders, showSource, cardFilter) | |||
end | |||
if outStr and outStr ~= "" then table.insert(outLocal, outStr) end | |||
else | |||
for _, key in ipairs(state.keyOrder[sw] or {}) do | |||
local entries = state.keyToTemplates[sw][key] or {} | |||
if type(mode.render_key) == "function" then | |||
local outStr = mode.render_key(frame, key, entries, noHeaders, showSource) | |||
if outStr and outStr ~= "" then table.insert(outLocal, outStr) end | |||
end | |||
end | |||
end | |||
end | |||
return outLocal | |||
end | |||
function p.get(frame) | |||
local args = getArgs(frame, { removeBlanks = false }) | |||
local id = args[1] or "" | |||
if id == "" then return "" end | |||
local showSource = trim(args.showSource or "") == "" | |||
local filter = build_key_filter(args) | |||
local renderOptions = build_render_options(filter) | |||
local state = new_switch_state() | |||
local errors = {} | |||
local ok = each_entity_data(frame, id, function(parsed, ctx) | |||
add_entries_from_meta(state, parsed, ctx, filter, false) | |||
end, function(kind, name, isStore, tplPath) | |||
if not filter.hasWhitelist then | |||
errors[#errors + 1] = build_missing_template_error(kind, name, isStore, tplPath) | |||
end | |||
end) | |||
if not ok then return "" end | |||
local out = {} | local out = {} | ||