Module:CastleLD: Difference between revisions
From Jcastle.info
No edit summary |
No edit summary |
||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- | -- Helper to clean text of HTML, wiki syntax, and excess whitespace | ||
local function sanitizeText(input) | local function sanitizeText(input) | ||
if not input then return "" end | if not input then return "" end | ||
-- Remove HTML tags | -- Remove HTML tags | ||
input = mw.ustring.gsub(input, "<[^>]+>", "") | input = mw.ustring.gsub(input, "<[^>]+>", "") | ||
-- Remove wiki links, | -- Remove wiki links, preserve visible text | ||
input = mw.ustring.gsub(input, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2") | input = mw.ustring.gsub(input, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2") | ||
input = mw.ustring.gsub(input, "%[%[([^%]]+)%]%]", "%1") | input = mw.ustring.gsub(input, "%[%[([^%]]+)%]%]", "%1") | ||
-- Decode HTML entities | -- Decode HTML entities | ||
input = mw.text.decode(input) | input = mw.text.decode(input) | ||
-- Collapse whitespace | -- Collapse whitespace | ||
input = mw.ustring.gsub(input, "\r?\n", " ") | input = mw.ustring.gsub(input, "\r?\n", " ") | ||
input = mw.ustring.gsub(input, "%s+", " ") | input = mw.ustring.gsub(input, "%s+", " ") | ||
return mw.text.trim(input) | |||
end | end | ||
Line 21: | Line 20: | ||
local castleData = frame:getParent().args | local castleData = frame:getParent().args | ||
-- Extract lat/lon | |||
local lat, lon = string.match(castleData["GPSLocation"] or "", "([%d%.%-]+)[,%s]+([%d%.%-]+)") | local lat, lon = string.match(castleData["GPSLocation"] or "", "([%d%.%-]+)[,%s]+([%d%.%-]+)") | ||
-- Alternate names | |||
local altNames = {} | local altNames = {} | ||
if castleData["Japanese Name"] then table.insert(altNames, castleData["Japanese Name"]) end | if castleData["Japanese Name"] then table.insert(altNames, castleData["Japanese Name"]) end | ||
Line 28: | Line 29: | ||
if castleData["Alternate Names"] then table.insert(altNames, castleData["Alternate Names"]) end | if castleData["Alternate Names"] then table.insert(altNames, castleData["Alternate Names"]) end | ||
-- Build main JSON-LD structure | |||
local data = { | local data = { | ||
["@context"] = "https://schema.org", | ["@context"] = "https://schema.org", | ||
Line 52: | Line 54: | ||
temporalCoverage = castleData["Historical Period"] or "", | temporalCoverage = castleData["Historical Period"] or "", | ||
url = "https://jcastle.info/view/" .. mw.uri.encode(castleData["English Name"] or "", "WIKI"), | url = "https://jcastle.info/view/" .. mw.uri.encode(castleData["English Name"] or "", "WIKI"), | ||
tourBookingPage = castleData["Website"] | tourBookingPage = sanitizeText(castleData["Website"]), | ||
additionalProperty = { | additionalProperty = { | ||
{["@type"]="PropertyValue", name="Castle Condition", value=castleData["Castle Condition"] or ""}, | {["@type"]="PropertyValue", name="Castle Condition", value=castleData["Castle Condition"] or ""}, | ||
Line 63: | Line 65: | ||
{["@type"]="PropertyValue", name="Visitor Information", value=castleData["Visitor Information"] or ""}, | {["@type"]="PropertyValue", name="Visitor Information", value=castleData["Visitor Information"] or ""}, | ||
{["@type"]="PropertyValue", name="Time Required", value=castleData["Time Required"] or ""}, | {["@type"]="PropertyValue", name="Time Required", value=castleData["Time Required"] or ""}, | ||
{["@type"]="PropertyValue", name="Contributor", value=castleData["Contributor"] or ""} | |||
} | } | ||
} | } | ||
return mw.text.jsonEncode(data, mw.text.JSON_PRETTY) | |||
end | end | ||
return p | return p |
Latest revision as of 00:20, 20 July 2025
Documentation for this module may be created at Module:CastleLD/doc
local p = {} -- Helper to clean text of HTML, wiki syntax, and excess whitespace local function sanitizeText(input) if not input then return "" end -- Remove HTML tags input = mw.ustring.gsub(input, "<[^>]+>", "") -- Remove wiki links, preserve visible text input = mw.ustring.gsub(input, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2") input = mw.ustring.gsub(input, "%[%[([^%]]+)%]%]", "%1") -- Decode HTML entities input = mw.text.decode(input) -- Collapse whitespace input = mw.ustring.gsub(input, "\r?\n", " ") input = mw.ustring.gsub(input, "%s+", " ") return mw.text.trim(input) end function p.getJSONLD(frame) local castleData = frame:getParent().args -- Extract lat/lon local lat, lon = string.match(castleData["GPSLocation"] or "", "([%d%.%-]+)[,%s]+([%d%.%-]+)") -- Alternate names 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 -- Build main JSON-LD structure local data = { ["@context"] = "https://schema.org", ["@type"] = "LandmarksOrHistoricalBuildings", 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 = "https://jcastle.info/view/" .. mw.uri.encode(castleData["English Name"] or "", "WIKI"), tourBookingPage = sanitizeText(castleData["Website"]), 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 ""}, {["@type"]="PropertyValue", name="Contributor", value=castleData["Contributor"] or ""} } } return mw.text.jsonEncode(data, mw.text.JSON_PRETTY) end return p