RE: keeping word together (not breaking words onto next line) - XSLT code if interested

Just in case someone else might have this issue and need a piece of code to not break words on certain symbols.

I have my own quirky variable naming convention, they are prefixed with their scope. Also this code works with straight strings and not mixed content but can be modified to do this with a little work.

<xsl:variable name="l_Title">
   <xsl:call-template name="ct_KeepWordTogether">
      <xsl:with-param name="p_String" select="string to parse goes here" as="xs:string" />
   </xsl:call-template>   
</xsl:variable>

<fo:marker marker-class-name="partsPageTitle"><xsl:copy-of select="$l_Title" /></fo:marker>

<xsl:template name="ct_KeepWordTogether">
   <xsl:param name="p_String" select="''" as="xs:string" />
   <xsl:param name="p_Counter" select="1" as="xs:double" />
   <xsl:variable name="l_ToSpaceOrNotToSpace" select="if ($p_Counter = 1) then '' else ' '" />

   <xsl:choose>
      <xsl:when test="contains($p_String, ' ')">
         <xsl:variable name="l_StringToParse" select="substring-before($p_String, ' ')" />

         <xsl:choose>
            <xsl:when test="contains($l_StringToParse, '-') or contains($l_StringToParse, '/') or contains($l_StringToParse, '\')">
               <xsl:value-of select="$l_ToSpaceOrNotToSpace" /><fo:inline keep-together.within-line="always"><xsl:value-of select="$l_StringToParse" /></fo:inline>
            </xsl:when>
            <xsl:otherwise>
               <xsl:value-of select="concat($l_ToSpaceOrNotToSpace, $l_StringToParse)" />
            </xsl:otherwise>
         </xsl:choose>

         <xsl:call-template name="ct_KeepWordTogether">
            <xsl:with-param name="p_String" select="substring-after($p_String, ' ')" as="xs:string" />
            <xsl:with-param name="p_Counter" select="$p_Counter + 1" as="xs:double" />
         </xsl:call-template>
      </xsl:when>

      <xsl:otherwise>
         <xsl:choose>
            <xsl:when test="contains($p_String, '-') or contains($p_String, '/') or contains($p_String, '\')">
               <xsl:value-of select="$l_ToSpaceOrNotToSpace" /><fo:inline keep-together.within-line="always"><xsl:value-of select="$p_String" /></fo:inline>
            </xsl:when>
            <xsl:otherwise>
               <xsl:value-of select="concat($l_ToSpaceOrNotToSpace, $p_String)" />
            </xsl:otherwise>
         </xsl:choose>
      </xsl:otherwise>
   </xsl:choose>

</xsl:template>

Marijan (Mario) Madunic
Publishing Specialist
New Flyer Industries


-----Original Message-----
From: www-xsl-fo-request@w3.org [mailto:www-xsl-fo-request@w3.org] On Behalf Of Mario Madunic
Sent: Tuesday, December 21, 2010 7:44 AM
To: www-xsl-fo@w3.org
Subject: RE: keeping word together (not breaking words onto next line)

Short answer is it can be done while creating the marker.

Marijan (Mario) Madunic
Publishing Specialist
New Flyer Industries

-----Original Message-----
From: www-xsl-fo-request@w3.org [mailto:www-xsl-fo-request@w3.org] On Behalf Of Mario Madunic
Sent: Tuesday, December 21, 2010 6:31 AM
To: www-xsl-fo@w3.org
Cc: G. Ken Holman
Subject: RE: keeping word together (not breaking words onto next line)

Thanks Jeremias,

Seems I forgot to put one twist into the mix. The title is a marker. So did some testing with the xsl:fo source by adding the fo:inline manually and parsing it.

<fo:marker marker-class-name="partsPageTitle">FLUSH WINDOW ASSEMBLY, NON-EMERGENCY <fo:inline keep-together.within-line="always">TIP-IN/FIXED</fo:inline></fo:marker>

Works like a charm. So my question is when defining the marker "partsPagetitle" can I place the fo:inline there while creating the xsl:fo or will I need to do a pass on the FO and add the fo:inline during this step. Not a big deal to parse the FO but do not want to go up the wrong path while creating a called-template to parse the string, find any word containing symbols, and then wrap the string in fo:inline.

Marijan (Mario) Madunic
Publishing Specialist
New Flyer Industries

-----Original Message-----
From: www-xsl-fo-request@w3.org [mailto:www-xsl-fo-request@w3.org] On Behalf Of Jeremias Maerki
Sent: Tuesday, December 21, 2010 2:02 AM
To: www-xsl-fo@w3.org
Subject: Re: keeping word together (not breaking words onto next line)

Have you had the "TIP-IN/FIXED" enclosed in an fo:inline? That should
really work. Otherwise, it's a bug (or missing functionality). Using
that on fo:block, it prevents the whole block text from being broken.

