Author:
jensscherbl
Version:
1.0
Release Date:
14 Aug 2011
Category:
String Operations

Description

Pimped the nl2br utility to wrap plaintext paragraphs with HTML paragraph elements before inserting HTML line breaks in a string.

Credits to nl2br, only changed and added a few lines.

XSLT

View Raw
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<!--

	Example:

	<xsl:call-template name="plaintext-paragraphs">
		<xsl:with-param name="string" select="body"/>
	</xsl:call-template>
	
	-->

	<xsl:template name="plaintext-paragraphs">
		<xsl:param name="string"/>	
		<xsl:choose>
			<xsl:when test="contains($string,'&#10;&#10;')">
				<p>				
					<xsl:call-template name="nl2br">
						<xsl:with-param name="string" select="substring-before($string,'&#10;&#10;')"/>
					</xsl:call-template>
				</p>				
				<xsl:call-template name="plaintext-paragraphs">
					<xsl:with-param name="string" select="substring-after($string,'&#10;&#10;')"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<p>				
					<xsl:call-template name="nl2br">
						<xsl:with-param name="string" select="$string"/>
					</xsl:call-template>
				</p>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="nl2br">
		<xsl:param name="string"/>
		<xsl:value-of select="normalize-space(substring-before($string,'&#10;'))"/>
		<xsl:choose>
			<xsl:when test="contains($string,'&#10;')">
				<br />
				<xsl:call-template name="nl2br">
					<xsl:with-param name="string" select="substring-after($string,'&#10;')"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$string"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>

Discuss this XSLT Utility

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details