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

-- 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 = 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], '[^#]+#([^#]+)') 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 t.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 .. '] ] ' .. "''"		hat = mw.html.create('div'):addClass('dablink extract-hat'):wikitext(hat) 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 t.truthy( args['subsections'] ), templates = '-' .. table.concat(d.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 t.truthy( args['bold'] ), noSelfLinks = true, noBehaviorSwitches = true, fixReferences = true, linkBold = true }

-- Get the extract itself local title = page .. '#' .. (section or '') local ok, extract = pcall(t.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 = t.getNamespaces('File') if t.truthy(options.files or true) and not t.matchAny(extract, '%[%[', fileNamespaces, ':') then local templates = t.get(title, { only = 'templates', templates = d.templates, fixReferences = true } ) local parameters = t.getParameters(templates) local file, captions, caption for _, pair in pairs(d.captions) do			file = pair[1] file = parameters[file] if file and t.matchAny(parameters[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 = t.getCategories(extract, options.categories)

-- Add tracking category local category = d.categories[ mw.title.getCurrentTitle.namespace ] if category 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