stringtranslate.com

Module:Annotated link

local function pipedLink(name, display) return '[[:'..name..'|'..display..']]' endlocal function isEmpty(value) return value == nil or value == '' endlocal function notEmpty(value) return not isEmpty(value) end-- Unescape functionality grabbed from https://stackoverflow.com/a/14899740/1832568local function unescape(str)str = string.gsub(str, '&#(%d+);', string.char)str = string.gsub(str, '&#x(%d+);', function(d) return string.char(tonumber(d, 16)) end)return strendlocal function hashDelimitedList(list_string) return mw.text.gsplit(unescape(list_string), '%s*#%s*') endlocal function alarmingMessage(message)return '<span style="color:#d33">[[Module:Annotated link]] '..message..'.</span>'..'[[Category:Pages displaying alarming messages about Module:Annotated link]]'endlocal function optionallyVisibleCategory(class, category)return '<span style="display:none" class="'..class..'">'..category..'</span>[[Category:'..category..' via Module:Annotated link]]'endlocal function handleFirstLetterCase(short_description, case)return mw.ustring.gsub(short_description, '^([^%d])', function(first_char)if case == 'upper' thenreturn mw.ustring.upper(first_char)endreturn mw.ustring.lower(first_char) end)endlocal mLang = require('Module:Lang')local function langify(args)local lang = args.langlocal text = args.textif isEmpty(lang) or lang == 'en' thenreturn textendreturn mLang._lang {lang,text,italic = args.italic,nocat = args.nocat,size = args.size,cat = args.cat,rtl = args.rtl}endlocal function formatResult(result, dash, description, prefix_parentheses)if notEmpty(description) thenif prefix_parentheses thenlocal startIdx = description:find("%(")if startIdx then local beforeParens = description:sub(1, startIdx - 2) local insideParens = description:sub(startIdx, -1) return result..' '..insideParens..dash..' '..beforeParensendendreturn result..dash..' '..descriptionendreturn resultendlocal function annotatedLink(args)local name = args.nameif isEmpty(name) thenreturn alarmingMessage('requires a page name (including namespace)')end-- In order to handle an attempt to annotate a template link-- already formatted with the likes of {{tl|<template name>}};-- unescape name to make sense of braces in lua patern matching.name = unescape(name)if name:match('^{%b{}}$') then-- The possibility to extract useful data exists here: e.g. {{tl*|Template}}.return alarmingMessage('requires only a page name (including namespace) without markup. '..'If an attempt is being made to annotate a link to a template, '..'provide only the template name with namespace e.g. "Template:Example"')end-- If a literal link was provided as name;-- extract the content and apply it to name and display as appropriate.local wikilink = mw.ustring.match(name, '^%[%[%s*:*%s*(.-)%s*%]%]$')if wikilink thenlocal link_name, link_display = unpack(mw.text.split(wikilink, '%s*|%s*'))if link_name thenname = link_nameendif link_display and isEmpty(args.display) thenargs.display = link_displayendend-- Prepare to concatenate.local resultlocal is_template = name:match('^Template:(.+)$')local template_link = args.template_linkif is_template and template_link ~= 'no' thenresult = '{{'..pipedLink(name, is_template)..'}}'if template_link == 'code' thenresult = '<code>'..result..'</code>'endelselocal display = args.displayif isEmpty(display) thendisplay = nameendresult = langify({lang = args.link_lang,text = pipedLink(name, display),italic = args.link_lang_italic,nocat = args.link_lang_nocat,size = args.link_lang_size,cat = args.link_lang_cat,rtl = args.link_lang_rtl})if notEmpty(args.quote) thenresult = '"'..result..'"'endlocal abbr = args.abbrif notEmpty(abbr) thenresult = result..' (<abbr'local abbr_title = args.abbr_titleif notEmpty(abbr_title) thenresult = result..' title="'..abbr_title..'"'endresult = result..'>'..abbr..'</abbr>)'endendif isEmpty(result) thenreturn alarmingMessage('could not create a link for "'..name..'"')endlocal aka = args.akaif notEmpty(aka) thenresult = result..', also known as '..langify({lang = args.aka_lang,text = aka,italic = args.aka_lang_italic,nocat = args.aka_lang_nocat,size = args.aka_lang_size,cat = args.aka_lang_cat,rtl = args.aka_lang_rtl})endlocal wedge = args.wedgeif notEmpty(wedge) thenresult = result..', '..langify({lang = args.wedge_lang,text = wedge,italic = args.wedge_lang_italic,nocat = args.wedge_lang_nocat,size = args.wedge_lang_size,cat = args.wedge_lang_cat,rtl = args.wedge_lang_rtl})end-- Exclude wikidata fallback for any specified list of link titles,-- unless explicity instructed that it's okay.local not_wikidata_for_links_starting_with = args.not_wikidata_for_links_starting_withif isEmpty(args.wikidata) and notEmpty(not_wikidata_for_links_starting_with) thenfor only_explicit in hashDelimitedList(not_wikidata_for_links_starting_with) doif name:match('^'..only_explicit) thenargs.only = 'explicit'breakendendend-- Get the short description from Module:GetShortDescription.local short_description = require('Module:GetShortDescription').main({none_is_valid = args.none_is_valid,none_is_nil = args.none_is_nil,lang_italic = args.desc_lang_italic,lang_nocat = args.desc_lang_nocat,lang_size = args.desc_lang_size,lang_cat = args.desc_lang_cat,lang_rtl = args.desc_lang_rtl,lang_no = args.desc_lang_no,prefer = args.prefer,only = args.only,name = name})local dash = args.dashif isEmpty(dash) thendash = '&nbsp;–'endlocal fallback = args.fallbackif isEmpty(short_description) or short_description.redlink thenreturn formatResult(result, dash, fallback, args.prefix_parentheses)endif short_description.alarm thenreturn short_description.alarmendlocal maintenance = ''if short_description.redirected thenmaintenance = optionallyVisibleCategory('category-annotation-with-redirected-description','Pages displaying short descriptions of redirect targets')endlocal fellbackif short_description.wikidata thenif short_description.fellback thenfellback = truemaintenance = maintenance..optionallyVisibleCategory('category-wikidata-fallback-annotation','Pages displaying wikidata descriptions as a fallback')endshort_description = short_description.wikidata-- Filter against likely rubbish wikidata descriptions.local not_wikidata_descriptions_including = args.not_wikidata_descriptions_includingif notEmpty(not_wikidata_descriptions_including) then-- Case insentive matching.local lower_case_short_description = short_description:lower()for exclusion in hashDelimitedList(not_wikidata_descriptions_including:lower()) doif lower_case_short_description:match(exclusion) thenshort_description = ''breakendendendif isEmpty(short_description) thenreturn formatResult(result, dash, fallback, args.prefix_parentheses)endelseshort_description = short_description.explicitendlocal lower_case_name = name:lower()if notEmpty(short_description) and not short_description:match(' ') then-- Filter against likely rubbish single word descriptions.local lower_case_short_description = short_description:lower()local not_single_word = args.not_single_wordif notEmpty(not_single_word) then-- Case insentive matching.for single_word in hashDelimitedList(not_single_word:lower()) doif single_word == lower_case_short_description thenshort_description = ''breakendendendif isEmpty(short_description) or lower_case_name:match(lower_case_short_description) thenreturn formatResult(result, dash, fallback, args.prefix_parentheses)endif isEmpty(args.space_cat) thenmaintenance = maintenance..optionallyVisibleCategory('category-spaceless-annotation','Pages displaying short descriptions with no spaces')endendif lower_case_name == short_description:lower() thenif fellback thenreturn formatResult(result, dash, fallback, args.prefix_parentheses)endmaintenance = maintenance..optionallyVisibleCategory('category-annotation-matches-name','Pages displaying short descriptions matching their page name')end-- Short descriptions on en Wikipedia should be formatted with an uppercase first letter, but-- the typical application of this module will require the first character to be lowercase, but-- some descriptions may start with proper names and should start with an uppercase letter even if used in an annotaion.-- By default; this module will not affect the first letter case of descriptions retrieved by Module:GetShortDescription, but-- the first letter case may be transformed explicitly if required.local desc_first_letter_case = args.desc_first_letter_caseif desc_first_letter_case == 'upper' or desc_first_letter_case == 'lower' thenshort_description = handleFirstLetterCase(short_description, desc_first_letter_case)endreturn formatResult(result, dash, (short_description or fallback)..maintenance, args.prefix_parentheses)endlocal p = {}function p.main(frame)local args = require('Module:Arguments' ).getArgs(frame)if isEmpty(args) thenreturn alarmingMessage('could not getArgs') -- This really would be alarming.endreturn annotatedLink(args)endreturn p