As an alternative, you can try to use word joiners (&#x2060;) to keep
the formatter from breaking a word apart at a particular place.

I've verified both approaches with Apache FOP 1.0:

      <fo:block-container width="11.3cm" background-color="lightgray">
        <fo:block>FLUSH WINDOW ASSEMBLY, NON-EMERGENCY TIP-IN/FIXED</fo:block>
        <fo:block keep-together.within-line="always">FLUSH WINDOW ASSEMBLY, NON-EMERGENCY TIP-IN/FIXED</fo:block>
        <fo:block>FLUSH WINDOW ASSEMBLY, NON-EMERGENCY <fo:inline keep-together.within-line="always">TIP-IN/FIXED</fo:inline></fo:block>
        <fo:block>FLUSH WINDOW ASSEMBLY, NON-EMERGENCY TIP-&#x2060;IN/&#x2060;FIXED</fo:block>
      </fo:block-container>

Some FO formatters implement Unicode UAX #14 for line breaking:
http://unicode.org/reports/tr14/
That document may give you additional ideas on how to control
line-breaking behaviour.

On 20.12.2010 17:16:02 Mario Madunic wrote:
> After doing some investigation by playing with the string "FLUSH WINDOW
> ASSEMBLY, NON-EMERGENCY TIP-IN/FIXED", I found that the culprit is the
> "-". When I remove it from the string I get the behaviour I'm looking
> for without using @keep-together.within-line. Is there a way to modify
> this behaviour?
> 
> Marijan (Mario) Madunic
> Publishing Specialist
> New Flyer Industries
> 
> -----Original Message-----
> From: www-xsl-fo-request@w3.org [mailto:www-xsl-fo-request@w3.org] On Behalf Of Mario Madunic
> Sent: Monday, December 20, 2010 9:47 AM
> To: www-xsl-fo@w3.org
> Subject: RE: keeping word together (not breaking words onto next line)
> 
> Thanks Jeremias, but it is keeping the entire string on one line instead of forcing an entire word to the next line. I'm looking at keeping words together when they would span two lines. Is there a group of attributes that I should be looking at?
> 
> Example:
> FLUSH WINDOW ASSEMBLY, NON-EMERGENCY TIP-IN/FIXED
> 
> Should become
> FLUSH WINDOW ASSEMBLY, NON-EMERGENCY
> TIP-IN/FIXED
> 
> Not
> FLUSH WINDOW ASSEMBLY, NON-EMERGENCY TIP-IN/
> FIXED
> 
> Marijan (Mario) Madunic
> Publishing Specialist
> New Flyer Industries
> 
> -----Original Message-----
> From: www-xsl-fo-request@w3.org [mailto:www-xsl-fo-request@w3.org] On Behalf Of Jeremias Maerki
> Sent: Monday, December 20, 2010 8:31 AM
> To: www-xsl-fo@w3.org
> Subject: Re: keeping word together (not breaking words onto next line)
> 
> <fo:block keep-together.within-line="always">My Title</fo:block>
> 
> ...or the same with fo:inline.
> 
> HTH
> 
> On 20.12.2010 14:52:13 Mario Madunic wrote:
> > I’m trying to keep words in titles together when they would break onto
> > two lines. An example would be when a title has the following
> > text“TIP-IN/FIXED”. Currently it is breaking the string at the “/
> > ” and placing “FIXED” on the next line. What is required is the
> > whole string on the next line. How can this be accomplished.
> > 
> > Marijan (Mario) Madunic
> > Publishing Specialist
> > New Flyer Industries


Jeremias Maerki



--------------------------------------------------------------------
Please consider the environment before printing this e-mail.

CONFIDENTIALITY STATEMENT: This communication (and  any and all information or material transmitted with this communication) is confidential, may be privileged and is intended only for the use of the intended recipient. If you are not the intended recipient, any review, retransmission, circulation, distribution, reproduction, conversion to hard copy, copying or other use of this communication, information or material is strictly prohibited and may be illegal. If you received this communication in error or if it is forwarded to you without the express authorization of New Flyer, please notify us immediately by telephone or by return email and permanently delete the communication, information and material from any computer, disk drive, diskette or other storage device or media. Thank you.



--------------------------------------------------------------------
Please consider the environment before printing this e-mail.

CONFIDENTIALITY STATEMENT: This communication (and  any and all information or material transmitted with this communication) is confidential, may be privileged and is intended only for the use of the intended recipient. If you are not the intended recipient, any review, retransmission, circulation, distribution, reproduction, conversion to hard copy, copying or other use of this communication, information or material is strictly prohibited and may be illegal. If you received this communication in error or if it is forwarded to you without the express authorization of New Flyer, please notify us immediately by telephone or by return email and permanently delete the communication, information and material from any computer, disk drive, diskette or other storage device or media. Thank you.



--------------------------------------------------------------------
Please consider the environment before printing this e-mail.

CONFIDENTIALITY STATEMENT: This communication (and  any and all information or material transmitted with this communication) is confidential, may be privileged and is intended only for the use of the intended recipient. If you are not the intended recipient, any review, retransmission, circulation, distribution, reproduction, conversion to hard copy, copying or other use of this communication, information or material is strictly prohibited and may be illegal. If you received this communication in error or if it is forwarded to you without the express authorization of New Flyer, please notify us immediately by telephone or by return email and permanently delete the communication, information and material from any computer, disk drive, diskette or other storage device or media. Thank you.

Received on Tuesday, 21 December 2010 14:24:59 UTC