Module:Excerpt

local t = require('Module:Transcluder')

local d = require('Module:Extract/data')

local p = {}

-- Helper function to handle errors function getError(message, value) if type(message) == 'string' then message = t.getError(message, value) end if mw.title.getCurrentTitle.isContentPage and d.categories.errors then message:node('') end return message end

function p.main(frame) local args = t.parseArgs(frame)

-- Make sure the requested page exists local page = args[1] if not page then return getError('no-page') end local title = mw.title.new(page) if not title then return getError('no-page') end if title.isRedirect then title = title.redirectTarget end if not title.exists then return getError('page-not-found', page) end page = title.prefixedText

-- Define some useful variables local section = args[2] or mw.ustring.match(args[1], '[^#]+#([^#]+)')

-- Define the HTML elements local block = mw.html.create('div'):addClass('excerpt-block')

local hat if truthy( args['hat'] ) then if args['this'] then hat = args['this'] elseif args['indicator'] then hat = 'This is' elseif args['only'] == 'file' then hat = 'This file is' elseif args['only'] == 'files' then hat = 'These files are' elseif args['only'] == 'list' then hat = 'This list is' elseif args['only'] == 'lists' then hat = 'These lists are' elseif args['only'] == 'table' then hat = 'This table is' elseif args['only'] == 'tables' then hat = 'These tables are' elseif args['only'] == 'paragraph' then hat = 'This paragraph is' elseif args['only'] == 'paragraphs' then hat = 'These paragraphs are' else hat = 'This section is' end hat = hat .. ' an excerpt from ' if section then hatnote = hatnote .. '' .. page				.. ' § ' .. mw.ustring.gsub(section, '%[%[([^]|]+)|?[^*%]%]', '%1') .. ']]' -- remove nested links else hatnote = hatnote ..  .. page ..  end hatnote = hatnote .. "''" .. ' [ ['		hatnote = hatnote .. title:fullUrl('action=edit') .. ' edit' hatnote = hatnote .. '] ] ' .. "''"		hatnote = require('Module:Hatnote')._hatnote(hatnote, {selfref=true}) or getError('Error generating hatnote') end

-- Map template parameters to module options local only = { ['file'] = 'files', ['list'] = 'lists', ['table'] = 'tables', ['template'] = 'templates', ['paragraph'] = 'paragraphs', }

-- Build the module options out of the template arguments and the desired defaults local options = { files = args['files'] or args['file'], lists = args['lists'] or args['list'], tables = args['tables'] or args['table'], sections = args['sections'] or not t.truthy( args['subsections'] ), templates = '-' .. table.concat(templates, ','), paragraphs = args['paragraphs'] or args['paragraph'], categories = args['categories'] or args['category'] or 0, references = args['references'] or args['reference'], only = only[ args['only'] ] or args['only'], noBold = not t.truthy( args['bold'] ), noSelfLinks = true, noBehaviorSwitches = true, fixReferences = true, linkBold = true }

-- Get the excerpt itself local title = page .. '#' .. (section or '') local ok, excerpt = pcall(t.get, title, options) if not ok then return getError(excerpt) end

-- Add a line break in case the excerpt starts with a table or list excerpt = '\n' .. excerpt

-- If no file was found, try to build one from the infobox if t.truthy(options.files or true) and not t.matchAny(excerpt, '%[%[', {'File','Image'}, ':') then local infobox = t.get(title, { only = 'templates', templates = '.*[Ii]nfobox.*', fixReferences = true } ) local parameters = t.getParameters(infobox) for key, value in pairs(parameters) do			if t.matchAny(value, '^.*%.', {'[Jj][Pp][Ee]?[Gg]','[Pp][Nn][Gg]','[Gg][Ii][Ff]','[Ss][Vv][Gg]'}, '.*') then local file = mw.ustring.match(value, '%[?%[?.-:([^{|]+)%]?%]?') or value -- Example.jpg to Example.jpg local caption = '' for k, v in pairs(parameters) do					if mw.ustring.match(k, '.*[Cc]aption.*') or mw.ustring.match(k, '.*[Ll]egend.*') then v = mw.text.trim( mw.ustring.gsub(v, '%{%{[Cc]ollapsed infobox section.*%}%}', '') ) if v ~= '' then caption = v							break end end end excerpt =  .. file ..  .. excerpt break end end end

-- Remove nested categories excerpt = frame:preprocess(excerpt) local categories, excerpt = t.getCategories(excerpt, options.categories)

-- Add tracking category local category = d.categories[ mw.title.getCurrentTitle.namespace ] if category then excerpt = excerpt .. ''	end

-- Combine and return the elements excerpt = mw.html.create(tag):addClass('excerpt'):wikitext(excerpt) return block:node(style):node(hatnote):node(excerpt) end

return p