Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Text Formatter Laravel Package

s9e/text-formatter

PHP text formatting library with plugin support for BBCode, Markdown, HTML, and more. Includes predefined bundles, extensive documentation, and a JavaScript port for client-side preview and demos. Install via Composer and integrate customizable parsing/rendering.

View on GitHub
Deep Wiki
Context7

By default, template normalization consists in optimizing a template's content by removing superfluous whitespace and inlining content wherever possible, as well as normalize HTML elements' and attributes' names to lowercase and other menial tasks.

Template normalization is performed by $configurator->templateNormalizer, which you can access as an array.

$configurator = new s9e\TextFormatter\Configurator;

foreach ($configurator->templateNormalizer as $i => $normalizer)
{
	echo $i, "\t", get_class($normalizer), "\n";
}
0	s9e\TextFormatter\Configurator\TemplateNormalizations\PreserveSingleSpaces
1	s9e\TextFormatter\Configurator\TemplateNormalizations\RemoveComments
2	s9e\TextFormatter\Configurator\TemplateNormalizations\RemoveInterElementWhitespace
3	s9e\TextFormatter\Configurator\TemplateNormalizations\NormalizeElementNames
4	s9e\TextFormatter\Configurator\TemplateNormalizations\FixUnescapedCurlyBracesInHtmlAttributes
5	s9e\TextFormatter\Configurator\TemplateNormalizations\EnforceHTMLOmittedEndTags
6	s9e\TextFormatter\Configurator\TemplateNormalizations\InlineCDATA
7	s9e\TextFormatter\Configurator\TemplateNormalizations\InlineElements
8	s9e\TextFormatter\Configurator\TemplateNormalizations\InlineTextElements
9	s9e\TextFormatter\Configurator\TemplateNormalizations\UninlineAttributes
10	s9e\TextFormatter\Configurator\TemplateNormalizations\MinifyXPathExpressions
11	s9e\TextFormatter\Configurator\TemplateNormalizations\NormalizeAttributeNames
12	s9e\TextFormatter\Configurator\TemplateNormalizations\OptimizeConditionalAttributes
13	s9e\TextFormatter\Configurator\TemplateNormalizations\FoldArithmeticConstants
14	s9e\TextFormatter\Configurator\TemplateNormalizations\FoldConstantXPathExpressions
15	s9e\TextFormatter\Configurator\TemplateNormalizations\InlineXPathLiterals
16	s9e\TextFormatter\Configurator\TemplateNormalizations\DeoptimizeIf
17	s9e\TextFormatter\Configurator\TemplateNormalizations\OptimizeChooseDeadBranches
18	s9e\TextFormatter\Configurator\TemplateNormalizations\OptimizeChooseAttributes
19	s9e\TextFormatter\Configurator\TemplateNormalizations\OptimizeChooseText
20	s9e\TextFormatter\Configurator\TemplateNormalizations\OptimizeChoose
21	s9e\TextFormatter\Configurator\TemplateNormalizations\OptimizeConditionalValueOf
22	s9e\TextFormatter\Configurator\TemplateNormalizations\InlineAttributes
23	s9e\TextFormatter\Configurator\TemplateNormalizations\NormalizeUrls
24	s9e\TextFormatter\Configurator\TemplateNormalizations\InlineInferredValues
25	s9e\TextFormatter\Configurator\TemplateNormalizations\RenameLivePreviewEvent
26	s9e\TextFormatter\Configurator\TemplateNormalizations\SetRelNoreferrerOnTargetedLinks
27	s9e\TextFormatter\Configurator\TemplateNormalizations\MinifyInlineCSS

Remove a normalization

$configurator = new s9e\TextFormatter\Configurator;

echo $configurator->templateNormalizer->normalizeTemplate('<![CDATA[ Will be inlined ]]>'), "\n";

$configurator->templateNormalizer->remove('InlineCDATA');

echo $configurator->templateNormalizer->normalizeTemplate('<![CDATA[ Will not be inlined ]]>');
 Will be inlined 
 Will not be inlined 

Add your own custom normalization

You can append() or prepend() a callback to the template normalizer. It will be called with one argument, a DOMNode that represents the <xsl:template/> element that contains the template, which you can modify normally. At the end, the node is serialized back to XML.

The template normalizer iterates through the list of normalizations until none of them modifies the template or the internal iteration limit is reached. If you create a normalization that adds something to a template without testing whether it's already been added by a previous iteration, it will be run multiple times in a row.

$configurator = new s9e\TextFormatter\Configurator;

// Add a callback that adds a "?" to the template only if there isn't one already
$configurator->templateNormalizer->append(
	function (DOMNode $template)
	{
		if (strpos($template->textContent, '?') === false)
		{
			$template->appendChild($template->ownerDocument->createTextNode('?'));
		}
	}
);

// Add a callback that adds a "!" to the template
$configurator->templateNormalizer->append(
	function (DOMNode $template)
	{
		$template->appendChild($template->ownerDocument->createTextNode('!'));
	}
);

echo $configurator->templateNormalizer->normalizeTemplate('Hello world');
Hello world?!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours