Anonymous

Module:MwJson: Difference between revisions

From OSL Demo
1,848 bytes added ,  15 November 2023
Update package: OSW Core
(Update package: OSW Core)
(Update package: OSW Core)
Tags: Mobile edit Mobile web edit
Line 280: Line 280:
local smw_res = nil
local smw_res = nil
if (mode == p.mode.header) then
if (mode == p.mode.header) then
-- get the semantic properties by looking up the json keys in the json-ld context
smw_res = p.getSemanticProperties({jsonschema=jsonschema, jsondata=json_res_store.res, store=false, debug=debug})
smw_res = p.getSemanticProperties({jsonschema=jsonschema, jsondata=json_res_store.res, store=false, debug=debug})
-- embed json-ld in resulting html for search engine discovery
jsonld["@context"] = smw_res.context
jsonld["@context"] = smw_res.context
jsonld["@type"] = p.tableMerge(p.tablefy(jsonschema.schema_type), p.tablefy(jsonld["@type"])) --
jsonld["@type"] = p.tableMerge(p.tablefy(jsonschema.schema_type), p.tablefy(jsonld["@type"])) --
Line 303: Line 307:
for i, category in ipairs(schema_res.visited) do
for i, category in ipairs(schema_res.visited) do
if (mode == p.mode.footer) then category = schema_res.visited[max_index - i +1] end --reverse order for footer templates
if (mode == p.mode.footer) then category = schema_res.visited[max_index - i +1] end --reverse order for footer templates
local jsonschema = schema_res.jsonschemas[category]
local super_jsonschema = schema_res.jsonschemas[category]
local template = schema_res.templates[category]
local template = schema_res.templates[category]
if (template ~= nil) then
if (template ~= nil) then
Line 312: Line 316:
end
end
local child = frame:newChild{args=stripped_jsondata}
local child = frame:newChild{args=stripped_jsondata}
if ( template:sub(1, #"=") == "=" ) then template = "\n" .. template end -- add line break if template starts with heading (otherwise not rendered by mw parser)
wikitext = wikitext .. child:preprocess( template )
wikitext = wikitext .. child:preprocess( template )
elseif (mode == p.mode.header) then
elseif (mode == p.mode.header) then
local infobox_res = p.renderInfoBox({jsonschema=jsonschema, jsondata=jsondata})
local ignore_properties = {[p.keys.category]=true} -- don't render type/category on every subclass
for j, subcategory in ipairs(schema_res.visited) do
if j > i then
local subjsonschema = schema_res.jsonschemas[subcategory]
for k, v in pairs(subjsonschema['properties']) do
-- skip properties that are overwritten in subschemas, render them only once at the most specific position
ignore_properties[k] = true
end
end
end
-- render the infobox for the schema itself and every super_schema using always the global json-ld context (merged within walkJsonSchema())
-- context needs to be preprocessed with buildContext() since the generic json/table merge of the @context atttribute produces a list of strings (remote context) and context objects
local infobox_res = p.renderInfoBox({jsonschema=super_jsonschema, context=p.buildContext({jsonschema=jsonschema}).context, jsondata=jsondata, ignore_properties=ignore_properties})
wikitext = wikitext .. frame:preprocess( infobox_res.wikitext )
wikitext = wikitext .. frame:preprocess( infobox_res.wikitext )
end
end
Line 327: Line 344:
if (debug) then msg = msg .. "Store page properties" end
if (debug) then msg = msg .. "Store page properties" end
smw_res.properties['Display title of'] = display_label --set special property display title
smw_res.properties['Display title of'] = display_label --set special property display title
smw_res.properties['Display title of lowercase'] = display_label:lower() --store lowercase for case insensitive query
smw_res.properties['Display title of normalized'] = display_label:lower():gsub('[^%w]+','') --store with all non-alphanumeric chars removed for normalized query
mw.ext.displaytitle.set(display_label)
mw.ext.displaytitle.set(display_label)
--smw_res.properties['@category'] = jsondata[p.keys.category]
--smw_res.properties['@category'] = jsondata[p.keys.category]
Line 350: Line 369:
local jsondata = p.defaultArg(args.jsondata, {})
local jsondata = p.defaultArg(args.jsondata, {})
local schema = p.defaultArg(args.jsonschema, nil)
local schema = p.defaultArg(args.jsonschema, nil)
local context = p.buildContext({jsonschema=schema}).context
local ignore_properties = {[p.keys.category]=true} -- don't render type/category on every subclass
local res = ""
local res = ""
if schema == nil then return res end
if schema == nil then return res end
local context = p.defaultArg(args.context, p.buildContext({jsonschema=schema}).context)
local ignore_properties = p.defaultArg(args.ignore_properties, {})
local schema_label = ""
local schema_label = ""
if schema['title'] ~= nil then schema_label = schema['title'] end
if schema['title'] ~= nil then schema_label = schema['title'] end
Line 383: Line 404:
if (type(e) ~= 'table') then  
if (type(e) ~= 'table') then  
local p_type = p.defaultArgPath(context, {k, '@type'}, '@value')
local p_type = p.defaultArgPath(context, {k, '@type'}, '@value')
if (p_type == '@id') then e = "[[" .. string.gsub(e, "Category:", ":Category:") .. "]]" end
if (p_type == '@id') then  
e = string.gsub(e, "Category:", ":Category:") -- make sure category links work
e = string.gsub(e, "File:", ":File:") -- do not embedd images but link to them
e = "[[" .. e .. "]]"  
end
cell:wikitext("\n* " .. e .. "")  
cell:wikitext("\n* " .. e .. "")  
end
end
Line 389: Line 414:
else
else
local p_type = p.defaultArgPath(context, {k, '@type'}, '@value')
local p_type = p.defaultArgPath(context, {k, '@type'}, '@value')
if (p_type == '@id') then v = "[[" .. string.gsub(v, "Category:", ":Category:") .. "]]" end
if (p_type == '@id') then  
v = string.gsub(v, "Category:", ":Category:") -- make sure category links work
v = string.gsub(v, "File:", ":File:") -- do not embedd images but link to them
v = "[[" .. v .. "]]"  
end
cell:wikitext( v )
cell:wikitext( v )
end
end
Line 476: Line 505:
--]]
--]]


