aboutsummaryrefslogtreecommitdiff
path: root/templates/html2tomboy.xsl
blob: f3da05e5a2865cb34ee9ddd1e47bd412d72d561a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:h="http://www.w3.org/1999/xhtml"
>
 <!--
  Convert HTML generated by grauphel's tomboy2html converter
  back into the tomboy note markup
 -->
 <xsl:output omit-xml-declaration="yes"/>

 <xsl:template match="/">
  <xsl:apply-templates select="h:html/h:body"/>
 </xsl:template>

 <xsl:template match="h:body">
  <xsl:call-template name="content"/>
 </xsl:template>

 <xsl:template name="content">
  <xsl:for-each select="child::node()">
   <xsl:choose>
    <xsl:when test="local-name(.) = ''">
     <xsl:call-template name="string-replace-all">
      <xsl:with-param name="text" select="string(.)" />
      <xsl:with-param name="replace" select="'&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;'" />
      <xsl:with-param name="by" select="'&#09;'" />
     </xsl:call-template>
    </xsl:when>
    <xsl:when test="local-name(.) = 'br'">
     <!-- do nothing -->
    </xsl:when>
    <xsl:when test="local-name(.) = 'ul'">
     <list>
      <xsl:for-each select="h:li">
       <list-item dir="ltr"><xsl:call-template name="content"/></list-item>
      </xsl:for-each>
     </list>
    </xsl:when>
    <xsl:when test="local-name(.) = 'b'">
     <bold><xsl:call-template name="content"/></bold>
    </xsl:when>
    <xsl:when test="local-name(.) = 'i'">
     <italic><xsl:call-template name="content"/></italic>
    </xsl:when>
    <xsl:when test="local-name(.) = 'span' and @class = 'strikethrough'">
     <strikethrough><xsl:call-template name="content"/></strikethrough>
    </xsl:when>
    <xsl:when test="local-name(.) = 'span' and @class = 'highlight'">
     <highlight><xsl:call-template name="content"/></highlight>
    </xsl:when>
    <xsl:when test="local-name(.) = 'span' and contains(@style, 'monospace')">
     <monospace><xsl:call-template name="content"/></monospace>
    </xsl:when>
    <xsl:when test="local-name(.) = 'span' and @class = 'small'">
     <xsl:text disable-output-escaping="yes">&lt;size:small></xsl:text>
     <xsl:call-template name="content"/>
     <xsl:text disable-output-escaping="yes">&lt;/size:small></xsl:text>
    </xsl:when>
    <xsl:when test="local-name(.) = 'span' and @class = 'large'">
     <xsl:text disable-output-escaping="yes">&lt;size:large></xsl:text>
     <xsl:call-template name="content"/>
     <xsl:text disable-output-escaping="yes">&lt;/size:large></xsl:text>
    </xsl:when>
    <xsl:when test="local-name(.) = 'span' and @class = 'huge'">
     <xsl:text disable-output-escaping="yes">&lt;size:huge></xsl:text>
     <xsl:call-template name="content"/>
     <xsl:text disable-output-escaping="yes">&lt;/size:huge></xsl:text>
    </xsl:when>
    <xsl:when test="local-name(.) = 'a' and contains(@href, '://')">
     <xsl:text disable-output-escaping="yes">&lt;link:url></xsl:text>
     <xsl:choose>
      <xsl:when test="starts-with(@href, 'file://')">
       <xsl:value-of select="substring(@href, 8)"/>
      </xsl:when>
      <xsl:otherwise>
       <xsl:value-of select="@href"/>
      </xsl:otherwise>
     </xsl:choose>
     <xsl:text disable-output-escaping="yes">&lt;/link:url></xsl:text>
    </xsl:when>
    <xsl:when test="local-name(.) = 'a' and not(contains(@href, '://'))">
     <xsl:text disable-output-escaping="yes">&lt;link:internal></xsl:text>
     <xsl:call-template name="content"/>
     <xsl:text disable-output-escaping="yes">&lt;/link:internal></xsl:text>
    </xsl:when>
    <xsl:otherwise>
     <xsl:message terminate="yes">
      Unsupported tag
      <xsl:value-of select="local-name(.)"/>
     </xsl:message>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:for-each>
 </xsl:template>

 <!-- http://geekswithblogs.net/Erik/archive/2008/04/01/120915.aspx -->
 <xsl:template name="string-replace-all">
  <xsl:param name="text" />
  <xsl:param name="replace" />
  <xsl:param name="by" />
  <xsl:choose>
   <xsl:when test="contains($text, $replace)">
    <xsl:value-of select="substring-before($text,$replace)" />
    <xsl:value-of select="$by" />
    <xsl:call-template name="string-replace-all">
     <xsl:with-param name="text" select="substring-after($text,$replace)" />
     <xsl:with-param name="replace" select="$replace" />
     <xsl:with-param name="by" select="$by" />
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$text" />
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>