Skip to content

Commit

Permalink
fix(xsl): update paragrpah and document language detection
Browse files Browse the repository at this point in the history
  • Loading branch information
NPavie committed Jun 19, 2024
1 parent b3507b5 commit 99f4030
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 67 deletions.
45 changes: 40 additions & 5 deletions Common/DaisyConverterLib/resources/xslts/Common3.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -2164,6 +2164,37 @@
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="paragraphLatin">
<xsl:choose>
<xsl:when test="$paragraphStyleId and $styles/w:style[@w:type='paragraph' and @w:styleId=$paragraphStyleId]/w:rPr/w:lang/@w:val">
<xsl:value-of select="$styles/w:style[@w:type='paragraph' and @w:styleId=$paragraphStyleId]/w:rPr/w:lang/@w:val" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$defaultLatin"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="paragraphEastAsia">
<xsl:choose>
<xsl:when test="$paragraphStyleId and $styles/w:style[@w:type='paragraph' and @w:styleId=$paragraphStyleId]/w:rPr/w:lang/@w:eastAsia">
<xsl:value-of select="$styles/w:style[@w:type='paragraph' and @w:styleId=$paragraphStyleId]/w:rPr/w:lang/@w:eastAsia" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$defaultEastAsia"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="paragraphComplex">
<xsl:choose>
<xsl:when test="$paragraphStyleId and $styles/w:style[@w:type='paragraph' and @w:styleId=$paragraphStyleId]/w:rPr/w:lang/@w:bidi">
<xsl:value-of select="$styles/w:style[@w:type='paragraph' and @w:styleId=$paragraphStyleId]/w:rPr/w:lang/@w:bidi" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$defaultComplex"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<!-- deduce languages from runner first-->
<xsl:variable name="runnerLanguages">
<xsl:for-each select="$paragraphNode/w:r">
Expand Down Expand Up @@ -2193,17 +2224,21 @@
</xsl:for-each>
</xsl:variable>
<xsl:choose>
<!-- Prioritize the language count -->
<xsl:when test="msxsl:node-set($languagesSorted)/lang[1]/@val">
<xsl:value-of select="msxsl:node-set($languagesSorted)/lang[1]/@val"/>
</xsl:when>
<xsl:when test="w:rPr/w:eastAsianLayout or (w:rPr/w:rFonts/@w:hint='eastAsia')">
<xsl:value-of select="$defaultEastAsia"/>
<!-- then check if east asia is used as default -->
<xsl:when test="w:rPr/w:eastAsianLayout or (w:rPr/w:rFonts/@w:hint='eastAsia') or (w:pPr/w:rPr/w:rFonts/@w:hint='eastAsia')">
<xsl:value-of select="$paragraphEastAsia"/>
</xsl:when>
<xsl:when test="w:rPr/w:cs or (w:rPr/w:rFonts/@w:hint='cs')">
<xsl:value-of select="$defaultComplex"/>
<!-- then check if complex is used as default -->
<xsl:when test="w:rPr/w:cs or (w:rPr/w:rFonts/@w:hint='cs') or (w:pPr/w:rPr/w:rFonts/@w:hint='cs')">
<xsl:value-of select="$paragraphComplex"/>
</xsl:when>
<!-- default as latin -->
<xsl:otherwise>
<xsl:value-of select="$defaultLatin"/>
<xsl:value-of select="$paragraphLatin"/>
</xsl:otherwise>
</xsl:choose>

