[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Center string


Subject: Re: [xsl] Center string
From: Frédéric Schwebel <Fred.Schwebel@xxxxxxxxxxx>
Date: Wed, 17 Jun 2009 17:46:56 +0200

Thanks for your code, Michael, I'll stick to mine, it's less compact
and less "rest-of-the-sheet" independant but it's a bit better with
balancing words between lines. Here it is (sorry most of the names are
french). The first "xsl:when" is dealing with the case where a word is
longer than the max line length.

<!-- fonction de mise en ligne des titres avant centrage -->
<xsl:function name="doc:centreTitre" as="xs:string*">
	<xsl:param name="mots" as="xs:string*" />
	<xsl:param name="longueurIdeale" as="xs:integer" />
	<xsl:param name="longueurMax" as="xs:integer" />
	<xsl:param name="dejaFait" as="xs:string?" />

	<xsl:variable name="derniereLigne" as="xs:string"
select='functx:substring-after-last($dejaFait,$sautAGenerer)' />
	<xsl:variable name="motCourant" as="xs:string"
select="translate($mots[1],$carcoup,'')" />
	<xsl:variable name="longueurIdealeCourante" as="xs:integer">
		<xsl:choose>
			<xsl:when test="($longueurIdeale + string-length($motCourant) + 1)
&lt;= $longueurMax">
				<xsl:value-of select="xs:integer($longueurIdeale +
round(string-length($motCourant) div 2))" />
			</xsl:when>
			<xsl:otherwise><xsl:value-of select="$longueurIdeale" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="derniereLigneAvecMot" as="xs:string"
select="concat($derniereLigne,$espace,$motCourant)" />
	<xsl:variable name="finAvecMot" as="xs:string"
select="translate(string-join($mots,$espace),$carcoup,'')" />

	<xsl:variable name="motMisEnPage" as="xs:string">
		<xsl:choose>
			<!-- cas 0 : un mot plus long que la longueur max -->
			<xsl:when test="string-length($motCourant) > $longueurMax">
				<xsl:choose>
					<!-- le mot rentre dans la longueur max, on le coupe pas -->
					<xsl:when test="string-length($motCourant) &lt;= $longueur">
						<xsl:variable name="motSansCoupe">
							<xsl:if test="string-length($derniereLigne) > 0"><xsl:value-of
select="$sautAGenerer" /></xsl:if>
							<xsl:value-of select="$motCourant" />
						</xsl:variable>
						<xsl:value-of select="string-join($motSansCoupe,'')" />
					</xsl:when>
					<!-- sinon mode sagouin et zut -->
					<xsl:otherwise>
						<xsl:variable name="motSansCoupe" select="substring($motCourant,1,
								$longueurMax -
string-length($derniereLigne)-1-xs:integer(string-length($derniereLigne)
> 0))" />
						<!-- <xsl:message select="$motSansCoupe" />
						<xsl:message select="$longueurMax -
string-length($derniereLigne)-1" /> -->
						<xsl:variable name="motCoupeSagouin">
							<xsl:if test="string-length($derniereLigne) > 0">
								<xsl:value-of select="$espace" />
							</xsl:if>
							<xsl:value-of select="$motSansCoupe" />
							<xsl:if test="string-length($motSansCoupe) > 0">
								<xsl:choose>
									<xsl:when
test="starts-with($mots[1],$debMath)"><xsl:text>&pt5;</xsl:text></xsl:when>
									<xsl:otherwise><xsl:text>&pt36;</xsl:text></xsl:otherwise>
								</xsl:choose>
							</xsl:if>
							<xsl:value-of select="$sautAGenerer" />
						</xsl:variable>
						<xsl:variable name="motCoupeSagouin2">
							<xsl:value-of select="$motCoupeSagouin" />
							<xsl:value-of
select="doc:centreTitre(substring-after($motCourant,$motSansCoupe),
								$longueurIdeale, $longueurMax,
concat(functx:trim($dejaFait),functx:trim(string-join($motCoupeSagouin,''))))
"
/>
						</xsl:variable>
						<xsl:value-of select="string-join($motCoupeSagouin2,'')" />
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<!-- cas 1 : on est en dibut de ligne -->
			<xsl:when test="string-length($derniereLigne)=0">
				<xsl:value-of select="$motCourant" />
			</xsl:when>
			<!-- cas 2 : le mot rentre dans la ligne de (longueur ideale +
moitie longueur mot courant) -->
			<xsl:when test="string-length($derniereLigneAvecMot) &lt;=
$longueurIdealeCourante">
				<xsl:value-of select="concat($espace,$motCourant)" />
			</xsl:when>
			<!-- cas 3 : faut passer ` la ligne -->
			<xsl:otherwise>
				<xsl:value-of select="concat($sautAGenerer,$motCourant)" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:value-of select="functx:trim($motMisEnPage)" />

	<xsl:if test="(count($mots) > 1) and not($motMisEnPage =
concat($espace,$finAvecMot))">
		<xsl:value-of select="doc:centreTitre($mots[position() > 1],
$longueurIdeale,
$longueurMax,	concat(functx:trim($dejaFait),functx:trim($motMisEnPage)))"
/>
	</xsl:if>
</xsl:function>


2009/6/12 Michael Ludwig <milu71@xxxxxx>:
> Emmanuel Bigui schrieb am 12.06.2009 um 01:57:04 (+0200):
>>
>> Dealing with a minimum / maximum line length can be done with
>> regexp: they help produce, I think, more legible stylesheets
>
> Of course, regular expression! That's certainly an improvement
> over the low-level code that I produced after some battling in
> the trenches to determine how exactly to pass the parameters.
>
>> -- although it may be a question of taste.
>
> Regular expressions have a tendency to be pretty compact, but I
> wouldn't say that's a disadvantage.
>
> Michael Ludwig


Current Thread