Module:Excerpt

local Transcluder = require('Module:Transcluder')

local yesno = require('Module:Yesno')

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

local p = {}

-- Helper function to get arguments local args function getArg(key, default) value = args[key] if value and mw.text.trim(value) ~= '' then return value end return default end

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

-- Helper function to get localized messages function getMessage(key) local ok, message = pcall( function return require('Module:TNT').format('I18n/Module:Extract.tab', key) end ) if not ok then return key end return message end

function p.main(frame) args = Transcluder.parseArgs(frame)

-- Make sure the requested page exists local page = getArg(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

-- Set variables local fragment = getArg('fragment') local section = fragment or getArg(2, mw.ustring.match(getArg(1), '[^#]+#([^#]+)')) local hat = yesno( getArg('hat', true) ) local this = getArg('this') local indent = yesno( getArg('indent') ) local only = getArg('only') local files = getArg('files') local lists = getArg('lists') local tables = getArg('tables') local paragraphs = getArg('paragraphs') local references = getArg('references') local noBold = not yesno( getArg('bold') ) local tag = getArg('tag', 'div') local more = yesno( getArg('more') )

-- Build the hatnote if hat then if this then hat = this elseif indent then hat = getMessage('this') elseif only then hat = getMessage(only) else hat = getMessage('section') end hat = hat .. ' ' .. getMessage('extract') .. ' '		if section and not fragment then hat = hat .. '' .. page				.. ' § ' .. mw.ustring.gsub(section, '%[%[([^]|]+)|?[^*%]%]', '%1') .. ']]' -- remove nested links else hat = hat ..  .. page ..  end hat = hat .. "''" .. ' [ ['		hat = hat .. title:fullUrl('action=edit') .. ' edit' hat = hat .. '] ] ' .. "''"		local ok, Hatnote = pcall( function  return require('Module:Hatnote') end ) if ok then hat = Hatnote._hatnote( hat, { extraclasses = 'dablink extract-hat', selfref = true } ) else hat = mw.html.create('div'):addClass('dablink extract-hat'):wikitext(hat) end else hat = nil end

-- Build the "Read more" link if more then more = "" .. getMessage('more') .. "" more = mw.html.create('div'):addClass('noprint extract-more'):wikitext(more) else more = nil end

-- Build the options for Module:Transcluder out of the template arguments and the desired defaults local options = { files = files, lists = lists, tables = tables, paragraphs = paragraphs, templates = '-' .. table.concat(data.templates, ','), sections = 0, categories = 0, references = references, only = only and mw.text.trim(only, 's') .. 's', noBold = noBold, noSelfLinks = true, noBehaviorSwitches = true, fixReferences = true, linkBold = true, }

-- Get the extract itself local title = page .. '#' .. (section or '') local ok, extract = pcall(Transcluder.get, title, options) if not ok then return getError(extract) end if mw.text.trim(extract) == '' then if section then return getError('section-empty', section) else return getError('lead-empty') end end

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

-- If no file was found, try to extract one from the removed infoboxes local fileNamespaces = Transcluder.getNamespaces('File') if (files ~= '0' or not files) and not Transcluder.matchAny(extract, '%[%[', fileNamespaces, ':') then local templates = Transcluder.get(title, { only = 'templates', templates = data.templates, fixReferences = true } ) local parameters = Transcluder.getParameters(templates) local file, captions, caption for _, pair in pairs(data.captions) do			file = pair[1] file = parameters[file] if file and Transcluder.matchAny(file, '^.*%.', {'[Jj][Pp][Ee]?[Gg]','[Pp][Nn][Gg]','[Gg][Ii][Ff]','[Ss][Vv][Gg]'}, '.*') then file = mw.ustring.match(file, '%[?%[?.-:([^{|]+)%]?%]?') or file -- Example.jpg to Example.jpg captions = pair[2] for _, p in pairs(captions) do					if parameters[p] then caption = parameters[p] break end end extract =  .. file ..  .. extract break end end end

-- Remove nested categories extract = frame:preprocess(extract) local categories, extract = Transcluder.getCategories(extract, options.categories)

-- Add tracking categories local contentCategory = data.categories.content if contentCategory and mw.title.getCurrentTitle.isContentPage then message:node('') end local namespaceCategory = data.categories[ mw.title.getCurrentTitle.namespace ] if namespaceCategory then extract = extract .. ''	end

-- Load the styles local styles if data.styles then styles = frame:extensionTag( 'templatestyles', '', { src = data.styles } ) end

-- Combine and return the elements extract = mw.html.create(tag):addClass('extract'):wikitext(extract) local block = mw.html.create('div'):addClass('extract-block') if indent then block:addClass('extract-indent') end return block:node(styles):node(hat):node(extract):node(more) end

return p