-- Module:Excerpt implements the Excerpt template-- Documentation and master version: https://en.wikipedia.org/wiki/Module:Excerpt-- Authors: User:Sophivorus, User:Certes, User:Aidan9382 & others-- License: CC-BY-SA-3.0localTranscluder=require('Module:Transcluder')localyesno=require('Module:Yesno')localok,config=pcall(require,'Module:Excerpt/config')ifnotokthenconfig={}endlocalp={}-- Helper function to get argumentslocalargslocalfunctiongetArg(key,default)localvalue=args[key]ifvalueandmw.text.trim(value)~=''thenreturnvalueendreturndefaultend-- Helper function to handle errorslocalfunctiongetError(message,value)iftype(message)=='string'thenmessage=Transcluder.getError(message,value)endifconfig.categoriesandconfig.categories.errorsandmw.title.getCurrentTitle().isContentPagethenmessage:node('[[Category:'..config.categories.errors..']]')endreturnmessageend-- Helper function to get localized messageslocalfunctiongetMessage(key)localok,TNT=pcall(require,'Module:TNT')ifnotokthenreturnkeyendreturnTNT.format('I18n/Module:Excerpt.tab',key)end-- Main entry point for templatesfunctionp.main(frame)args=Transcluder.parseArgs(frame)-- Make sure the requested page existslocalpage=getArg(1)ifnotpageorpage=='{{{1}}}'thenreturngetError('no-page')endlocaltitle=mw.title.new(page)ifnottitlethenreturngetError('invalid-title',page)endiftitle.isRedirectthentitle=title.redirectTargetendifnottitle.existsthenreturngetError('page-not-found',page)endpage=title.prefixedText-- Set variables from the template parameterslocalsection=getArg(2,mw.ustring.match(getArg(1),'[^#]+#(.+)'))localhat=yesno(getArg('hat',true))localedit=yesno(getArg('edit',true))localthis=getArg('this')localonly=getArg('only')localfiles=getArg('files',getArg('file',(only=='file'and1)))locallists=getArg('lists',getArg('list',(only=='list'and1)))localtables=getArg('tables',getArg('table',(only=='table'and1)))localtemplates=getArg('templates',getArg('template',(only=='template'and1)))localparagraphs=getArg('paragraphs',getArg('paragraph',(only=='paragraph'and1)))localreferences=getArg('references')localsubsections=notyesno(getArg('subsections'))localnoLinks=notyesno(getArg('links',true))localnoBold=notyesno(getArg('bold'))localonlyFreeFiles=yesno(getArg('onlyfreefiles',true))localbriefDates=yesno(getArg('briefdates',false))localinline=yesno(getArg('inline'))localquote=yesno(getArg('quote'))localmore=yesno(getArg('more'))localclass=getArg('class')localdisplaytitle=getArg('displaytitle')orpage-- Build the hatnoteifhatandnotinlinethenifthisthenhat=thiselseifquotethenhat=getMessage('this')elseifonlythenhat=getMessage(only)elsehat=getMessage('section')endhat=hat..' '..getMessage('excerpt')..' 'ifsectionthenhat=hat..'[[:'..page..'#'..mw.uri.anchorEncode(section)..'|'..displaytitle..' § '..mw.ustring.gsub(section,'%[%[([^]|]+)|?[^]]*%]%]','%1')..']].'-- remove nested linkselsehat=hat..'[[:'..page..'|'..displaytitle..']].'endifeditthenhat=hat..'<span class="mw-editsection-like plainlinks"><span class="mw-editsection-bracket">[</span>['hat=hat..title:fullUrl('action=edit')..' '..mw.message.new('editsection'):plain()hat=hat..']<span class="mw-editsection-bracket">]</span></span>'endifconfig.hatthenhat=config.hat..hat..'}}'hat=frame:preprocess(hat)elsehat=mw.html.create('div'):addClass('dablink excerpt-hat'):wikitext(hat)endelsehat=nilend-- Build the "Read more" linkifmoreandnotinlinethenmore="'''[["..page..'#'..(sectionor'').."|"..getMessage('more').."]]'''"more=mw.html.create('div'):addClass('noprint excerpt-more'):wikitext(more)elsemore=nilend-- Build the options for Module:Transcluder out of the template parameters and the desired defaultslocaloptions={files=files,lists=lists,tables=tables,paragraphs=paragraphs,sections=subsections,categories=0,references=references,only=onlyandmw.text.trim(only,'s')..'s',noLinks=noLinks,noBold=noBold,noSelfLinks=true,noNonFreeFiles=onlyFreeFiles,noBehaviorSwitches=true,fixReferences=true,linkBold=true,}-- Get the excerpt itselflocaltitle=page..'#'..(sectionor'')localok,excerpt=pcall(Transcluder.get,title,options)ifnotokthenreturngetError(excerpt)endifmw.text.trim(excerpt)==''andnotonlythenifsectionthenreturngetError('section-empty',section)elsereturngetError('lead-empty')endend-- Fix birth and death dates, but only in the first paragraphifbriefDatesthenlocalstartpos=1-- skip initial templateslocalslocale=0repeatstartpos=e+1s,e=mw.ustring.find(excerpt,"%s*%b{}%s*",startpos)untilnotsors>startposs,e=mw.ustring.find(excerpt,"%b()",startpos)-- get (...), which may be (year–year)ifsands<startpos+100then-- look only near the startlocalyear1,conjunction,year2=mw.ustring.match(mw.ustring.sub(excerpt,s,e),'(%d%d%d+)(.-)(%d%d%d+)')ifyear1andyear2and(mw.ustring.match(conjunction,'[%-–—]')ormw.ustring.match(conjunction,'{{%s*[sS]nd%s*}}'))thenlocaly1=tonumber(year1)localy2=tonumber(year2)ify2>y1andy2<y1+125andy1<=tonumber(os.date("%Y"))thenexcerpt=mw.ustring.sub(excerpt,1,s)..year1.."–"..year2..mw.ustring.sub(excerpt,e)endendendend-- If no file was found, try to get one from the infoboxlocalfileNamespaces=Transcluder.getNamespaces('File')if((only=='file'oronly=='files')or(notonlyand(files~='0'ornotfiles)))and-- caller asked for filesnotTranscluder.matchAny(excerpt,'%[%[',fileNamespaces,':')and-- and there are no files in Transcluder's outputconfig.captions-- and we have the config option required to try finding files in templatesthen-- We cannot distinguish the infobox from the other templates so we search them alllocalinfobox=Transcluder.getTemplates(excerpt);infobox=table.concat(infobox)localparameters=Transcluder.getParameters(infobox)localfile,captions,caption,cssclasses,cssclassfor_,pairinpairs(config.captions)dofile=pair[1]file=parameters[file]iffileandTranscluder.matchAny(file,'^.*%.',{'[Jj][Pp][Ee]?[Gg]','[Pp][Nn][Gg]','[Gg][Ii][Ff]','[Ss][Vv][Gg]'},'.*')thenfile=mw.ustring.match(file,'%[?%[?.-:([^{|]+)%]?%]?')orfile-- [[File:Example.jpg{{!}}upright=1.5]] to Example.jpgcaptions=pair[2]for_,pinpairs(captions)doifparameters[p]thencaption=parameters[p]breakendend-- Check for CSS classes-- We opt to use skin-invert-image instead of skin-invert-- in all other cases, the CSS provided in the infobox is usedifpair[3]thencssclasses=pair[3]for_,pinpairs(cssclasses)doifparameters[p]thencssclass=((parameters[p]=='skin-invert')and'skin-invert-image'orparameters[p])breakendendendexcerpt='[[File:'..file..(cssclassand('|class='..cssclass)or'')..'|thumb|'..(captionor'')..']]'..excerptif(onlyFreeFiles)thenexcerpt=Transcluder.removeNonFreeFiles(excerpt)endbreakendendend-- Unlike other elements, templates are filtered here-- because we had to search the infoboxes for fileslocaltrashifonlyand(only=='template'oronly=='templates')thentrash,excerpt=Transcluder.getTemplates(excerpt,templates);else-- Remove blacklisted templateslocalblacklist=config.blacklistandtable.concat(config.blacklist,',')or''iftemplatesthenifstring.sub(templates,1,1)=='-'then--Unwanted templates. Append to blacklistblacklist=templates..','..blacklistelse--Wanted templates. Replaces blacklist and acts as whitelistblacklist=templatesendelseblacklist='-'..blacklistendtrash,excerpt=Transcluder.getTemplates(excerpt,blacklist);end-- Remove extra line breaks but leave one before and after so the parser interprets lists, tables, etc. correctlyexcerpt=mw.text.trim(excerpt)excerpt=string.gsub(excerpt,'\n\n\n+','\n\n')excerpt='\n'..excerpt..'\n'-- Remove nested categoriesexcerpt=frame:preprocess(excerpt)localcategories,excerpt=Transcluder.getCategories(excerpt,options.categories)-- Add tracking categoriesifconfig.categoriesthenlocalcontentCategory=config.categories.contentifcontentCategoryandmw.title.getCurrentTitle().isContentPagethenexcerpt=excerpt..'[[Category:'..contentCategory..']]'endlocalnamespaceCategory=config.categories[mw.title.getCurrentTitle().namespace]ifnamespaceCategorythenexcerpt=excerpt..'[[Category:'..namespaceCategory..']]'endend-- Load the styleslocalstylesifconfig.stylesthenstyles=frame:extensionTag('templatestyles','',{src=config.styles})end-- Combine and return the elementsifinlinethenreturnmw.text.trim(excerpt)endlocaltag='div'ifquotethentag='blockquote'endexcerpt=mw.html.create('div'):addClass('excerpt'):wikitext(excerpt)localblock=mw.html.create(tag):addClass('excerpt-block'):addClass(class)returnblock:node(styles):node(hat):node(excerpt):node(more)end-- Entry points for backwards compatibilityfunctionp.lead(frame)returnp.main(frame)endfunctionp.excerpt(frame)returnp.main(frame)endreturnp