Module:CastleLD

From Jcastle.info
Revision as of 21:02, 19 July 2025 by Eric (talk | contribs) (Created page with "local p = {} function p.getJSONLD(frame) local castleData = frame:getParent().args -- Split GPS local lat, lon = string.match(castleData["GPSLocation"] or "", "([%d%.%-]+)[,%s]+([%d%.%-]+)") -- Build 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.inse...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:CastleLD/doc

local p = {}

function p.getJSONLD(frame)
	local castleData = frame:getParent().args

	-- Split GPS
	local lat, lon = string.match(castleData["GPSLocation"] or "", "([%d%.%-]+)[,%s]+([%d%.%-]+)")

	-- Build 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

	local data = {
		["@context"] = "https://schema.org",
		["@type"] = "LandmarksOrHistoricalBuildings",
		name = castleData["English Name"] or "",
		alternateName = altNames,
		description = castleData["History"] or "",
		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 = castleData["Website"] or "",
		isAccessibleForFree = false,
		openingHours = "Mo-Su 09:00-17:00",
		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 ""}
		}
	}

	local json = mw.text.jsonEncode(data, mw.text.JSON_PRETTY)
	return frame:extensionTag("script", json, { type = "application/ld+json" })
end

return p