Module:R:Perseus: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
No edit summary
No edit summary
Line 65: Line 65:
local word = args['w'] or args[2] or args[1] or mw.title.getCurrentTitle().text
local word = args['w'] or args[2] or args[1] or mw.title.getCurrentTitle().text
local word_display = args['w'] or (not greek and args[1]) or (is_polytonic(args[1]) and args[1]) or mw.title.getCurrentTitle().text
local word_display = args['w'] or (not greek and args[1]) or (is_polytonic(args[1]) and args[1]) or mw.title.getCurrentTitle().text
local beta_or_latin = greek and (args['w'] or (args[1] and not is_polytonic(args[1])) and word) or (greek and beta(word)) or word
local beta_or_latin = greek and ((not is_polytonic(args[1]) and args[1]) or args['w'] and word) or (greek and beta(word)) or word
local redirect = is_collision(word,template) and not args[1] and not args['w']
local redirect = is_collision(word,template) and not args[1] and not args['w']
return word_display == template and "" or format_perseus_wikilink(word_display, beta_or_latin, template, redirect)
return word_display == template and "" or format_perseus_wikilink(word_display, beta_or_latin, template, redirect)

Revision as of 15:08, 18 October 2016

This module implements {{R:Autenrieth}}, {{R:Elementary Lewis}}, {{R:Harpocration}}, {{R:L&S}}, {{R:LSJ}}, {{R:Middle Liddell}}, {{R:Peck}}, {{R:PersEnc}}, {{R:Platner}}, {{R:Slater}}, {{R:Smith's Antiquities}}, {{R:Smith's Geography}}, {{R:Smith's Persons}}, {{R:Stillwell}}, and {{R:Zoega}}.

1 June 2016: An LSJ collision set was added to allow several thousand entries such as πεῖ (peî) and πέρα (péra) to be redirected to the appropriate Perseus lemma disambiguation pages without template arguments. The code was slightly refactored to mitigate the added complexity.


local export = {}

local collisions = mw.loadData("Module:R:Perseus/collision-data")

function lower_dashed(w) return string.gsub(string.lower(w), " ", "-") end

function remove_diacritics(x) return mw.ustring.gsub(mw.ustring.toNFD(x),'[^%w]+',"") end

-- maybe there is a better way to do this
function beta(w) return require("Module:R:Perseus/polytonic-to-perseus-betacode").polytonic_to_perseus_betacode(w) end

-- resources : template-name -> (perseus-resource-id, collisions-index-name, f-query-entry-postprocess, query-entry-suffix, code-lang)
local resources = {
	["R:L&S"]={"1999.04.0059", "LS", nil, nil, 'latin'},
	["R:Elementary Lewis"]={"1999.04.0060", "EL", nil, nil, 'latin' },
	["R:Peck"]={"1999.04.0062", nil, lower_dashed, '-harpers', 'latin'},
	["R:PersEnc"]={"1999.04.0004", nil, lower_dashed, "", 'latin' },
	["R:Stillwell"]={"1999.04.0006", "PECS", lower_dashed, "", 'latin'},
	["R:Platner"]={"1999.04.0054", "TDAR", lower_dashed, "", 'latin'},
	["R:Smith's Antiquities"]={"1999.04.0063", nil, lower_dashed, '-cn', 'latin'},
	["R:Smith's Persons"]={"1999.04.0104", nil, lower_dashed, '-bio-1', 'latin'},
	["R:Smith's Geography"]={"1999.04.0064", nil, lower_dashed, '-geo', 'latin'},
	["R:LSJ"]={"1999.04.0057","LSJ", nil, nil, 'greek'},
	["R:Middle Liddell"]={"1999.04.0058", "ML", nil, nil, 'greek'},
	["R:Harpocration"]={"2013.01.0002", nil, function(w) return lower_dashed(remove_diacritics(w)) end, "", 'greek'},
	["R:Autenrieth"]={"1999.04.0073", "Autenrieth", nil, nil, 'greek'},
	["R:Slater"]={"1999.04.0072", "Slater", nil, nil, "greek"},
	["R:Zoega"]={"2003.02.0002", "Zoega", nil, nil, 'non'}
}

function lang(template) return resources[template][5] end

function is_collision(x,template)
	local template_collisions = resources[template][2] ~= nil
	local lhs_postprocess = (resources[template][3]) or function(w) return w end
	return template_collisions and collisions[resources[template][2]][lhs_postprocess(x)] == true
end

function format_perseus_url(title, beta_or_latin, template, redirect)
	local harpo = template == 'R:Harpocration' and ":letter="..string.upper(string.sub(remove_diacritics(beta_or_latin),1,1)) or ""
	local resource = resources[template][1] or ''
	local url_redirect_lhs = 'http://www.perseus.tufts.edu/hopper/resolveform?type=exact&lookup='
	local url_entry_lhs = 'http://www.perseus.tufts.edu/hopper/text?doc=Perseus:text:'..resource..harpo..':entry='
	local url_rhs = redirect and '&lang='..lang(template) or ''
	local postprocess = resources[template][3] ~= nil and (function(w) return resources[template][3](w)..(resources[template][4] or '') end) or function(w) return w end
	return (redirect and url_redirect_lhs or url_entry_lhs)..postprocess(beta_or_latin)..url_rhs
end

function format_perseus_wikilink(title, beta_or_latin, template, redirect)
	local title_span = is_polytonic(title) and '<span lang="grc">'..title..'</span>' or title
	return (
		beta_or_latin == '' and '' or
		'[' .. format_perseus_url(title, beta_or_latin, template, redirect) .. ' ' .. title_span .. '] in '
		)
end

function is_polytonic(txt)
	return require("Module:scripts").findBestScript(txt, require("Module:languages").getByCode("grc")):getCode() == "polytonic"
end

function export.create(frame)
	local args = frame:getParent().args
	local template = string.sub(frame:getParent():getTitle(), 10)
	local greek = lang(template) == 'greek'
	local word = args['w'] or args[2] or args[1] or mw.title.getCurrentTitle().text
	local word_display = args['w'] or (not greek and args[1]) or (is_polytonic(args[1]) and args[1]) or mw.title.getCurrentTitle().text
	local beta_or_latin = greek and ((not is_polytonic(args[1]) and args[1]) or args['w'] and word) or (greek and beta(word)) or word
	local redirect = is_collision(word,template) and not args[1] and not args['w']
	return word_display == template and "" or format_perseus_wikilink(word_display, beta_or_latin, template, redirect)
end

return export