Jump to content

Module:Annotated link

Permanently protected module
From Wikipedia, the free encyclopedia

localfunctionpipedLink(name,display)return'[[:'..name..'|'..display..']]'endlocalfunctionisEmpty(value)returnvalue==nilorvalue==''endlocalfunctionnotEmpty(value)returnnotisEmpty(value)end-- Unescape functionality grabbed from https://stackoverflow.com/a/14899740/1832568localfunctionunescape(str)str=string.gsub(str,'&#(%d+);',string.char)str=string.gsub(str,'&#x(%d+);',function(d)returnstring.char(tonumber(d,16))end)returnstrendlocalfunctionhashDelimitedList(list_string)returnmw.text.gsplit(unescape(list_string),'%s*#%s*')endlocalfunctionalarmingMessage(message)return'<span style="color:#d33">[[Module:Annotated link]] '..message..'.</span>'..'[[Category:Pages displaying alarming messages about Module:Annotated link]]'endlocalfunctionoptionallyVisibleCategory(class,category)return'<span style="display:none" class="'..class..'">'..category..'</span>[[Category:'..category..' via Module:Annotated link]]'endlocalfunctionhandleFirstLetterCase(short_description,case)returnmw.ustring.gsub(short_description,'^([^%d])',function(first_char)ifcase=='upper'thenreturnmw.ustring.upper(first_char)endreturnmw.ustring.lower(first_char)end)endlocalmLang=require('Module:Lang')localfunctionlangify(args)locallang=args.langlocaltext=args.textifisEmpty(lang)orlang=='en'thenreturntextendreturnmLang._lang{lang,text,italic=args.italic,nocat=args.nocat,size=args.size,cat=args.cat,rtl=args.rtl}endlocalfunctionformatResult(result,dash,description,prefix_parentheses)ifnotEmpty(description)thenifprefix_parenthesesthenlocalstartIdx=description:find("%(")ifstartIdxthenlocalbeforeParens=description:sub(1,startIdx-2)localinsideParens=description:sub(startIdx,-1)returnresult..' '..insideParens..dash..' '..beforeParensendendreturnresult..dash..' '..descriptionendreturnresultendlocalfunctionannotatedLink(args)localname=args.nameifisEmpty(name)thenreturnalarmingMessage('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)ifname:match('^{%b{}}$')then-- The possibility to extract useful data exists here: e.g. {{tl*|Template}}.returnalarmingMessage('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.localwikilink=mw.ustring.match(name,'^%[%[%s*:*%s*(.-)%s*%]%]$')ifwikilinkthenlocallink_name,link_display=unpack(mw.text.split(wikilink,'%s*|%s*'))iflink_namethenname=link_nameendiflink_displayandisEmpty(args.display)thenargs.display=link_displayendend-- Prepare to concatenate.localresultlocalis_template=name:match('^Template:(.+)$')localtemplate_link=args.template_linkifis_templateandtemplate_link~='no'thenresult='{{'..pipedLink(name,is_template)..'}}'iftemplate_link=='code'thenresult='<code>'..result..'</code>'endelselocaldisplay=args.displayifisEmpty(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})ifnotEmpty(args.quote)thenresult='"'..result..'"'endlocalabbr=args.abbrifnotEmpty(abbr)thenresult=result..' (<abbr'localabbr_title=args.abbr_titleifnotEmpty(abbr_title)thenresult=result..' title="'..abbr_title..'"'endresult=result..'>'..abbr..'</abbr>)'endendifisEmpty(result)thenreturnalarmingMessage('could not create a link for "'..name..'"')endlocalaka=args.akaifnotEmpty(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})endlocalwedge=args.wedgeifnotEmpty(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.localnot_wikidata_for_links_starting_with=args.not_wikidata_for_links_starting_withifisEmpty(args.wikidata)andnotEmpty(not_wikidata_for_links_starting_with)thenforonly_explicitinhashDelimitedList(not_wikidata_for_links_starting_with)doifname:match('^'..only_explicit)thenargs.only='explicit'breakendendend-- Get the short description from Module:GetShortDescription.localshort_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})localdash=args.dashifisEmpty(dash)thendash='&nbsp;–'endlocalfallback=args.fallbackifisEmpty(short_description)orshort_description.redlinkthenreturnformatResult(result,dash,fallback,args.prefix_parentheses)endifshort_description.alarmthenreturnshort_description.alarmendlocalmaintenance=''ifshort_description.redirectedthenmaintenance=optionallyVisibleCategory('category-annotation-with-redirected-description','Pages displaying short descriptions of redirect targets')endlocalfellbackifshort_description.wikidatathenifshort_description.fellbackthenfellback=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.localnot_wikidata_descriptions_including=args.not_wikidata_descriptions_includingifnotEmpty(not_wikidata_descriptions_including)then-- Case insentive matching.locallower_case_short_description=short_description:lower()forexclusioninhashDelimitedList(not_wikidata_descriptions_including:lower())doiflower_case_short_description:match(exclusion)thenshort_description=''breakendendendifisEmpty(short_description)thenreturnformatResult(result,dash,fallback,args.prefix_parentheses)endelseshort_description=short_description.explicitendlocallower_case_name=name:lower()ifnotEmpty(short_description)andnotshort_description:match(' ')then-- Filter against likely rubbish single word descriptions.locallower_case_short_description=short_description:lower()localnot_single_word=args.not_single_wordifnotEmpty(not_single_word)then-- Case insentive matching.forsingle_wordinhashDelimitedList(not_single_word:lower())doifsingle_word==lower_case_short_descriptionthenshort_description=''breakendendendifisEmpty(short_description)orlower_case_name:match(lower_case_short_description)thenreturnformatResult(result,dash,fallback,args.prefix_parentheses)endifisEmpty(args.space_cat)thenmaintenance=maintenance..optionallyVisibleCategory('category-spaceless-annotation','Pages displaying short descriptions with no spaces')endendiflower_case_name==short_description:lower()theniffellbackthenreturnformatResult(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.localdesc_first_letter_case=args.desc_first_letter_caseifdesc_first_letter_case=='upper'ordesc_first_letter_case=='lower'thenshort_description=handleFirstLetterCase(short_description,desc_first_letter_case)endreturnformatResult(result,dash,(short_descriptionorfallback)..maintenance,args.prefix_parentheses)endlocalp={}functionp.main(frame)localargs=require('Module:Arguments').getArgs(frame)ifisEmpty(args)thenreturnalarmingMessage('could not getArgs')-- This really would be alarming.endreturnannotatedLink(args)endreturnp
close