Module

TemplateSource

From Dogcraft Wiki

mNo edit summary
m (Domino moved page Module:Test to Module:TemplateSource without leaving a redirect)
(No difference)

Revision as of 20:34, 31 January 2026

Meant to be used with the {{Template source}} template. Returns the plaintext contents of a template. Obeys the mostly the same behaviours as transclusion would, respects <onlyinclude>, <includeonly>, and <noinclude> tags. This module is primarily meant to be used as input to <syntaxhighlight>, which doesn't handle some tags (like <templatestyles> or <categorytree>) correctly.

Example

The source of the {{Pagetabs}} template, wrapped in a <syntaxhighlight> tag.

{{#tag:syntaxhighlight|{{#invoke:TemplateSource|templateSource|Template:Pagetabs}}|lang=html|line=1}} generates:

<templatestyles src="User:Domino/CategoryTree.css" /><templatestyles src="Template:Templates_Nav/styles.css" /><div class="template-documentation"><div class="templates-nav"> [[:Category:Templates|<span style="color:black;">'''Index of templates'''</span>]]
<categorytree mode="all" hideprefix="always" style="width:260px;" class="highlight-root-only">Content templates</categorytree>
{{#categorytree:Documentation templates|mode="all"|hideprefix="always"|style="width:260px;"|class="highlight-root-only"}}
{{#categorytree:Interface templates|mode="all"|hideprefix="always"|style="width:260px;"|class="highlight-root-only"}}
{{#categorytree:Miscellaneous templates|mode="all"|hideprefix="always"|style="width:260px;"|class="highlight-root-only"}}
{{#categorytree:Navigation templates|mode="all"|hideprefix="always"|style="width:260px;"|class="highlight-root-only"}}
{{#categorytree:Tool templates|mode="all"|hideprefix="always"|style="width:260px;"|class="highlight-root-only"}}</div><div class="template-documentation-content">

local p = {}

function handleOnlyInclude( text )
	onlyincludes = {}
	
	for capture in mw.ustring.gmatch( text, '<onlyinclude>(.-)</onlyinclude>' ) do
		table.insert(onlyincludes, capture)
	end
	
	return table.concat(onlyincludes, '')
end

function stripNoinclude( text )
	stripCompleteNoincludes = mw.ustring.gsub( text, '<noinclude>.-</noinclude>', '' )
	stripDanglingNoincludes = mw.ustring.gsub( stripCompleteNoincludes, '<noinclude>.*', '' )
	return stripDanglingNoincludes
end

function stripIncludeonlyTags( text )
	stripIncludeonlyOpenTags = mw.ustring.gsub( text, '<includeonly>', '' )
	stripIncludeonlyCloseTags = mw.ustring.gsub( stripIncludeonlyOpenTags, '</includeonly>', '' )
	return stripIncludeonlyCloseTags
end

function templateText()
	pageContent = mw.title.new( 'Template:Templates Nav' ):getContent()
	
	-- stripping includeonlys
	strippedIncludeOnly = stripIncludeonlyTags( pageContent )
	
	-- handling onlyinclude tags
	handledOnlyInclude = handleOnlyInclude( strippedIncludeOnly )
	if mw.ustring.len( handledOnlyInclude ) > 0 then
		strippedNoIncludesInsideOnlyIncludes = stripNoinclude( handledOnlyInclude )
		return mw.text.nowiki( strippedNoIncludesInsideOnlyIncludes )
	end
	
	-- handling normal noincludes
	strippedNoIncludes = stripNoinclude( strippedIncludeOnly )
	return mw.text.nowiki( strippedNoIncludes )
end

function p.templateSource(frame)
	text = templateText()
	return mw.text.decode( text )
end

return p
This page was last modified on 31 January 2026, at 20:34. (40 hours ago)