rtf.xsl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet version="1.0"
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4. xmlns="http://www.w3.org/1999/xhtml"
  5. xmlns:rtf="http://rtf2xml.sourceforge.net/"
  6. xmlns:c="calibre"
  7. extension-element-prefixes="c"
  8. exclude-result-prefixes="rtf"
  9. >
  10. <xsl:template match = "rtf:para">
  11. <xsl:choose>
  12. <xsl:when test = "parent::rtf:paragraph-definition[@name='heading 1']|
  13. parent::rtf:paragraph-definition[@name='heading 2']|
  14. parent::rtf:paragraph-definition[@name='heading 3']|
  15. parent::rtf:paragraph-definition[@name='heading 4']|
  16. parent::rtf:paragraph-definition[@name='heading 5']|
  17. parent::rtf:paragraph-definition[@name='heading 6']|
  18. parent::rtf:paragraph-definition[@name='heading 7']|
  19. parent::rtf:paragraph-definition[@name='heading 8']|
  20. parent::rtf:paragraph-definition[@name='heading 9']
  21. ">
  22. <xsl:variable name="head-number" select="substring(parent::rtf:paragraph-definition/@name, 9)"/>
  23. <xsl:element name="h{$head-number}">
  24. <xsl:apply-templates/>
  25. </xsl:element>
  26. </xsl:when>
  27. <xsl:otherwise>
  28. <xsl:call-template name = "para"/>
  29. </xsl:otherwise>
  30. </xsl:choose>
  31. </xsl:template>
  32. <xsl:template match="rtf:style-group">
  33. <xsl:apply-templates/>
  34. </xsl:template>
  35. <xsl:template match="rtf:paragraph-definition">
  36. <xsl:choose>
  37. <xsl:when test = "parent::rtf:paragraph-definition[@name='heading 1']|
  38. parent::rtf:paragraph-definition[@name='heading 2']|
  39. parent::rtf:paragraph-definition[@name='heading 3']|
  40. parent::rtf:paragraph-definition[@name='heading 4']|
  41. parent::rtf:paragraph-definition[@name='heading 5']|
  42. parent::rtf:paragraph-definition[@name='heading 6']|
  43. parent::rtf:paragraph-definition[@name='heading 7']|
  44. parent::rtf:paragraph-definition[@name='heading 8']|
  45. parent::rtf:paragraph-definition[@name='heading 9']
  46. ">
  47. <xsl:apply-templates/>
  48. </xsl:when>
  49. <xsl:otherwise>
  50. <xsl:element name="div">
  51. <xsl:attribute name="class">
  52. <xsl:value-of select="@style-number"/>
  53. </xsl:attribute>
  54. <xsl:apply-templates/>
  55. </xsl:element>
  56. </xsl:otherwise>
  57. </xsl:choose>
  58. </xsl:template>
  59. <xsl:template name = "para">
  60. <xsl:element name = "p">
  61. <xsl:choose>
  62. <xsl:when test = "normalize-space(.) or child::*">
  63. <xsl:call-template name = "para-content"/>
  64. </xsl:when>
  65. <xsl:otherwise>
  66. <xsl:text>&#160;</xsl:text>
  67. </xsl:otherwise>
  68. </xsl:choose>
  69. </xsl:element>
  70. </xsl:template>
  71. <xsl:template name = "para_off">
  72. <xsl:if test = "normalize-space(.) or child::*">
  73. <xsl:element name = "p">
  74. <xsl:attribute name = "class">
  75. <xsl:value-of select = "../@style-number"/>
  76. </xsl:attribute>
  77. <xsl:call-template name = "para-content"/>
  78. </xsl:element>
  79. </xsl:if>
  80. </xsl:template>
  81. <xsl:template name = "para-content">
  82. <xsl:apply-templates/>
  83. </xsl:template>
  84. <xsl:template name = "para-content_off">
  85. <xsl:choose>
  86. <xsl:when test = "@italics = 'true' ">
  87. <emph rend = "paragraph-emph-italics">
  88. <xsl:apply-templates/>
  89. </emph>
  90. </xsl:when>
  91. <xsl:when test = "@bold = 'true' ">
  92. <emph rend = "paragraph-emph-bold">
  93. <xsl:apply-templates/>
  94. </emph>
  95. </xsl:when>
  96. <xsl:when test = "@underlined and @underlined != 'false'">
  97. <emph rend = "paragraph-emph-underlined">
  98. <xsl:apply-templates/>
  99. </emph>
  100. </xsl:when>
  101. <xsl:when test = "(@strike-through = 'true')
  102. or (@double-strike-through = 'true')
  103. or (@emboss = 'true')
  104. or (@engrave = 'true')
  105. or (@small-caps = 'true')
  106. or (@shadow = 'true')
  107. or (@hidden = 'true')
  108. or (@outline = 'true')
  109. ">
  110. <emph rend = "paragraph-emph">
  111. <xsl:apply-templates/>
  112. </emph>
  113. </xsl:when>
  114. <xsl:otherwise>
  115. <xsl:apply-templates/>
  116. </xsl:otherwise>
  117. </xsl:choose>
  118. </xsl:template>
  119. <xsl:template name="make-header">
  120. <head>
  121. <xsl:element name="meta">
  122. <xsl:attribute name="name">
  123. <xsl:text>generator</xsl:text>
  124. </xsl:attribute>
  125. <xsl:attribute name="content">
  126. <xsl:text>http://calibre-ebook.com</xsl:text>
  127. </xsl:attribute>
  128. </xsl:element>
  129. <xsl:choose>
  130. <xsl:when test="/rtf:doc/rtf:preamble/rtf:doc-information">
  131. <xsl:apply-templates select="/rtf:doc/rtf:preamble/rtf:doc-information" mode="header"/>
  132. </xsl:when>
  133. <xsl:otherwise>
  134. <xsl:call-template name="html-head"/>
  135. </xsl:otherwise>
  136. </xsl:choose>
  137. </head>
  138. </xsl:template>
  139. <xsl:template match="rtf:doc-information"/>
  140. <xsl:template match="rtf:doc-information" mode="header">
  141. <link rel="stylesheet" type="text/css" href="styles.css"/>
  142. <xsl:if test="not(rtf:title)">
  143. <title>unnamed</title>
  144. </xsl:if>
  145. <xsl:apply-templates/>
  146. </xsl:template>
  147. <xsl:template match="rtf:creation-time|rtf:doc-notes|rtf:author|rtf:revision-time">
  148. <xsl:element name="meta">
  149. <xsl:attribute name="name">
  150. <xsl:value-of select="name(.)"/>
  151. </xsl:attribute>
  152. <xsl:attribute name="content">
  153. <xsl:apply-templates/>
  154. </xsl:attribute>
  155. </xsl:element>
  156. </xsl:template>
  157. <xsl:template match="rtf:creation-time|rtf:revision-time">
  158. <xsl:element name="meta">
  159. <xsl:attribute name="name">
  160. <xsl:value-of select="name(.)"/>
  161. </xsl:attribute>
  162. <xsl:attribute name="content">
  163. <xsl:value-of select="@year"/>
  164. <xsl:text>-</xsl:text>
  165. <xsl:value-of select="@month"/>
  166. <xsl:text>-</xsl:text>
  167. <xsl:value-of select="@day"/>
  168. </xsl:attribute>
  169. </xsl:element>
  170. </xsl:template>
  171. <xsl:template match="rtf:operator|rtf:editing-time|rtf:number-of-pages|rtf:number-of-words|rtf:number-of-characters"/>
  172. <xsl:template match="rtf:title">
  173. <xsl:element name="title">
  174. <xsl:apply-templates/>
  175. </xsl:element>
  176. </xsl:template>
  177. <xsl:template name="html-head">
  178. <title>unnamed</title>
  179. <link rel="stylesheet" type="text/css" href="styles.css"/>
  180. </xsl:template>
  181. <xsl:template name="make-css-stylesheet">
  182. <xsl:document href="styles.css" method="text">
  183. <xsl:for-each select="//rtf:paragraph-definition">
  184. <xsl:if test = "generate-id(.) = generate-id(key('style-types', @style-number))">
  185. <xsl:text>div.</xsl:text>
  186. <xsl:value-of select="@style-number"/>
  187. <xsl:text>{</xsl:text>
  188. <xsl:call-template name="parse-styles-attrs"/>
  189. <xsl:text>}&#xA;</xsl:text>
  190. </xsl:if>
  191. </xsl:for-each>
  192. <xsl:text>span.italic{font-style:italic}&#xA;</xsl:text>
  193. <xsl:text>span.no-italic{font-style:normal}&#xA;</xsl:text>
  194. <xsl:text>span.bold{font-weight:bold}&#xA;</xsl:text>
  195. <xsl:text>span.no-bold{font-weight:normal}&#xA;</xsl:text>
  196. <xsl:text>span.underline{text-decoration:underline}&#xA;</xsl:text>
  197. <xsl:text>span.no-underline{text-decoration:none}&#xA;</xsl:text>
  198. <xsl:text>span.italic-bold{font-style:italic;font-weight:bold}&#xA;</xsl:text>
  199. <xsl:text>span.italic-underline{font-style:italic;text-decoration:underline}&#xA;</xsl:text>
  200. <xsl:text>span.bold-underline{font-weight:bold;text-decoration:underline}&#xA;</xsl:text>
  201. <xsl:text>&#xA;</xsl:text>
  202. </xsl:document>
  203. </xsl:template>
  204. <xsl:template name="parse-styles-attrs">
  205. <!--<xsl:text>position:relative;</xsl:text>
  206. <xsl:if test="@space-before">
  207. <xsl:text>padding-top:</xsl:text>
  208. <xsl:value-of select="@space-before"/>
  209. <xsl:text>pt;</xsl:text>
  210. </xsl:if>
  211. <xsl:if test="@space-after">
  212. <xsl:text>padding-bottom:</xsl:text>
  213. <xsl:value-of select="@space-after"/>
  214. <xsl:text>pt;</xsl:text>
  215. </xsl:if>-->
  216. <xsl:if test="@left-indent">
  217. <xsl:text>padding-left:</xsl:text>
  218. <xsl:value-of select="@left-indent"/>
  219. <xsl:text>pt;</xsl:text>
  220. </xsl:if>
  221. <xsl:if test="@right-indent">
  222. <xsl:text>padding-right:</xsl:text>
  223. <xsl:value-of select="@right-indent"/>
  224. <xsl:text>pt;</xsl:text>
  225. </xsl:if>
  226. <xsl:if test="@first-line-indent">
  227. <xsl:text>text-indent:</xsl:text>
  228. <xsl:value-of select="@first-line-indent"/>
  229. <xsl:text>pt;</xsl:text>
  230. </xsl:if>
  231. <xsl:if test="@bold='true'">
  232. <xsl:text>font-weight:</xsl:text>
  233. <xsl:value-of select="'bold'"/>
  234. <xsl:text>;</xsl:text>
  235. </xsl:if>
  236. <xsl:if test="@italics='true'">
  237. <xsl:text>font-style:</xsl:text>
  238. <xsl:value-of select="'italic'"/>
  239. <xsl:text>;</xsl:text>
  240. </xsl:if>
  241. <xsl:if test="@underlined and @underlined != 'false'">
  242. <xsl:text>text-decoration:underline</xsl:text>
  243. <xsl:text>;</xsl:text>
  244. </xsl:if>
  245. <!--<xsl:if test="@line-spacing">
  246. <xsl:text>line-height:</xsl:text>
  247. <xsl:value-of select="@line-spacing"/>
  248. <xsl:text>pt;</xsl:text>
  249. </xsl:if>-->
  250. <xsl:if test="(@align = 'just')">
  251. <xsl:text>text-align: justify;</xsl:text>
  252. </xsl:if>
  253. <xsl:if test="(@align = 'cent')">
  254. <xsl:text>text-align: center;</xsl:text>
  255. </xsl:if>
  256. <xsl:if test="(@align = 'left')">
  257. <xsl:text>text-align: left;</xsl:text>
  258. </xsl:if>
  259. <xsl:if test="(@align = 'right')">
  260. <xsl:text>text-align: right;</xsl:text>
  261. </xsl:if>
  262. </xsl:template>
  263. <xsl:template match="rtf:inline">
  264. <xsl:variable name="num-attrs" select="count(@*)"/>
  265. <xsl:choose>
  266. <xsl:when test="@footnote-marker">
  267. <xsl:text>[</xsl:text>
  268. <xsl:value-of select="count(preceding::rtf:footnote) + 1"/>
  269. <xsl:text>]</xsl:text>
  270. </xsl:when>
  271. <xsl:when test="(@superscript)">
  272. <xsl:element name="sup">
  273. <xsl:element name="span">
  274. <xsl:attribute name="class">
  275. <c:inline-class/>
  276. </xsl:attribute>
  277. <xsl:apply-templates/>
  278. </xsl:element>
  279. </xsl:element>
  280. </xsl:when>
  281. <xsl:when test="(@underscript or @subscript)">
  282. <xsl:element name="sub">
  283. <xsl:element name="span">
  284. <xsl:attribute name="class">
  285. <c:inline-class/>
  286. </xsl:attribute>
  287. <xsl:apply-templates/>
  288. </xsl:element>
  289. </xsl:element>
  290. </xsl:when>
  291. <xsl:otherwise>
  292. <xsl:element name="span">
  293. <xsl:attribute name="class">
  294. <c:inline-class/>
  295. </xsl:attribute>
  296. <xsl:apply-templates/>
  297. </xsl:element>
  298. </xsl:otherwise>
  299. </xsl:choose>
  300. </xsl:template>
  301. <xsl:template match="rtf:footnote"/>
  302. <xsl:template match="rtf:footnote" mode="bottom">
  303. <xsl:element name="div">
  304. <xsl:attribute name="class">
  305. <xsl:text>footnote</xsl:text>
  306. </xsl:attribute>
  307. <xsl:apply-templates/>
  308. </xsl:element>
  309. </xsl:template>
  310. <xsl:template match="rtf:list[@list-type='unordered']">
  311. <xsl:element name="ul">
  312. <xsl:apply-templates/>
  313. </xsl:element>
  314. </xsl:template>
  315. <xsl:template match="rtf:list[@list-type='ordered']">
  316. <xsl:element name="ol">
  317. <xsl:apply-templates/>
  318. </xsl:element>
  319. </xsl:template>
  320. <xsl:template match="rtf:item">
  321. <xsl:element name="li">
  322. <xsl:apply-templates/>
  323. </xsl:element>
  324. </xsl:template>
  325. <xsl:template match="rtf:item/rtf:style-group/rtf:paragraph-definition/rtf:para" priority="2">
  326. <xsl:apply-templates/>
  327. </xsl:template>
  328. <xsl:template match="rtf:table">
  329. <xsl:element name="table">
  330. <xsl:attribute name="id">
  331. <xsl:value-of select="generate-id(.)"/>
  332. </xsl:attribute>
  333. <xsl:apply-templates/>
  334. </xsl:element>
  335. </xsl:template>
  336. <xsl:template match="rtf:row">
  337. <xsl:element name="tr">
  338. <xsl:apply-templates/>
  339. </xsl:element>
  340. </xsl:template>
  341. <xsl:template match="rtf:cell">
  342. <xsl:element name="td">
  343. <xsl:if test="@class">
  344. <xsl:attribute name="class"><xsl:value-of select="@class"/></xsl:attribute>
  345. </xsl:if>
  346. <xsl:apply-templates/>
  347. </xsl:element>
  348. </xsl:template>
  349. <!--
  350. <xsl:include href="blocks.xsl"/>
  351. <xsl:include href="fields.xsl"/>
  352. -->
  353. <xsl:output method = "xml"/>
  354. <xsl:key name="style-types" match="rtf:paragraph-definition" use="@style-number"/>
  355. <xsl:variable name = "delete-list-text">true</xsl:variable>
  356. <xsl:variable name = "delete-field-blocks">true</xsl:variable>
  357. <xsl:variable name = "delete-annotation">false</xsl:variable>
  358. <xsl:template match="/">
  359. <xsl:call-template name="make-css-stylesheet"/>
  360. <html>
  361. <xsl:call-template name="make-header"/>
  362. <xsl:apply-templates/>
  363. </html>
  364. </xsl:template>
  365. <xsl:template match="rtf:doc">
  366. <xsl:apply-templates/>
  367. </xsl:template>
  368. <xsl:template match="rtf:preamble">
  369. <xsl:apply-templates/>
  370. </xsl:template>
  371. <xsl:template match="rtf:page-break">
  372. <br style = "page-break-after:always"/>
  373. </xsl:template>
  374. <xsl:template match="rtf:hardline-break">
  375. <br/>
  376. </xsl:template>
  377. <xsl:template match="rtf:rtf-definition|rtf:font-table|rtf:color-table|rtf:style-table|rtf:page-definition|rtf:list-table|rtf:override-table|rtf:override-list|rtf:list-text"/>
  378. <xsl:template match="rtf:body">
  379. <xsl:element name="body">
  380. <xsl:apply-templates/>
  381. <xsl:if test = "//rtf:footnote">
  382. <hr/>
  383. </xsl:if>
  384. <xsl:for-each select="//rtf:footnote">
  385. <xsl:apply-templates select="." mode="bottom"/>
  386. </xsl:for-each>
  387. </xsl:element>
  388. </xsl:template>
  389. <xsl:template match="rtf:section">
  390. <xsl:element name="div">
  391. <xsl:apply-templates/>
  392. </xsl:element>
  393. </xsl:template>
  394. <xsl:template match = "rtf:field-block">
  395. <xsl:apply-templates/>
  396. </xsl:template>
  397. <xsl:template match = "rtf:field[@type='hyperlink']">
  398. <xsl:element name ="a">
  399. <xsl:attribute name = "href">
  400. <xsl:choose>
  401. <xsl:when test="@argument">
  402. <xsl:value-of select="@argument"/>
  403. </xsl:when>
  404. <xsl:otherwise>
  405. <xsl:if test = "not(contains(@link, '/'))">#</xsl:if>
  406. <xsl:value-of select = "@link"/>
  407. </xsl:otherwise>
  408. </xsl:choose>
  409. </xsl:attribute>
  410. <xsl:apply-templates/>
  411. </xsl:element>
  412. </xsl:template>
  413. <xsl:template match = "rtf:field[@type='bookmark-start']">
  414. <xsl:element name ="a">
  415. <xsl:attribute name = "id">
  416. <xsl:value-of select = "@number"/>
  417. </xsl:attribute>
  418. <xsl:apply-templates/>
  419. </xsl:element>
  420. </xsl:template>
  421. <xsl:template match = "rtf:field">
  422. <xsl:apply-templates/>
  423. </xsl:template>
  424. <xsl:template match="rtf:pict">
  425. <img src = "{@num}"/>
  426. </xsl:template>
  427. <xsl:template match="*">
  428. <xsl:message>
  429. <xsl:text>no match for element: "</xsl:text>
  430. <xsl:value-of select="name(.)"/>
  431. <xsl:text>" &#xA;</xsl:text>
  432. </xsl:message>
  433. <xsl:apply-templates/>
  434. </xsl:template>
  435. </xsl:stylesheet>