XSL file to convert HTML to tomboy markup
authorChristian Weiske <cweiske@cweiske.de>
Thu, 7 Jul 2016 15:11:16 +0000 (17:11 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 7 Jul 2016 15:15:41 +0000 (17:15 +0200)
templates/html2tomboy.xsl [new file with mode: 0644]
tests/data/full-formattest.html [new file with mode: 0644]
tests/html2tomboyXslTest.php [new file with mode: 0644]

diff --git a/templates/html2tomboy.xsl b/templates/html2tomboy.xsl
new file mode 100644 (file)
index 0000000..a354000
--- /dev/null
@@ -0,0 +1,93 @@
+<?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:value-of select="."/>
+    </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>
+
+</xsl:stylesheet>
diff --git a/tests/data/full-formattest.html b/tests/data/full-formattest.html
new file mode 100644 (file)
index 0000000..74d27c0
--- /dev/null
@@ -0,0 +1,41 @@
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+  <title>Test</title>
+ </head>
+ <body>Eine Zeile Text.<br />
+Zeilenumbruch.<br />
+Noch ein Zeilenumbruch.<br />
+<br />
+Eine Leerzeile obendrüber.<br />
+<br />
+Jetzt kommt eine Liste:<br />
+<ul><li>eins<br />
+</li><li>zwei<br />
+<ul><li>zwei-eins<br />
+<ul><li>zwei-eins-eins<br />
+</li><li>zwei-eins-zwei<br />
+</li></ul></li><li>zwei-zwei<br />
+</li></ul></li><li>drei<br />
+<ul><li>drei-eins<br />
+<ul><li>drei-eins-eins<br />
+<ul><li>drei-eins-eins-eins</li></ul></li></ul></li></ul></li></ul>
+Formatierungen:<br />
+<b>Fetter Text</b><br />
+<i>Kursiver Text</i><br />
+<span class="strikethrough">Durchgestrichener Text</span><br />
+<span class="highlight">Hervorgehobener Text</span><br />
+<span style="font-family:monospace; white-space: pre-wrap">Feste Breite</span><br />
+<br />
+Schriftgrößen:<br />
+<span class="small">klein</span><br />
+normal<br />
+<span class="large">groß</span><br />
+<span class="huge">riesig</span><br />
+<br />
+Links:<br />
+<ul><li><a href="Test.htm">Test</a><br />
+</li><li><a href="http://cweiske.de/?foo#bar">http://cweiske.de/?foo#bar</a><br />
+</li><li><a href="file:///home/cweiske/fam.jpg">/home/cweiske/fam.jpg</a></li></ul>
+<br />
+Ende.<br /></body>
+</html>
diff --git a/tests/html2tomboyXslTest.php b/tests/html2tomboyXslTest.php
new file mode 100644 (file)
index 0000000..5e9139e
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+class html2tomboyXslTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Use xsltproc and diff to check if the XSL file generates
+     * the same output as we expect.
+     */
+    public function testFormat()
+    {
+        exec(
+            'xsltproc'
+            . ' ../templates/html2tomboy.xsl'
+            . ' data/full-formattest.html'
+            . ' | diff -u data/formattest.tomboynotecontent -',
+            $out,
+            $retval
+        );
+        $this->assertEquals(
+            0, $retval,
+            "diff exit status is not 0:\n" . implode("\n", $out)
+        );
+    }
+}
+?>
\ No newline at end of file