Skip to content

Commit

Permalink
Better handling of tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
ottoville committed Sep 20, 2015
1 parent 3073d7b commit 2239087
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 41 deletions.
16 changes: 15 additions & 1 deletion docx2html.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
ul>li>p:first-child {display:inline;}
ul.outsidee { list-style-position:outside} ul.outsidee>li>p:first-child { text-indent:0 !important;display:block}
ul>li>p:not(:first-child) { text-indent:0 }
ul, li { margin:0;padding:0 } li p {} input[type="text"] {height:18px} p {margin:0}
ul, li { margin:0;padding:0 } li p {} input[type="text"] {height:18px} input[type="checkbox"] { margin:0 } p {margin:0}
article>* {display:none} article>section {display:block; margin-top:0.1cm;border-width:1px;border-style:solid;}
<xsl:value-of select="base-uri(.)"/>
<xsl:for-each select="document(resolve-uri('styles.xml',base-uri()))/w:styles/w:style">
Expand Down Expand Up @@ -245,6 +245,20 @@
</xsl:call-template>
</xsl:for-each>
</article>
<script>
<xsl:text>
<![CDATA[
var tabs=document.getElementsByClassName('tab');
for(t=0;t<tabs.length;t++) {
var tabinterval=parseFloat(tabs[t].style.minWidth);
var realwidth=parseFloat(window.getComputedStyle(tabs[t]).width)
if(realwidth>tabinterval) {
tabs[t].style.width=Math.ceil(realwidth/tabinterval)*tabinterval+'px';
}
}
]]>
</xsl:text>
</script>
</xsl:template>
<xsl:template name="section">
<xsl:param name="reldocument" />
Expand Down
7 changes: 1 addition & 6 deletions paragraphs.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,7 @@
</xsl:if>
</xsl:variable>
<p class="{normalize-space($class)}" pid="{$currentid}">
<xsl:apply-templates select="w:pPr|w:r[
(w:fldChar[@w:fldCharType='begin'] or
(not(w:fldChar|w:instrText) and not(./preceding-sibling::w:r[w:fldChar][1]/w:fldChar/@w:fldCharType='separate')))
and
not(local-name(./preceding-sibling::*[1])='r' and not(w:tab) and deep-equal(./w:rPr|./w:fldChar, ./preceding-sibling::*[1]/w:rPr|./preceding-sibling::*[1]/w:fldChar))
]|w:ins|w:hyperlink|w:sdt">
<xsl:apply-templates select="w:pPr|w:r[(w:tab or not(./following-sibling::w:r[w:tab]))]|w:ins|w:hyperlink|w:sdt">
<xsl:with-param name="scopeselector">p[pid='<xsl:value-of select="$currentid"/>']</xsl:with-param>
<xsl:with-param name="listintend" select="$listintend" />
<xsl:with-param name="reldocument" select="$reldocument" />
Expand Down
83 changes: 49 additions & 34 deletions text.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,53 @@
<xsl:when test="local-name(.)='u'">text-decoration:underline;</xsl:when>
</xsl:choose>
</xsl:for-each>
<xsl:if test="../w:tab[not(./preceding-sibling::w:r)]">
padding-left:<xsl:value-of select="(count(../w:tab[not(./preceding-sibling::w:r)]) * number(document(resolve-uri('settings.xml',base-uri()))/w:settings/w:defaultTabStop/@w:val)) div 20"/>pt;
</xsl:if>
</xsl:template>
<xsl:template match="w:tab[./preceding-sibling::w:r]">
<xsl:variable name="tabstop" select="number(document(resolve-uri('settings.xml',base-uri()))/w:settings/w:defaultTabStop/@w:val) div 20"/>
<xsl:choose>
<xsl:when test="count(../../w:pPr/w:ind[@w:hanging]) and count(./preceding-sibling::w:r/w:tab)=0">
<br>
<xsl:attribute name="style">width:<xsl:value-of select="$tabstop"/>pt;display:inline-block</xsl:attribute>
</br>
<br style="{'width:18pt;display:inline-block'}"/>
</xsl:when>
<xsl:otherwise>
<span><xsl:attribute name="style"><xsl:attribute name="style">width:<xsl:value-of select="$tabstop"/>pt;display:inline-block</xsl:attribute></xsl:attribute> </span>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="w:t">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template name="text" match="w:r">
<xsl:template match="w:r[w:tab]">
<xsl:param name="reldocument" />
<xsl:variable name="prevtab" select="preceding-sibling::*[w:tab][1]" />
<xsl:variable name="tabstop" select="(number(document(resolve-uri('settings.xml',base-uri()))/w:settings/w:defaultTabStop/@w:val) div 20) * (4 div 3) "/>
<span>
<xsl:attribute name="class">tab</xsl:attribute>
<xsl:attribute name="style">
display:inline-block;
min-width:<xsl:value-of select="$tabstop"/>px;
text-indent:0;
</xsl:attribute>
<xsl:choose>
<xsl:when test="not($prevtab)">
<xsl:apply-templates select="./preceding-sibling::w:r[(w:fldChar[@w:fldCharType='begin'] or
(not(w:fldChar|w:instrText) and not(./preceding-sibling::w:r[w:fldChar][1]/w:fldChar/@w:fldCharType='separate')))]">
<xsl:with-param name="reldocument" select="$reldocument" />
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="$prevtab">
<xsl:call-template name="text">
<xsl:with-param name="reldocument" select="$reldocument" />
</xsl:call-template>
</xsl:for-each>
<xsl:apply-templates select="./preceding-sibling::*[(count(./preceding-sibling::*) &gt; count($prevtab/preceding-sibling::*)) and (w:fldChar[@w:fldCharType='begin'] or
(not(w:fldChar|w:instrText) and not(./preceding-sibling::w:r[w:fldChar][1]/w:fldChar/@w:fldCharType='separate')))]">
<xsl:with-param name="reldocument" select="$reldocument" />
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</span>
<xsl:if test="../w:pPr/w:ind/@w:hanging and not(./preceding-sibling::w:r[w:tab])">
<br />
</xsl:if>
<xsl:if test="not(./following-sibling::w:r[w:tab])">
<xsl:call-template name="text">
<xsl:with-param name="reldocument" select="$reldocument" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="fieldtext" match="w:r[./preceding-sibling::w:r[w:fldChar][1]/w:fldChar/@w:fldCharType='separate']">
</xsl:template>
<xsl:template name="text" match="w:r[not(w:tab) and not(./preceding-sibling::w:r[w:fldChar][1]/w:fldChar/@w:fldCharType='separate')]">
<xsl:param name="reldocument" />
<xsl:variable name="class">
<xsl:if test="count(w:rPr/w:rStyle)">
Expand All @@ -73,26 +98,14 @@
<xsl:value-of select="concat(' ',../../w:pPr/w:rPr/w:rStyle/@w:val)"/>
</xsl:if>
</xsl:variable>
<xsl:variable name="thisid" select="generate-id(.)"/>
<xsl:variable name="nextdifferent" select="./following-sibling::*[not(local-name(.)=local-name(current()) and not(w:tab) and deep-equal(./w:rPr,current()/w:rPr))][1]"/>
<span>
<xsl:attribute name="class" select="normalize-space($class)"/>
<xsl:attribute name="style">
<xsl:apply-templates select="w:rPr"/>
</xsl:attribute>
<xsl:apply-templates select="w:t|w:tab|w:drawing">
<xsl:apply-templates select="w:t|w:drawing">
<xsl:with-param name="reldocument" select="$reldocument" />
</xsl:apply-templates>
<xsl:if test="count(./following-sibling::*)!=0 and (generate-id(./following-sibling::*[1])!=generate-id($nextdifferent))">
<xsl:apply-templates select="./following-sibling::*/w:t intersect $nextdifferent/preceding-sibling::*/w:t">
<xsl:with-param name="reldocument" select="$reldocument" />
</xsl:apply-templates>
</xsl:if>
<xsl:if test="count(./following-sibling::*)!=0 and count($nextdifferent)=0">
<xsl:apply-templates select="./following-sibling::*/w:tab|./following-sibling::*/w:t">
<xsl:with-param name="reldocument" select="$reldocument" />
</xsl:apply-templates>
</xsl:if>
</span>
</xsl:template>
<xsl:template match="w:r[w:fldChar[@w:fldCharType='begin']]">
Expand Down Expand Up @@ -129,9 +142,11 @@
<xsl:attribute name="style">
<xsl:apply-templates select="w:rPr"/>
</xsl:attribute>
<xsl:apply-templates select="$text">
<xsl:with-param name="reldocument" select="$reldocument" />
</xsl:apply-templates>
<xsl:for-each select="$text">
<xsl:call-template name="text">
<xsl:with-param name="reldocument" select="$reldocument" />
</xsl:call-template>
</xsl:for-each>
</a>
</xsl:when>
<xsl:otherwise>
Expand Down

0 comments on commit 2239087

Please sign in to comment.