Modul:Transclude

Aus Steel Beasts Wiki

Die Dokumentation für dieses Modul kann unter Modul:Transclude/Doku erstellt werden

local p = {}

local transcluder = require( 'Modul:Transcluder' )

local function getLstSections( title, options )
	if options.sections == nil then
		return ''
	end
	
	local separator = '\n\n'
	if options.inlineSections == true then
		separator = ' '
	end

	local out = {}

	for _, section in ipairs( mw.text.split( options.sections, ', ', true ) ) do
		table.insert( out, transcluder.get( string.format( "%s#%s", title, section ) ) )
	end
	
	return table.concat( out, separator )
end


local function parseTitleAndSections( title, args )
	local sections = {}
	
	-- loop from 2 -> 10
	for i = 2, 10, 1 do
		if args[ i ] ~= nil then
			table.insert( sections, args[ i ] )
		end
	end
	
	if #sections == 1 then
		if mw.ustring.find( title, '#', 0, true ) == nil then
			title = title .. '#' .. sections[ 1 ]
		end
	end
	
	return title, table.concat( sections, ', ' ) or {}
end

--- Creates the options table from passed args
--- Supports the following args:
--- * inlineSections = Don't add two '\n' after each section
---                    Only works with loadSections
--- * loadSections   = request each section separately
---                    Allows to set a list of <section> elements to translcude
---                    Required when specifying a list of sections in {{Transclude}}
--- * noFiles        = Remove all files from the output
--- * filterFiles    = Filter files, see Module:Transcluder
---                  = e.g. '1,2' -> Exclude all files except the first and second
--- * noRefs         = Remove all references from the output
--- @param args table
--- @return table
local function makeOptions( args )
	local function isSet( a1, a2 )
		return (args[ a1 ] ~= nil and args[ a1 ] ~= '') or
		       (args[ a2 ] ~= nil and args[ a2 ] ~= '')
	end

	local options = {
		loadSections = false,
		inlineSections = false,
		categories = '0',
	}

	if isSet( 'loadSections', 'Sektionen' ) then
		options[ 'loadSections' ] = true
	end
	
	if isSet( 'noFiles', 'Keine Dateien' ) then
		options[ 'files' ] = 0
	end
	
	if isSet( 'filterFiles', 'Dateifilter' ) then
		options[ 'files' ] = args[ 'fileFilter' ] or args[ 'Dateifilter' ]
	end
	
	if isSet( 'inlineSections', 'keinSektionsumbruch' ) then
		options[ 'inlineSections' ] = true
	end
	
	if isSet( 'noRefs', 'keineReferenzen' ) then
		options[ 'references' ] = 0
		options[ 'templates' ] = '-Quelle'
	end

	if isSet( 'onlyParagraphs', 'Nur Text' ) then
		options[ 'only' ] = 'paragraphs'
	end

	if isSet( 'onlyFirstParagraph', 'Erster Satz' ) then
		options[ 'only' ] = 'paragraphs'
		options[ 'paragraphs' ] = 0
	end

	return options
end

--- Transclude part of a page
--- Removes all categories by default
---
--- @param frame table
--- @return string
function p.transclude( frame )
	local options
	local args = {}
	local title

	for key, value in pairs(frame:getParent().args) do args[key] = value end
	for key, value in pairs(frame.args) do args[key] = value end

	options = makeOptions( args )
	
	title = args[ 'Titel' ] or args[ 1 ] or nil
	
	if title == nil then
		error( 'Kein Seitentitel angegeben' )
	end
	
	title, sections = parseTitleAndSections( title, args )

	if #sections > 1 then
		options[ 'sections' ] = sections
	end

	if options.loadSections == true then
		return frame:preprocess( getLstSections( title, options ) )
	end

	return frame:preprocess( mw.ustring.gsub( transcluder.get( title, options ), '[\r\n\s]*</?translate>[\r\n\s]*', '' ) )
end

return p