Edge Rewrite
Jump to content

Module:Deprecated archive/no nowiki demo

From Wikipedia, the free encyclopedia
--[=[
Experimental display-only version of {{Deprecated archive}}.

This module intentionally removes both mw.text.decode() and mw.text.nowiki()
from the title path. It is not intended for production use: title text can
create links or introduce HTML when the returned wikitext is parsed.

The demo() entry point renders the current Lua testcase corpus side by side
with the production module and this pass-through version.
]=]

require('strict')

local p = {}

local tracking_category = '[[Category:Deprecated archive template links]]'
local positional_parameter_error =
	'<span style="font-size:100%" class="error citation-comment">(Positional parameters ignored)</span>'

local function trim(value)
	if value == nil then
		return ''
	end
	return mw.text.trim(tostring(value))
end

local function is_set(value)
	return trim(value) ~= ''
end

local function get_args(frame)
	local args = {}
	local parent = frame and frame:getParent()

	if parent then
		for key, value in pairs(parent.args) do
			if is_set(value) then
				args[key] = value
			end
		end
	end

	if frame then
		for key, value in pairs(frame.args) do
			if is_set(value) then
				args[key] = value
			end
		end
	end

	return args
end

local function removed_link_marker()
	return '<sup>&#91;[[WP:ATODAY|link removed]]&#93;</sup>'
end

local function removed_link_label(label)
	if trim(label) == '' then
		return removed_link_marker()
	end

	-- Deliberately do not decode or escape label. This is the behavior under
	-- investigation, not a safe replacement for Module:Deprecated archive.
	return '<span style="text-decoration: underline dashed #ccc;">' ..
		label ..
		'</span>' .. removed_link_marker()
end

local function has_positional_parameters(args)
	for key, _ in pairs(args) do
		if type(key) == 'number' then
			return true
		end
	end
	return false
end

function p._main(args)
	local archive = trim(args.url)
	local label = trim(args.title)
	local positional_error = has_positional_parameters(args) and positional_parameter_error or ''

	-- Keep the archive URL as non-rendered metadata, matching the production module.
	local _ = { archive = archive }

	return removed_link_label(label) .. positional_error .. tracking_category
end

function p.main(frame)
	return p._main(get_args(frame))
end

local current = require('Module:Deprecated archive')

