WordML: Translate spaces in fixed-font text

I had problems with Blogger formatting, so I’ve decided to translate all fixed-font spaces in my Word texts into non-breakable spaces in translated Blogger-ready HTML. To do this conversion, I had to find the font of the current range … but it could be stored in the range properties, character style or paragraph style, and the range property could use a proportional font that overrides the character- or paragraph style fixed font.

I’ve defined the xsl:key instructions that extract the paragraph or character style fonts …

<xsl:key name="parafont" match="w:rFonts/@w:ascii" 
use="ancestor::w:style[@w:type = 'paragraph']/@w:styleId" />
<xsl:key name="rangefont" match="w:rFonts/@w:ascii"
use="ancestor::w:style[@w:type = 'character']/@w:styleId" />

… and used them in pretty complex (as it has to handle so many cases) xsl:choose statement in the w:t (text-within-range) template:

<xsl:template match="w:t/text()">
<xsl:variable name="pfont"
select="key('parafont',ancestor::w:p/w:pPr/w:pStyle/@w:val)" />
<xsl:variable name="rfont"
select="key('rangefont',ancestor::w:r/w:rPr/w:rStyle/@w:val)" />
<xsl:variable name="font" select="w:rPr/w:rFonts/@w:ascii" />
<xsl:variable name="xlate" select="translate(.,' ','&#160;')" />
<xsl:choose>
<xsl:when test="contains($font,'Courier')">
<xsl:value-of select="$xlate" /></xsl:when>
<xsl:when test="string($font) != ''">
<xsl:value-of select="." /></xsl:when>
<xsl:when test="contains($rfont,'Courier')">
<xsl:value-of select="$xlate" /></xsl:when>
<xsl:when test="string($rfont) != ''">
<xsl:value-of select="." /></xsl:when>
<xsl:when test="contains($pfont,'Courier')">
<xsl:value-of select="$xlate" /></xsl:when>
<xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
</xsl:choose>
</xsl:template>

No comments:

Post a Comment