Module:CastleLD: Difference between revisions
From Jcastle.info
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
local function escapeForMediaWiki(url) | |||
-- Replace < and > so MW doesn't autolink | |||
return url:gsub("<", "<"):gsub(">", ">") | |||
local function | |||
-- | |||
end | end | ||
function p.getJSONLD(frame) | function p.getJSONLD(frame) | ||
local castleData = frame:getParent().args | local castleData = frame:getParent().args | ||
local lat, lon = string.match(castleData["GPSLocation"] or "", "([%d%.%-]+)[,%s]+([%d%.%-]+)") | local lat, lon = string.match(castleData["GPSLocation"] or "", "([%d%.%-]+)[,%s]+([%d%.%-]+)") | ||
| Line 29: | Line 14: | ||
local pageUrl = "https://jcastle.info/view/" .. mw.uri.encode(castleData["English Name"] or "", "WIKI") | local pageUrl = "https://jcastle.info/view/" .. mw.uri.encode(castleData["English Name"] or "", "WIKI") | ||
local websiteUrl = castleData["Website"] or "" | |||
local schemaUrl = "https://schema.org" | |||
local data = { | local data = { | ||
["@context"] = | ["@context"] = escapeForMediaWiki(schemaUrl), | ||
["@type"] = "LandmarksOrHistoricalBuildings", | ["@type"] = "LandmarksOrHistoricalBuildings", | ||
["@id"] = pageUrl, | ["@id"] = escapeForMediaWiki(pageUrl), | ||
name = castleData["English Name"] or "", | name = castleData["English Name"] or "", | ||
alternateName = altNames, | alternateName = altNames, | ||
| Line 54: | Line 41: | ||
}, | }, | ||
temporalCoverage = castleData["Historical Period"] or "", | temporalCoverage = castleData["Historical Period"] or "", | ||
url = pageUrl, | url = escapeForMediaWiki(pageUrl), | ||
tourBookingPage = | tourBookingPage = escapeForMediaWiki(websiteUrl), | ||
additionalProperty = { | additionalProperty = { | ||
{["@type"]="PropertyValue", name="Castle Condition", value=castleData["Castle Condition"] or ""}, | {["@type"]="PropertyValue", name="Castle Condition", value=castleData["Castle Condition"] or ""}, | ||
| Line 71: | Line 58: | ||
return json | return json | ||
end | end | ||
Revision as of 00:05, 20 July 2025
Documentation for this module may be created at Module:CastleLD/doc
local function escapeForMediaWiki(url)
-- Replace < and > so MW doesn't autolink
return url:gsub("<", "<"):gsub(">", ">")
end
function p.getJSONLD(frame)
local castleData = frame:getParent().args
local lat, lon = string.match(castleData["GPSLocation"] or "", "([%d%.%-]+)[,%s]+([%d%.%-]+)")
local altNames = {}
if castleData["Japanese Name"] then table.insert(altNames, castleData["Japanese Name"]) end
if castleData["Romaji Name"] then table.insert(altNames, castleData["Romaji Name"]) end
if castleData["Alternate Names"] then table.insert(altNames, castleData["Alternate Names"]) end
local pageUrl = "https://jcastle.info/view/" .. mw.uri.encode(castleData["English Name"] or "", "WIKI")
local websiteUrl = castleData["Website"] or ""
local schemaUrl = "https://schema.org"
local data = {
["@context"] = escapeForMediaWiki(schemaUrl),
["@type"] = "LandmarksOrHistoricalBuildings",
["@id"] = escapeForMediaWiki(pageUrl),
name = castleData["English Name"] or "",
alternateName = altNames,
description = sanitizeText(castleData["History"]),
founder = {
["@type"] = "Person",
name = castleData["Founder"] or ""
},
foundingDate = castleData["Year Founded"] or "",
geo = {
["@type"] = "GeoCoordinates",
latitude = tonumber(lat),
longitude = tonumber(lon)
},
address = {
["@type"] = "PostalAddress",
addressLocality = castleData["City"] or "",
addressRegion = castleData["Prefecture"] or "",
addressCountry = "Japan"
},
temporalCoverage = castleData["Historical Period"] or "",
url = escapeForMediaWiki(pageUrl),
tourBookingPage = escapeForMediaWiki(websiteUrl),
additionalProperty = {
{["@type"]="PropertyValue", name="Castle Condition", value=castleData["Castle Condition"] or ""},
{["@type"]="PropertyValue", name="Castle Type", value=castleData["Castle Type"] or ""},
{["@type"]="PropertyValue", name="Designations", value=castleData["Designations"] or ""},
{["@type"]="PropertyValue", name="Main Keep Structure", value=castleData["Main Keep Structure"] or ""},
{["@type"]="PropertyValue", name="Artifacts", value=castleData["Artifacts"] or ""},
{["@type"]="PropertyValue", name="Features", value=castleData["Features"] or ""},
{["@type"]="PropertyValue", name="Access", value=castleData["Access"] or ""},
{["@type"]="PropertyValue", name="Visitor Information", value=castleData["Visitor Information"] or ""},
{["@type"]="PropertyValue", name="Time Required", value=castleData["Time Required"] or ""}
}
}
local json = mw.text.jsonEncode(data, mw.text.JSON_PRETTY)
return json
end
