Module:Excerpt

local Transcluder = require('Module:Transcluder')

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

local p = {}

-- 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) return require('Module:TNT').format('I18n/Module:Extract.tab', key) end

function p.main(frame) local args = Transcluder.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], '[^#]+#([^#]+)') local hat = args['hat'] or true local this = args['this'] local indent = args['indent'] local only = args['only'] local tag = args['tag'] or 'div'

if Transcluder.truthy(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 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, {selfref = true}) else hat = mw.html.create('div'):addClass('dablink extract-hat'):wikitext(hat) end else hat = nil end

-- Build the options for Module:Transcluder 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 Transcluder.truthy( args['subsections'] ), templates = '-' .. table.concat(data.templates, ','), paragraphs = args['paragraphs'] or args['paragraph'], categories = args['categories'] or args['category'] or 0, references = args['references'] or args['reference'], only = args['only'] and mw.text.trim( args['only'], 's' ) .. 's', noBold = not Transcluder.truthy( args['bold'] ), 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

-- 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 Transcluder.truthy(options.files or true) 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

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

return p