fb2.xsl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:fb="__FB_NS__">
  2. <!--
  3. #########################################################################
  4. # #
  5. # #
  6. # copyright 2002 Paul Henry Tremblay #
  7. # Copyright 2011 Kovid Goyal
  8. # #
  9. # This program is distributed in the hope that it will be useful, #
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of #
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
  12. # General Public License for more details. #
  13. # #
  14. # You should have received a copy of the GNU General Public License #
  15. # along with this program; if not, write to the Free Software #
  16. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA #
  17. # 02111-1307 USA #
  18. # #
  19. # #
  20. #########################################################################
  21. -->
  22. <xsl:output method="xml" encoding="UTF-8"/>
  23. <xsl:key name="note-link" match="fb:section" use="@id"/>
  24. <xsl:template match="/*">
  25. <html>
  26. <head>
  27. <xsl:if test="fb:description/fb:title-info/fb:lang = 'ru'">
  28. <meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8"/>
  29. </xsl:if>
  30. <title>
  31. <xsl:value-of select="fb:description/fb:title-info/fb:book-title"/>
  32. </title>
  33. <style type="text/css">
  34. body { text-align : justify }
  35. h1{ font-size : 160%; font-style : normal; font-weight : bold; text-align : left; border : 1px solid Black; background-color : #E7E7E7; margin-left : 0px; page-break-before : always; }
  36. h2{ font-size : 130%; font-style : normal; font-weight : bold; text-align : left; background-color : #EEEEEE; border : 1px solid Gray; page-break-before : always; }
  37. h3{ font-size : 110%; font-style : normal; font-weight : bold; text-align : left; background-color : #F1F1F1; border : 1px solid Silver;}
  38. h4{ font-size : 100%; font-style : normal; font-weight : bold; text-align : left; border : 1px solid Gray; background-color : #F4F4F4;}
  39. h5{ font-size : 100%; font-style : italic; font-weight : bold; text-align : left; border : 1px solid Gray; background-color : #F4F4F4;}
  40. h6{ font-size : 100%; font-style : italic; font-weight : normal; text-align : left; border : 1px solid Gray; background-color : #F4F4F4;}
  41. small { font-size : 80% }
  42. blockquote { margin-left :4em; margin-top:1em; margin-right:0.2em;}
  43. hr { color : Black }
  44. ul {margin-left: 0}
  45. .epigraph{width:75%; margin-left : 25%; font-style: italic;}
  46. div.paragraph, p.paragraph { text-indent: 2em; margin-top: 0; margin-bottom: 0; }
  47. .subtitle { text-align: center; }
  48. </style>
  49. <link rel="stylesheet" type="text/css" href="inline-styles.css" />
  50. </head>
  51. <body>
  52. <xsl:for-each select="fb:description/fb:title-info/fb:annotation">
  53. <div>
  54. <xsl:call-template name="annotation"/>
  55. </div>
  56. <hr/>
  57. </xsl:for-each>
  58. <!-- BUILD TOC -->
  59. <ul>
  60. <xsl:apply-templates select="fb:body" mode="toc"/>
  61. </ul>
  62. <hr/>
  63. <!-- END BUILD TOC -->
  64. <!-- BUILD BOOK -->
  65. <xsl:for-each select="fb:body">
  66. <xsl:if test="position()!=1">
  67. <hr/>
  68. </xsl:if>
  69. <xsl:if test="@name">
  70. <h4 align="center">
  71. <xsl:value-of select="@name"/>
  72. </h4>
  73. </xsl:if>
  74. <!-- <xsl:apply-templates /> -->
  75. <xsl:apply-templates/>
  76. </xsl:for-each>
  77. </body>
  78. </html>
  79. </xsl:template>
  80. <!-- author template -->
  81. <xsl:template name="author">
  82. <xsl:value-of select="fb:first-name"/>
  83. <xsl:text disable-output-escaping="no">&#032;</xsl:text>
  84. <xsl:value-of select="fb:middle-name"/>&#032;
  85. <xsl:text disable-output-escaping="no">&#032;</xsl:text>
  86. <xsl:value-of select="fb:last-name"/>
  87. <br/>
  88. </xsl:template>
  89. <!-- sequence template -->
  90. <xsl:template name="sequence">
  91. <li/>
  92. <xsl:value-of select="@name"/>
  93. <xsl:if test="@number">
  94. <xsl:text disable-output-escaping="no">,&#032;#</xsl:text>
  95. <xsl:value-of select="@number"/>
  96. </xsl:if>
  97. <xsl:if test="fb:sequence">
  98. <ul>
  99. <xsl:for-each select="fb:sequence">
  100. <xsl:call-template name="sequence"/>
  101. </xsl:for-each>
  102. </ul>
  103. </xsl:if>
  104. <!-- <br/> -->
  105. </xsl:template>
  106. <!-- toc template -->
  107. <xsl:template match="fb:section|fb:body" mode="toc">
  108. <xsl:choose>
  109. <xsl:when test="name()='body' and position()=1 and not(fb:title)">
  110. <xsl:apply-templates select="fb:section" mode="toc"/>
  111. </xsl:when>
  112. <xsl:otherwise>
  113. <li>
  114. <a href="#TOC_{generate-id()}"><xsl:value-of select="normalize-space(fb:title/fb:p[1] | @name)"/></a>
  115. <xsl:if test="fb:section">
  116. <ul><xsl:apply-templates select="fb:section" mode="toc"/></ul>
  117. </xsl:if>
  118. </li>
  119. </xsl:otherwise>
  120. </xsl:choose>
  121. </xsl:template>
  122. <!-- description -->
  123. <xsl:template match="fb:description">
  124. <xsl:apply-templates/>
  125. </xsl:template>
  126. <!-- body -->
  127. <xsl:template match="fb:body">
  128. <div><xsl:apply-templates/></div>
  129. </xsl:template>
  130. <xsl:template match="fb:section">
  131. <xsl:variable name="section_has_title">
  132. <xsl:choose>
  133. <xsl:when test="./fb:title"><xsl:value-of select="generate-id()" /></xsl:when>
  134. <xsl:otherwise>None</xsl:otherwise>
  135. </xsl:choose>
  136. </xsl:variable>
  137. <xsl:if test="$section_has_title = 'None'">
  138. <div id="TOC_{generate-id()}">
  139. <xsl:if test="@id">
  140. <xsl:element name="a">
  141. <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
  142. </xsl:element>
  143. </xsl:if>
  144. </div>
  145. </xsl:if>
  146. <xsl:apply-templates>
  147. <xsl:with-param name="section_toc_id" select="$section_has_title" />
  148. </xsl:apply-templates>
  149. </xsl:template>
  150. <!-- section/title -->
  151. <xsl:template match="fb:section/fb:title|fb:poem/fb:title">
  152. <xsl:param name="section_toc_id" />
  153. <xsl:choose>
  154. <xsl:when test="count(ancestor::node()) &lt; 9">
  155. <xsl:element name="{concat('h',count(ancestor::node())-3)}">
  156. <xsl:if test="../@id">
  157. <xsl:attribute name="id"><xsl:value-of select="../@id" /></xsl:attribute>
  158. </xsl:if>
  159. <xsl:if test="$section_toc_id != 'None'">
  160. <xsl:element name="a">
  161. <xsl:attribute name="id">TOC_<xsl:value-of select="$section_toc_id"/></xsl:attribute>
  162. </xsl:element>
  163. </xsl:if>
  164. <a name="TOC_{generate-id()}"></a>
  165. <xsl:if test="@id">
  166. <xsl:element name="a">
  167. <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
  168. </xsl:element>
  169. </xsl:if>
  170. <xsl:apply-templates/>
  171. </xsl:element>
  172. </xsl:when>
  173. <xsl:otherwise>
  174. <xsl:element name="h6">
  175. <xsl:if test="@id">
  176. <xsl:element name="a">
  177. <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
  178. </xsl:element>
  179. </xsl:if>
  180. <xsl:apply-templates/>
  181. </xsl:element>
  182. </xsl:otherwise>
  183. </xsl:choose>
  184. </xsl:template>
  185. <!-- section/title -->
  186. <xsl:template match="fb:body/fb:title">
  187. <xsl:element name="h1">
  188. <xsl:apply-templates />
  189. </xsl:element>
  190. </xsl:template>
  191. <xsl:template match="fb:title/fb:p">
  192. <xsl:apply-templates/><xsl:text disable-output-escaping="no">&#032;</xsl:text><br/>
  193. </xsl:template>
  194. <!-- subtitle -->
  195. <xsl:template match="fb:subtitle">
  196. <xsl:if test="@id">
  197. <xsl:element name="a">
  198. <xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute>
  199. </xsl:element>
  200. </xsl:if>
  201. <h5 class="subtitle">
  202. <xsl:apply-templates/>
  203. </h5>
  204. </xsl:template>
  205. <!-- p -->
  206. <xsl:template match="fb:p">
  207. <xsl:element name="div">
  208. <xsl:attribute name="class">paragraph</xsl:attribute>
  209. <xsl:if test="@style">
  210. <xsl:attribute name="style"><xsl:value-of select="@style"/></xsl:attribute>
  211. </xsl:if>
  212. <xsl:if test="@id">
  213. <xsl:element name="a">
  214. <xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute>
  215. </xsl:element>
  216. </xsl:if>
  217. <xsl:apply-templates/>
  218. </xsl:element>
  219. </xsl:template>
  220. <!-- strong -->
  221. <xsl:template match="fb:strong">
  222. <strong><xsl:apply-templates/></strong>
  223. </xsl:template>
  224. <!-- emphasis -->
  225. <xsl:template match="fb:emphasis">
  226. <em> <xsl:apply-templates/></em>
  227. </xsl:template>
  228. <!-- style -->
  229. <xsl:template match="fb:style">
  230. <span class="{@name}"><xsl:apply-templates/></span>
  231. </xsl:template>
  232. <!-- empty-line -->
  233. <xsl:template match="fb:empty-line">
  234. <br/>
  235. </xsl:template>
  236. <!-- super/sub-scripts -->
  237. <xsl:template match="fb:sup">
  238. <sup><xsl:apply-templates/></sup>
  239. </xsl:template>
  240. <xsl:template match="fb:sub">
  241. <sub><xsl:apply-templates/></sub>
  242. </xsl:template>
  243. <!-- link -->
  244. <xsl:template match="fb:a">
  245. <xsl:element name="a">
  246. <xsl:attribute name="href"><xsl:value-of select="@xlink:href"/></xsl:attribute>
  247. <xsl:attribute name="title">
  248. <xsl:choose>
  249. <xsl:when test="starts-with(@xlink:href,'#')"><xsl:value-of select="key('note-link',substring-after(@xlink:href,'#'))/fb:p"/></xsl:when>
  250. <xsl:otherwise><xsl:value-of select="key('note-link',@xlink:href)/fb:p"/></xsl:otherwise>
  251. </xsl:choose>
  252. </xsl:attribute>
  253. <xsl:choose>
  254. <xsl:when test="(@type) = 'note'">
  255. <xsl:attribute name="link_note"></xsl:attribute>
  256. <sup>
  257. <xsl:apply-templates/>
  258. </sup>
  259. </xsl:when>
  260. <xsl:when test="(@type) = 'cite' and @id">
  261. <xsl:attribute name="link_cite"><xsl:value-of select="@id"/></xsl:attribute>
  262. <xsl:apply-templates/>
  263. </xsl:when>
  264. <xsl:otherwise>
  265. <xsl:apply-templates/>
  266. </xsl:otherwise>
  267. </xsl:choose>
  268. </xsl:element>
  269. </xsl:template>
  270. <!-- annotation -->
  271. <xsl:template name="annotation">
  272. <xsl:if test="@id">
  273. <xsl:element name="a">
  274. <xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute>
  275. </xsl:element>
  276. </xsl:if>
  277. <h3>Annotation</h3>
  278. <xsl:apply-templates/>
  279. </xsl:template>
  280. <!-- tables -->
  281. <xsl:template match="fb:table">
  282. <table>
  283. <xsl:apply-templates/>
  284. </table>
  285. </xsl:template>
  286. <xsl:template match="fb:tr">
  287. <xsl:element name="tr">
  288. <xsl:if test="@align">
  289. <xsl:attribute name="align"><xsl:value-of select="@align"/></xsl:attribute>
  290. </xsl:if>
  291. <xsl:apply-templates/>
  292. </xsl:element>
  293. </xsl:template>
  294. <xsl:template match="fb:td|fb:th">
  295. <xsl:element name="{local-name()}">
  296. <xsl:if test="@align">
  297. <xsl:attribute name="align"><xsl:value-of select="@align"/></xsl:attribute>
  298. </xsl:if>
  299. <xsl:if test="@style">
  300. <xsl:attribute name="style"><xsl:value-of select="@style"/></xsl:attribute>
  301. </xsl:if>
  302. <xsl:if test="@colspan">
  303. <xsl:attribute name="colspan"><xsl:value-of select="@colspan"/></xsl:attribute>
  304. </xsl:if>
  305. <xsl:if test="@rowspan">
  306. <xsl:attribute name="rowspan"><xsl:value-of select="@rowspan"/></xsl:attribute>
  307. </xsl:if>
  308. <xsl:apply-templates/>
  309. </xsl:element>
  310. </xsl:template>
  311. <!-- epigraph -->
  312. <xsl:template match="fb:epigraph">
  313. <blockquote class="epigraph">
  314. <xsl:if test="@id">
  315. <xsl:element name="a">
  316. <xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute>
  317. </xsl:element>
  318. </xsl:if>
  319. <div>
  320. <xsl:apply-templates/>
  321. </div>
  322. </blockquote>
  323. </xsl:template>
  324. <!-- epigraph/text-author -->
  325. <xsl:template match="fb:epigraph/fb:text-author">
  326. <blockquote>
  327. <i><xsl:apply-templates/></i>
  328. </blockquote>
  329. </xsl:template>
  330. <!-- cite -->
  331. <xsl:template match="fb:cite">
  332. <blockquote>
  333. <xsl:if test="@id">
  334. <xsl:element name="a">
  335. <xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute>
  336. </xsl:element>
  337. </xsl:if>
  338. <div>
  339. <xsl:apply-templates/>
  340. </div>
  341. </blockquote>
  342. </xsl:template>
  343. <!-- cite/text-author -->
  344. <xsl:template match="fb:text-author">
  345. <blockquote>
  346. <i> <xsl:apply-templates/></i></blockquote>
  347. </xsl:template>
  348. <!-- date -->
  349. <xsl:template match="fb:date">
  350. <xsl:choose>
  351. <xsl:when test="not(@value)">
  352. &#160;&#160;&#160;<xsl:apply-templates/>
  353. <br/>
  354. </xsl:when>
  355. <xsl:otherwise>
  356. &#160;&#160;&#160;<xsl:value-of select="@value"/>
  357. <br/>
  358. </xsl:otherwise>
  359. </xsl:choose>
  360. </xsl:template>
  361. <!-- poem -->
  362. <xsl:template match="fb:poem">
  363. <blockquote>
  364. <xsl:if test="@id">
  365. <xsl:element name="a">
  366. <xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute>
  367. </xsl:element>
  368. </xsl:if>
  369. <div>
  370. <xsl:apply-templates/>
  371. </div>
  372. </blockquote>
  373. </xsl:template>
  374. <!-- stanza -->
  375. <xsl:template match="fb:stanza">
  376. <xsl:apply-templates/>
  377. <br/>
  378. </xsl:template>
  379. <!-- v -->
  380. <xsl:template match="fb:v">
  381. <xsl:if test="@id">
  382. <xsl:element name="a">
  383. <xsl:attribute name="name"><xsl:value-of select="@id"/></xsl:attribute>
  384. </xsl:element>
  385. </xsl:if>
  386. <xsl:apply-templates/><br/>
  387. </xsl:template>
  388. <!-- image -->
  389. <xsl:template match="fb:body/fb:image|fb:section/fb:image">
  390. <div align="center">
  391. <xsl:element name="img">
  392. <xsl:choose>
  393. <xsl:when test="starts-with(@xlink:href,'#')">
  394. <xsl:attribute name="src"><xsl:value-of select="substring-after(@xlink:href,'#')"/></xsl:attribute>
  395. </xsl:when>
  396. <xsl:otherwise>
  397. <xsl:attribute name="src"><xsl:value-of select="@xlink:href"/></xsl:attribute>
  398. </xsl:otherwise>
  399. </xsl:choose>
  400. <xsl:if test="@title">
  401. <xsl:attribute name="title"><xsl:value-of select="@title"/></xsl:attribute>
  402. </xsl:if>
  403. </xsl:element>
  404. </div>
  405. </xsl:template>
  406. <xsl:template match="fb:image">
  407. <xsl:element name="img">
  408. <xsl:choose>
  409. <xsl:when test="starts-with(@xlink:href,'#')">
  410. <xsl:attribute name="src"><xsl:value-of select="substring-after(@xlink:href,'#')"/></xsl:attribute>
  411. </xsl:when>
  412. <xsl:otherwise>
  413. <xsl:attribute name="src"><xsl:value-of select="@xlink:href"/></xsl:attribute>
  414. </xsl:otherwise>
  415. </xsl:choose>
  416. <xsl:if test="@title">
  417. <xsl:attribute name="title"><xsl:value-of select="@title"/></xsl:attribute>
  418. </xsl:if>
  419. </xsl:element>
  420. </xsl:template>
  421. <!-- code -->
  422. <xsl:template match="fb:code">
  423. <code><xsl:apply-templates/></code>
  424. </xsl:template>
  425. <!-- Strikethrough text -->
  426. <xsl:template match="fb:strikethrough">
  427. <del><xsl:apply-templates/></del>
  428. </xsl:template>
  429. </xsl:stylesheet>