-
Notifications
You must be signed in to change notification settings - Fork 10
/
doc2fb.xsl
439 lines (435 loc) · 16.3 KB
/
doc2fb.xsl
1
<?xml version="1.0" encoding="UTF-8"?><!--This file was previously originated by Oleg Burau and slightly modified by Ildar ShaimordanovOleg Burauhttp://home.arcor.de/fb.toolsIldar Shaimordanovhttp://code.google.com/p/jsxt/--><xsl:stylesheet xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:st1="urn:schemas-microsoft-com:office:smarttags" exclude-result-prefixes="#default fo w v w10 sl aml wx o dt st1" version="1.0"> <xsl:output encoding="UTF-8" method="xml" indent="no"/> <!-- variables --> <xsl:variable name="pStyles" select="/w:wordDocument/w:styles/w:style[@w:type='paragraph']"/> <xsl:variable name="stdPStyleSize" select="/w:wordDocument/w:styles/w:style[@w:type='paragraph' and @w:default='on']/w:rPr/w:sz/@w:val"/> <!-- root --> <xsl:template match="/"> <FictionBook> <description> <title-info> <genre/> <author> <first-name> <xsl:value-of select="/w:wordDocument/o:DocumentProperties/o:Author"/> </first-name> <last-name/> </author> <book-title> <xsl:value-of select="/w:wordDocument/o:DocumentProperties/o:Title"/> </book-title> <date> <xsl:value-of select="substring(/w:wordDocument/o:DocumentProperties/o:Created,1,10)"/> </date> <xsl:if test="//w:p/w:r/w:pict/w:binData/@w:name[1]"> <coverpage> <image l:href="#doc2fb_image_{substring-after(//w:p/w:r/w:pict/w:binData/@w:name[1],'wordml://')}"/> </coverpage> </xsl:if> <lang> <xsl:value-of select="//w:p[1]/w:pPr/w:rPr/w:lang[1]/@w:val"/> </lang> </title-info> <document-info> <author> <first-name> <!-- <xsl:value-of select="/w:wordDocument/o:DocumentProperties/o:LastAuthor"/> --> </first-name> <last-name/> </author> <program-used>doc2html /f:fb2</program-used> <date> <xsl:value-of select="substring(/w:wordDocument/o:DocumentProperties/o:LastSaved,1,10)"/> </date> <version> <xsl:value-of select="/w:wordDocument/o:DocumentProperties/o:Revision"/> </version> </document-info> </description> <body> <xsl:apply-templates/> </body> <xsl:if test="count(//w:p/w:r/w:footnote)>0 or count(//w:p/w:r/w:endnote)>0"> <body name="notes"> <xsl:choose> <xsl:when test="count(//w:p/w:r/w:footnote)>0 and count(//w:p/w:r/w:endnote)>0"> <section> <title> <xsl:value-of select="//w:footnote[1]/w:p/w:pPr/w:pStyle/@w:val"/> </title> <xsl:call-template name="footnote"/> </section> <section> <title> <xsl:value-of select="//w:endnote[1]/w:p/w:pPr/w:pStyle/@w:val"/> </title> <xsl:call-template name="endnote"/> </section> </xsl:when> <xsl:when test="count(//w:p/w:r/w:footnote)>0"> <xsl:call-template name="footnote"/> </xsl:when> <xsl:when test="count(//w:p/w:r/w:endnote)>0"> <xsl:call-template name="endnote"/> </xsl:when> </xsl:choose> </body> </xsl:if> <xsl:for-each select="//w:p/w:r/w:pict"> <xsl:variable name="pictExt" select="substring(w:binData/@w:name, string-length(w:binData/@w:name)-2, 3)"/> <xsl:variable name="pictName" select="substring-after(w:binData/@w:name,'wordml://')"/> <xsl:if test="$pictExt='jpg' or $pictExt='png'"> <xsl:choose> <xsl:when test="$pictExt='png'"> <binary id="doc2fb_image_{$pictName}" content-type="image/png"> <xsl:value-of select="w:binData"/> </binary> </xsl:when> <xsl:when test="$pictExt='jpg'"> <binary id="doc2fb_image_{$pictName}" content-type="image/jpeg"> <xsl:value-of select="w:binData"/> </binary> </xsl:when> </xsl:choose> </xsl:if> </xsl:for-each> </FictionBook> </xsl:template> <!-- document properties --> <xsl:template match="o:DocumentProperties"/> <!-- fonts --> <xsl:template match="w:fonts"/> <!-- styles --> <xsl:template match="w:styles"/> <!-- macros and components --> <xsl:template match="w:docOleData"/> <!-- shapes --> <xsl:template match="w:shapeDefaults"/> <!-- big pictures --> <xsl:template match="w:bgPict"/> <!-- doc properties --> <xsl:template match="w:docPr"/> <xsl:template match="w:fldChar"/> <xsl:template match="w:instrText"/> <!-- section properties for the very last section in the document --> <xsl:template match="w:sectPr"/> <!-- section --> <xsl:template match="wx:sect"> <xsl:call-template name="section"/> </xsl:template> <!-- sub-section --> <xsl:template match="wx:sub-section"> <xsl:call-template name="section"/> </xsl:template> <!-- sub-section & sect --> <xsl:template name="section"> <xsl:choose> <xsl:when test="w:p and wx:sub-section"> <xsl:choose> <xsl:when test="count(w:p)=1"> <section> <xsl:apply-templates select="w:p"/> <xsl:apply-templates select="wx:sub-section"/> </section> </xsl:when> <xsl:otherwise> <section> <xsl:apply-templates select="w:p"/> </section> <xsl:apply-templates select="wx:sub-section"/> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:when test="wx:sub-section"> <xsl:apply-templates/> </xsl:when> <xsl:otherwise> <section> <xsl:apply-templates/> </section> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- paragraph --> <xsl:template match="w:p"> <xsl:variable name="thisPStyleName" select="w:pPr/w:pStyle/@w:val"/> <xsl:variable name="thisPStyle" select="$pStyles[@w:styleId=$thisPStyleName]"/> <xsl:variable name="thisPStyleSize" select="$thisPStyle/w:rPr/w:sz-cs/@w:val"/> <!-- test for subtitle --> <xsl:variable name="sizeTest" select="$thisPStyleSize > $stdPStyleSize or $thisPStyleSize > 24"/> <xsl:variable name="centerTest" select="w:pPr/w:jc/@w:val='center'"/> <xsl:variable name="boldTest" select="w:r/w:rPr/w:b"/> <xsl:choose> <xsl:when test="w:r or w:hlink or st1:*"> <xsl:choose> <xsl:when test="$sizeTest or $centerTest"> <xsl:choose> <xsl:when test="not(preceding-sibling::w:p[position()=1]) and not(w:r/w:pict)"> <title> <p> <xsl:apply-templates/> </p> </title> </xsl:when> <xsl:otherwise> <subtitle> <xsl:apply-templates/> </subtitle> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise> <!-- ident tests --> <xsl:variable name="thisPStyleLInd" select="$thisPStyle/w:pPr/w:ind/@w:left > 0"/> <xsl:variable name="thisPStyleRInd" select="$thisPStyle/w:pPr/w:ind/@w:right > 0"/> <xsl:variable name="prePStyleVal" select="preceding-sibling::w:p[position()=1]/w:pPr/w:pStyle/@w:val"/> <xsl:variable name="prePStyleLInd" select="$pStyles[@w:styleId=$prePStyleVal]/w:pPr/w:ind/@w:left > 0"/> <xsl:variable name="prePStyleRInd" select="$pStyles[@w:styleId=$prePStyleVal]/w:pPr/w:ind/@w:right > 0"/> <!-- cite tests --> <xsl:variable name="thisPStyleInd" select="$thisPStyleLInd and $thisPStyleRInd"/> <xsl:variable name="prePStyleInd" select="$prePStyleLInd and $prePStyleRInd"/> <xsl:choose> <!-- cite? --> <xsl:when test="$thisPStyleInd"> <xsl:if test="not($prePStyleInd)"> <cite> <xsl:apply-templates select="." mode="cite"/> </cite> </xsl:if> </xsl:when> <!-- poem? --> <xsl:when test="$thisPStyleLInd"> <xsl:if test="not($prePStyleLInd) and w:r"> <poem> <xsl:apply-templates select="." mode="stanza"/> <xsl:apply-templates select="." mode="poem-author"/> </poem> </xsl:if> </xsl:when> <xsl:otherwise> <p> <xsl:apply-templates/> </p> </xsl:otherwise> </xsl:choose> </xsl:otherwise> </xsl:choose> </xsl:when> <xsl:otherwise> <xsl:if test="not($thisPStyle/w:pPr/w:ind/@w:left > 0)"> <p/> </xsl:if> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- cite --> <xsl:template match="w:p" mode="cite"> <xsl:variable name="thisPStyleName" select="w:pPr/w:pStyle/@w:val"/> <xsl:variable name="thisPStyleInd" select="$pStyles[@w:styleId=$thisPStyleName]/w:pPr/w:ind/@w:left > 0 and $pStyles[@w:styleId=$thisPStyleName]/w:pPr/w:ind/@w:right > 0"/> <xsl:if test="$thisPStyleInd"> <xsl:variable name="prePStyleVal" select="preceding-sibling::w:p[position()=1]/w:pPr/w:pStyle/@w:val"/> <xsl:variable name="prePStyleInd" select="$pStyles[@w:styleId=$prePStyleVal]/w:pPr/w:ind/@w:left > 0 and $pStyles[@w:styleId=$prePStyleVal]/w:pPr/w:ind/@w:right > 0"/> <xsl:variable name="nextPStyleVal" select="following-sibling::w:p[1]/w:pPr/w:pStyle/@w:val"/> <xsl:variable name="nextPStyleInd" select="$pStyles[@w:styleId=$nextPStyleVal]/w:pPr/w:ind/@w:left > 0 and $pStyles[@w:styleId=$nextPStyleVal]/w:pPr/w:ind/@w:right > 0"/> <xsl:variable name="thisPStyleRAlign" select="$pStyles[@w:styleId=$thisPStyleName]/w:pPr/w:jc/@w:val='right'"/> <xsl:choose> <xsl:when test="$prePStyleInd and $thisPStyleRAlign and not($nextPStyleInd)"> <text-author> <xsl:apply-templates/> </text-author> </xsl:when> <xsl:otherwise> <p> <xsl:apply-templates/> </p> </xsl:otherwise> </xsl:choose> <xsl:apply-templates select="following-sibling::w:p[1]" mode="cite"/> </xsl:if> </xsl:template> <!-- poem --> <xsl:template match="w:p" mode="stanza"> <xsl:variable name="thisPStyleName" select="w:pPr/w:pStyle/@w:val"/> <xsl:variable name="thisPStyleLInd" select="$pStyles[@w:styleId=$thisPStyleName]/w:pPr/w:ind/@w:left > 0"/> <xsl:variable name="prePStyleVal" select="preceding-sibling::w:p[position()=1]/w:pPr/w:pStyle/@w:val"/> <xsl:variable name="prePStyleLInd" select="$pStyles[@w:styleId=$prePStyleVal]/w:pPr/w:ind/@w:left > 0"/> <xsl:variable name="nextPStyleVal" select="following-sibling::w:p[1]/w:pPr/w:pStyle/@w:val"/> <xsl:variable name="nextPStyleLInd" select="$pStyles[@w:styleId=$nextPStyleVal]/w:pPr/w:ind/@w:left > 0"/> <xsl:variable name="netxtPStyleBold" select="$pStyles[@w:styleId=$nextPStyleVal]/w:rPr/w:b"/> <xsl:if test="not($prePStyleLInd) or not(w:r)"> <xsl:choose> <xsl:when test="not(w:r)"> <xsl:if test="following-sibling::w:p[1][w:r]"> <stanza> <xsl:apply-templates select="following-sibling::w:p[1]" mode="v"/> </stanza> </xsl:if> </xsl:when> <xsl:otherwise> <stanza> <xsl:apply-templates select="." mode="v"/> </stanza> </xsl:otherwise> </xsl:choose> </xsl:if> <xsl:if test="$nextPStyleLInd"> <xsl:apply-templates select="following-sibling::w:p[1]" mode="stanza"/> </xsl:if> </xsl:template> <!-- stanza --> <xsl:template match="w:p" mode="v"> <xsl:variable name="thisPStyleName" select="w:pPr/w:pStyle/@w:val"/> <xsl:variable name="thisPStyleLInd" select="$pStyles[@w:styleId=$thisPStyleName]/w:pPr/w:ind/@w:left > 0"/> <xsl:variable name="nextPStyleVal" select="following-sibling::w:p[1]/w:pPr/w:pStyle/@w:val"/> <xsl:variable name="nextPStyleLInd" select="$pStyles[@w:styleId=$nextPStyleVal]/w:pPr/w:ind/@w:left > 0"/> <xsl:variable name="thisPStyleBold" select="$pStyles[@w:styleId=$thisPStyleName]/w:rPr/w:b"/> <xsl:if test="$thisPStyleLInd and w:r"> <xsl:choose> <xsl:when test="not($nextPStyleLInd)"> <xsl:if test="not($thisPStyleBold)"> <v> <xsl:apply-templates/> </v> <xsl:apply-templates select="following-sibling::w:p[1]" mode="v"/> </xsl:if> </xsl:when> <xsl:otherwise> <v> <xsl:apply-templates/> </v> <xsl:apply-templates select="following-sibling::w:p[1]" mode="v"/> </xsl:otherwise> </xsl:choose> </xsl:if> </xsl:template> <!-- poem-author --> <xsl:template match="w:p" mode="poem-author"> <xsl:variable name="thisPStyleName" select="w:pPr/w:pStyle/@w:val"/> <xsl:variable name="thisPStyleLInd" select="$pStyles[@w:styleId=$thisPStyleName]/w:pPr/w:ind/@w:left > 0"/> <xsl:variable name="nextPStyleVal" select="following-sibling::w:p[1]/w:pPr/w:pStyle/@w:val"/> <xsl:variable name="nextPStyleLInd" select="$pStyles[@w:styleId=$nextPStyleVal]/w:pPr/w:ind/@w:left > 0"/> <xsl:variable name="thisPStyleBold" select="$pStyles[@w:styleId=$thisPStyleName]/w:rPr/w:b"/> <xsl:if test="$thisPStyleLInd"> <xsl:choose> <xsl:when test="$thisPStyleBold and not($nextPStyleLInd)"> <text-author> <xsl:apply-templates/> </text-author> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="following-sibling::w:p[1]" mode="poem-author"/> </xsl:otherwise> </xsl:choose> </xsl:if> </xsl:template> <!-- smart tags --> <xsl:template match="st1:*"> <xsl:apply-templates/> </xsl:template> <!-- link [only external links] - - - use for all links <xsl:template match="w:hlink">--> <xsl:template match="w:hlink[not(@w:dest='')]"> <xsl:variable name="dest" select="@w:dest"/> <a l:href="{$dest}"> <xsl:apply-templates/> </a> </xsl:template> <!-- run --> <xsl:template match="w:r"> <xsl:choose> <xsl:when test="w:pict"> <xsl:apply-templates select="w:pict"/> </xsl:when> <xsl:when test="w:rPr/w:b and w:rPr/w:i"> <strong> <emphasis> <xsl:apply-templates select="w:t"/> </emphasis> </strong> </xsl:when> <xsl:when test="w:rPr/w:i"> <emphasis> <xsl:apply-templates select="w:t"/> </emphasis> </xsl:when> <xsl:when test="w:rPr/w:b"> <strong> <xsl:apply-templates select="w:t"/> </strong> </xsl:when> <xsl:otherwise> <xsl:apply-templates/> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- footnote --> <xsl:template match="w:footnote"> <xsl:variable name="meInContext" select="ancestor::w:r[1]/*[count(.|descendant-or-self::*)=count(descendant-or-self::*)]"/> <xsl:variable name="position" select="count($meInContext/preceding::*[name()='w:footnote' and ancestor::w:body]) + 1"/> <a l:href="#fn{$position}" type="note"> <xsl:text>[</xsl:text> <xsl:value-of select="$position"/> <xsl:text>]</xsl:text> </a> </xsl:template> <xsl:template name="footnote"> <xsl:for-each select="//w:p/w:r/w:footnote"> <xsl:variable name="meInContext" select="ancestor::w:r[1]/*[count(.|descendant-or-self::*)=count(descendant-or-self::*)]"/> <xsl:variable name="position" select="count($meInContext/preceding::*[name()='w:footnote' and ancestor::w:body]) + 1"/> <section id="fn{$position}"> <title> <p> <xsl:value-of select="$position"/> </p> </title> <xsl:apply-templates select="w:p"/> </section> </xsl:for-each> </xsl:template> <!-- endnote --> <xsl:template match="w:endnote"> <xsl:variable name="meInContext" select="ancestor::w:r[1]/*[count(.|descendant-or-self::*)=count(descendant-or-self::*)]"/> <xsl:variable name="position" select="count($meInContext/preceding::*[name()='w:endnote' and ancestor::w:body]) + 1"/> <a l:href="#en{$position}" type="note"> <xsl:text>{</xsl:text> <xsl:value-of select="$position"/> <xsl:text>}</xsl:text> </a> </xsl:template> <xsl:template name="endnote"> <xsl:for-each select="//w:p/w:r/w:endnote"> <xsl:variable name="meInContext" select="ancestor::w:r[1]/*[count(.|descendant-or-self::*)=count(descendant-or-self::*)]"/> <xsl:variable name="position" select="count($meInContext/preceding::*[name()='w:endnote' and ancestor::w:body]) + 1"/> <section id="en{$position}"> <title> <p> <xsl:value-of select="$position"/> </p> </title> <xsl:apply-templates select="w:p"/> </section> </xsl:for-each> </xsl:template> <!-- picture --> <xsl:template match="w:pict"> <xsl:variable name="pictExt" select="substring(w:binData/@w:name, string-length(w:binData/@w:name)-2, 3)"/> <xsl:if test="$pictExt='jpg' or $pictExt='png'"> <image l:href="#doc2fb_image_{substring-after(w:binData/@w:name,'wordml://')}"/> </xsl:if> </xsl:template> <!-- text --> <xsl:template match="w:t"> <xsl:value-of select="."/> </xsl:template></xsl:stylesheet>