-- constructs a property specific local jsonld context
function p.buildContext(args)
function p.buildContext(args)
local schema = p.defaultArg(args.jsonschema, {})
local schema = p.defaultArg(args.jsonschema, {})
Line 496: Line 526:
local properties = p.defaultArg(schema.properties, {})
local properties = p.defaultArg(schema.properties, {})


-- build property context
for k,v in pairs(properties) do
for k,v in pairs(properties) do
local subcontext = nil
local subcontext = nil
Line 649: Line 680:
if (jsondata[p.keys.name] ~= nil) then properties['Display title of'] = jsondata[p.keys.name]  
if (jsondata[p.keys.name] ~= nil) then properties['Display title of'] = jsondata[p.keys.name]  
elseif (jsondata[p.keys.label] ~= nil and jsondata[p.keys.label][1] ~= nil) then properties['Display title of'] = p.splitString(jsondata[p.keys.label][1], '@')[1]  
elseif (jsondata[p.keys.label] ~= nil and jsondata[p.keys.label][1] ~= nil) then properties['Display title of'] = p.splitString(jsondata[p.keys.label][1], '@')[1]  
else properties['Display title of'] = p.defaultArg(parent_schema_property.schema_data['title'], "") end
else properties['Display title of'] = p.defaultArg(subschema['title'], "") end
if (p.tableLength(properties) > 0) then
if (p.tableLength(properties) > 0) then
store_res = mw.smw.subobject( properties, subobjectId ) --store as subobject
store_res = mw.smw.subobject( properties, subobjectId ) --store as subobject
Cookies help us deliver our services. By using our services, you agree to our use of cookies.