summaryrefslogtreecommitdiff
path: root/tools/html-to-atom-dump.xsl
blob: fce78a214f4431db02dd356c688c42ff4280db27 (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
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:atom="http://www.w3.org/2005/Atom"
                xmlns:xhtml="http://www.w3.org/1999/xhtml"
                xmlns="http://www.w3.org/2005/Atom"
                version="1.0">
  <xsl:output method="xml" indent="yes"/>
  <xsl:variable name="baseIRI">
    <xsl:text>https://defanor.uberspace.net/</xsl:text>
  </xsl:variable>

  <!-- Feed -->
  <xsl:template match="/">
    <feed>
      <title>defanor's notes</title>
      <link rel="self" href="{$baseIRI}atom.xml" />
      <link rel="alternate" href="{$baseIRI}" />
      <id>
        <xsl:value-of select="$baseIRI" />
      </id>
      <!--
          Setting <updated> statically, since it applies to some
          meaningful feed updates. Perhaps should be modified on
          metadata changes.
      -->
      <updated>2018-04-14T10:00:00Z</updated>
      <xsl:apply-templates select="/list/entry" />
    </feed>
  </xsl:template>

  <!-- Turns index entries into Atom entries -->
  <xsl:template match="entry">
    <xsl:variable name="localPath">
      <xsl:text>../</xsl:text>
      <xsl:value-of select="@name" />
    </xsl:variable>
    <xsl:variable name="doc" select="document($localPath)" />
    <entry>
      <link rel="alternate" href="{$baseIRI}{@name}" />
      <id>
        <xsl:text>https://defanor.uberspace.net/</xsl:text>
        <xsl:value-of select="@name" />
      </id>
      <!-- Setting this statically for now. -->
      <author>
        <name>defanor</name>
      </author>
      <xsl:apply-templates mode="atom-entry" select="$doc" />
    </entry>
  </xsl:template>

  <xsl:template mode="atom-entry" match="/ | xhtml:head | xhtml:html">
    <xsl:apply-templates mode="atom-entry" />
  </xsl:template>

  <!-- Title -->
  <xsl:template mode="atom-entry" match="xhtml:title">
    <title>
      <xsl:value-of select="." />
    </title>
  </xsl:template>

  <!-- Description/summary -->
  <xsl:template mode="atom-entry"
                match="xhtml:meta[@name='description' or
                       @property='dc:description schema:description']">
    <summary>
      <xsl:value-of select="@content" />
    </summary>
  </xsl:template>

  <!--
      Atom is rather strict in its date-time requriements. It's not
      always convenient or useful to be that strict in HTML metadata,
      so might be better to introduce some check and conversion.
  -->
  <!-- Publication date -->
  <xsl:template mode="atom-entry"
                match="xhtml:meta[@property='dc:issued schema:datePublished']">
    <published>
      <xsl:value-of select="@content" />
    </published>
  </xsl:template>
  
  <!-- Modification date -->
  <xsl:template mode="atom-entry"
                match="xhtml:meta[@property='dc:modified schema:dateModified']">
    <updated>
      <xsl:value-of select="@content" />
    </updated>
  </xsl:template>

  <!-- Body/content -->
  <xsl:template mode="atom-entry" match="xhtml:body">
    <content type="xhtml">
      <!--
          "the content of atom:content MUST be a single XHTML div
          element"
      -->
      <xhtml:div>
        <xsl:copy-of select="*" />
      </xhtml:div>
    </content>
  </xsl:template>

  <!-- Skip everything else -->
  <xsl:template mode="atom-entry" match="node()" priority="0" />

</xsl:stylesheet>