Expand Down
98 changes: 36 additions & 62 deletions Common/DaisyConverterLib/resources/xslts/oox2Daisy.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:dcmitype="http://purl.org/dc/dcmitype/"
xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
xmlns:myObj="urn:Daisy" exclude-result-prefixes="w pic wp dcterms xsi cp dc a r v vt dcmitype myObj">
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:myObj="urn:Daisy" exclude-result-prefixes="w pic wp dcterms xsi cp dc a r v vt dcmitype myObj msxsl">
<!--Imports all the XSLT-->
<xsl:import href="Common.xsl"/>
<!--Implements Table of Contents-->
Expand Down Expand Up @@ -267,67 +268,40 @@
<meta name="dc:Identifier" content="{$UID}"/>
</xsl:otherwise>
</xsl:choose>
<meta name="dc:Language" content="{$doclang}"/>
<xsl:variable name="insertLangDefault" select="myObj:AddLanguage($doclang)"/>
<xsl:message terminate="no">progress:Parsing document languages</xsl:message>
<xsl:for-each select="document('word/document.xml')//w:body/w:p/w:r/w:rPr">
<xsl:if test="w:lang">
<xsl:choose>
<xsl:when test="w:rFonts/@w:hint='cs'">
<xsl:choose>
<xsl:when test="w:lang/@w:bidi">
<xsl:if test="myObj:AddLanguage(w:lang/@w:bidi)=1">
<meta name="dc:Language" content="{w:lang/@w:bidi}"/>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:if test="myObj:AddLanguage($doclangbidi)=1">
<meta name="dc:Language" content="{$doclangbidi}"/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="w:rFonts/@w:hint='eastAsia'">
<xsl:choose>
<xsl:when test="w:lang/@w:eastAsia">
<xsl:if test="myObj:AddLanguage(w:lang/@w:eastAsia)=1">
<meta name="dc:Language" content="{w:lang/@w:eastAsia}"/>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:if test="myObj:AddLanguage($doclangeastAsia)=1">
<meta name="dc:Language" content="{$doclangeastAsia}"/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="w:lang/@w:val">
<xsl:if test="myObj:AddLanguage(w:lang/@w:val)=1">
<meta name="dc:Language" content="{w:lang/@w:val}"/>
</xsl:if>
</xsl:when>
<xsl:when test="w:lang/@w:eastAsia">
<xsl:if test="myObj:AddLanguage(w:lang/@w:eastAsia)=1">
<meta name="dc:Language" content="{w:lang/@w:eastAsia}"/>
</xsl:if>
</xsl:when>
<xsl:when test="w:lang/@w:bidi">
<xsl:if test="myObj:AddLanguage(w:lang/@w:bidi)=1">
<meta name="dc:Language" content="{w:lang/@w:bidi}"/>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:if test="myObj:AddLanguage($doclang)=1">
<meta name="dc:Language" content="{$doclang}"/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>

<xsl:message terminate="no">progress:Parsing document languages</xsl:message>
<!-- Compute all content languages -->
<!-- deduce languages from paragraphes -->
<xsl:variable name="paragraphLanguages">
<xsl:for-each select="document('word/document.xml')//w:body//w:p">
<xsl:variable name="found">
<xsl:call-template name="GetParagraphLanguage">
<xsl:with-param name="paragraphNode" select="." />
</xsl:call-template>
</xsl:variable>
<lang val="{$found}" />
</xsl:for-each>
</xsl:variable>
<!-- Count languages -->
<xsl:variable name="uniqLanguages">
<xsl:for-each select="msxsl:node-set($paragraphLanguages)/lang">
<xsl:variable name="currentVal" select="@val"/>
<xsl:if test="count(preceding-sibling::lang[@val=$currentVal])=0">
<lang val="{$currentVal}"
count="{count(following-sibling::lang[@val=$currentVal]) + 1}" />
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="languagesSorted">
<xsl:for-each select="msxsl:node-set($uniqLanguages)/lang">
<xsl:sort select="@count" data-type="number" order="descending"/>
<lang val="{@val}" count="{@count}" />
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="msxsl:node-set($languagesSorted)/lang">
<xsl:variable name="insertLangDefault" select="myObj:AddLanguage(@val)"/>
<meta name="dc:Language" content="{@val}"/>
</xsl:for-each>
<!--End of Head element-->
</head>
<!--Starting Book Element-->
Expand Down

0 comments on commit 99f4030

Please sign in to comment.