local cases = {
	{ 'entity: Post Office Location', 'Post Office Location&nbsp;– James A. Farley' },
	{ 'entity: Inkjet Printing', 'Inkjet Printing&nbsp;– Process and Its Applications' },
	{ 'entity: GCT-PH1', 'GCT-PH1: Population, Housing Units, Area, and Density: 2010 - County &ndash; County Subdivision and Place from the 2010 Census Summary File 1 for Burlington County, New Jersey' },

	{ 'italic apostrophe markup', "''Price Predictions: With present inflation at nearly triple expectation, economists challenge 2009 forecast''" },
	{ 'italic apostrophe markup around non-Latin text', "''Вхутемас''" },
	{ 'bold apostrophe markup', "'''Bold-looking source title'''" },
	{ 'mixed bold-italic apostrophe markup', "'''''Bold italic-looking source title'''''" },
	{ 'ordinary single apostrophes', "Gods, Ghosts, and Shelley's 'Atheos'" },
	{ 'double quotes and apostrophes', 'Jason Leopold "DOJ Report Says Yoo\'s Torture Memo Failed To Cite Supreme Court Case"' },

	{ 'HTTP URL text', 'Papercore Summary http://papercore.org/Sherrington1975' },
	{ 'www URL text', 'Hungary–Malaysia relations on www.kln.gov.my' },
	{ 'wikilink-looking text', 'literal [[Example]] title' },
	{ 'HTML-looking text', 'Title <i>not italic</i>' },

	{ 'done: named entity', 'The Catacombs&mdash;Brian Johnson' },
	{ 'done: quoted entity', '"120&nbsp;mm DM 33A1 and DM 43A1 APFSDS-T rounds (Germany)"' },
	{ 'done: italic markup and entity', [=[Timothy K. Blauvelt. ''Patronage and betrayal in the post-Stalin succession: the case of Kruglov and Serov.'' Communist and Post-Communist Studies, Volume 41, Issue 1, March 2008, Pages 105&ndash;120. Quote from p. 117: "He died in obscurity."]=] },
	{ 'done: template pipe marker', 'Roman sites by list {{!}} Spain {{!}} Italica' },
	{ 'done: convert template', 'Natural England Unit 8 of the Cotswold Lakes, the {{convert|8|ha|adj=on}} Whelford Pool (most northerly)' },
	{ 'done: numeric entity', "&#91;Issue&#93; Psy Unveils BTS Video of Noh Hong-chul's Pelvis Thrusting" },
	{ 'done: em dash and non-breaking space', 'Pastscape&nbsp;— Detailed Result: BADDESLEY HOSPITALLERS PRECEPTORY' },
	{ 'done: non-Latin entity', 'Указ Президента Российской Федерации от 2 апреля 2010 г. №&nbsp;412' },
	{ 'done: in lang template', 'Regional Archive, Leiden: Aantekeningen van Mr. P.J. Idenburg betreffende het Leids Universitair verzet (Notes by Mr. P.J. Idenburg regarding the Leiden University resistance) {{in lang|nl}}' },
	{ 'done: entity with en dash', 'The greatest race&nbsp;– Telegraph' },
	{ 'done: italic title with entity', "''New&nbsp;Zealand Gazette''" },
	{ 'done: quote template', [=[{{"'}}Once', 'Clybourne Park', 'Porgy and Bess', Audra McDonald, 'Salesman' Win Tony Awards]=] },
	{ 'done: quote template and apostrophe', [=[{{"'}}A dark glory': The Divinely Violent Woman from Shelley to Hawthorne]=] },
	{ 'done: spaced dash template', [=["Statistics of the European Cities{{spaced ndash}} City of Plovdiv]=] },
	{ 'done: language template', [=["Adevărul despre {{Lang|ro|Adevărul}}" ]=] },
	{ 'done: angle-bracket text', 'A Testimonial From the Cardinal Bird :: <blank>' },

	-- These cases are in the sandbox module's production-comparison test and
	-- are included even where the direct production cases above overlap them.
	{ 'sandbox comparison: Brown University', 'Dara Khosrowshahi: Executive Profile & Biography' },
	{ 'sandbox comparison: German cuisine', 'Guide to German Sausages & Meat Products' },
	{ 'sandbox comparison: Alps', "Gods, Ghosts, and Shelley's 'Atheos'" },
	{ 'sandbox comparison: Abu Zubaydah', 'Jason Leopold "DOJ Report Says Yoo\'s Torture Memo Failed To Cite Supreme Court Case"' },
	{ 'sandbox comparison: Economy of Armenia', "''Price Predictions: With present inflation at nearly triple expectation, economists challenge 2009 forecast''" },
	{ 'sandbox comparison: Bauhaus', "''Вхутемас''" },
	{ 'sandbox comparison: Ainu people', '市史編さんニュース №100 ヌプンケシ' },
	{ 'sandbox comparison: Amphipolis', 'Bulletin de Correspondance Hellénique' },
	{ 'sandbox comparison: David Thompson', "David Thompson's narrative, 1784–1812" },
	{ 'sandbox comparison: Foreign relations of Hungary', 'Hungary–Malaysia relations on www.kln.gov.my' },
	{ 'sandbox comparison: Spin glass', 'Papercore Summary http://papercore.org/Sherrington1975' },
	{ 'sandbox comparison: Natacha Atlas', 'Ana Hina – Natacha Atlas & The Mazeeka Ensemble (World Village)' },
}

local function without_tracking_category(text)
	return (text:gsub('%[%[Category:[^%]]+%]%]$', ''))
end

local function render(module, title)
	return without_tracking_category(module._main({
		url = 'https://blacklisted.example.com/demo',
		title = title,
	}))
end

local function table_cell(value, class)
	return '<td' .. (class and ' class="' .. class .. '"' or '') .. '>' .. value .. '</td>'
end

function p.demo()
	local output = {
		'<table class="wikitable">',
		'<caption>Deprecated archive title pass-through demo</caption>',
		'<tr><th>Case</th><th>Title source</th><th>Current Lua escaping</th><th>Raw title pass-through</th></tr>',
	}

	for index, case in ipairs(cases) do
		local title = case[2]
		output[#output + 1] = '<tr>' ..
			table_cell(tostring(index) .. '. ' .. case[1], 'case') ..
			table_cell('<code>' .. mw.text.nowiki(title) .. '</code>', 'source') ..
			table_cell(render(current, title), 'result') ..
			table_cell(render(p, title), 'result') ..
			'</tr>'
	end

	output[#output + 1] = '</table>'
	return table.concat(output)
end

return p