Selaa lähdekoodia

add xsl style to rss feed

Thomas Buck 1 vuosi sitten
vanhempi
commit
0770106dfe
2 muutettua tiedostoa jossa 146 lisäystä ja 1 poistoa
  1. 3
    1
      macros.py
  2. 143
    0
      static/css/rss.xsl

+ 3
- 1
macros.py Näytä tiedosto

@@ -546,6 +546,7 @@ def hook_preconvert_sitemap():
546 546
 # -----------------------------------------------------------------------------
547 547
 
548 548
 _RSS = """<?xml version="1.0" encoding="UTF-8"?>
549
+<?xml-stylesheet href="%s" type="text/xsl"?>
549 550
 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
550 551
 <channel>
551 552
 <title>%s</title>
@@ -609,13 +610,14 @@ def hook_postconvert_rss():
609 610
 
610 611
     items = "".join(items)
611 612
 
613
+    style = "/css/rss.xsl"
612 614
     title = "xythobuz.de Blog"
613 615
     link = "%s" % BASE_URL
614 616
     feed = "%s/rss.xml" % BASE_URL
615 617
     desc = htmlspecialchars("xythobuz Electronics & Software Projects")
616 618
     date = email.utils.formatdate()
617 619
 
618
-    rss = _RSS % (title, link, feed, desc, date, date, items)
620
+    rss = _RSS % (style, title, link, feed, desc, date, date, items)
619 621
 
620 622
     fp = codecs.open(os.path.join(output, "rss.xml"), "w", "utf-8")
621 623
     fp.write(rss)

+ 143
- 0
static/css/rss.xsl Näytä tiedosto

@@ -0,0 +1,143 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<!--
3
+
4
+# Pretty Feed
5
+
6
+Styles an RSS/Atom feed, making it friendly for humans viewers, and adds a link
7
+to aboutfeeds.com for new user onboarding. See it in action:
8
+
9
+   https://interconnected.org/home/feed
10
+
11
+
12
+## How to use
13
+
14
+1. Download this XML stylesheet from the following URL and host it on your own
15
+   domain (this is a limitation of XSL in browsers):
16
+
17
+   https://github.com/genmon/aboutfeeds/blob/main/tools/pretty-feed-v3.xsl
18
+
19
+2. Include the XSL at the top of the RSS/Atom feed, like:
20
+
21
+```
22
+<?xml version="1.0" encoding="UTF-8"?>
23
+<?xml-stylesheet href="/PATH-TO-YOUR-STYLES/pretty-feed-v3.xsl" type="text/xsl"?>
24
+```
25
+
26
+3. Serve the feed with the following HTTP headers:
27
+
28
+```
29
+Content-Type: application/xml; charset=utf-8  # not application/rss+xml
30
+x-content-type-options: nosniff
31
+```
32
+
33
+(These headers are required to style feeds for users with Safari on iOS/Mac.)
34
+
35
+
36
+
37
+## Limitations
38
+
39
+- Styling the feed *prevents* the browser from automatically opening a
40
+  newsreader application. This is a trade off, but it's a benefit to new users
41
+  who won't have a newsreader installed, and they are saved from seeing or
42
+  downloaded obscure XML content. For existing newsreader users, they will know
43
+  to copy-and-paste the feed URL, and they get the benefit of an in-browser feed
44
+  preview.
45
+- Feed styling, for all browsers, is only available to site owners who control
46
+  their own platform. The need to add both XML and HTTP headers makes this a
47
+  limited solution.
48
+
49
+
50
+## Credits
51
+
52
+pretty-feed is based on work by lepture.com:
53
+
54
+   https://lepture.com/en/2019/rss-style-with-xsl
55
+
56
+This current version is maintained by aboutfeeds.com:
57
+
58
+   https://github.com/genmon/aboutfeeds
59
+
60
+
61
+## Feedback
62
+
63
+This file is in BETA. Please test and contribute to the discussion:
64
+
65
+     https://github.com/genmon/aboutfeeds/issues/8
66
+
67
+-->
68
+<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
69
+                xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"
70
+                xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
71
+  <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" />
72
+  <xsl:template match="/">
73
+    <html lang="en">
74
+      <head>
75
+        <meta charset="utf-8" />
76
+        <title>RSS Feed - xythobuz.de</title>
77
+        <meta name="description">
78
+          <xsl:attribute name="content">
79
+            <xsl:value-of select="/rss/channel/description" />
80
+          </xsl:attribute>
81
+        </meta>
82
+        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
83
+        <link rel="author" href="xythobuz@xythobuz.de" />
84
+        <link rel="shortcut icon" href="img/favicon.ico" />
85
+        <link type="text/css" rel="stylesheet" href="css/style.css" />
86
+      </head>
87
+      <body>
88
+        <div id="wrap"><div id="nav">
89
+          <a>
90
+            <xsl:attribute name="href">
91
+              <xsl:value-of select="/rss/channel/link" />
92
+            </xsl:attribute>
93
+            <h1>
94
+              <xsl:value-of select="/rss/channel/title" />
95
+              - RSS Feed
96
+              <img src="img/rss.png" />
97
+            </h1>
98
+          </a>
99
+        </div></div>
100
+        <div id="content">
101
+          <p>
102
+            This is the RSS feed for my blog.
103
+            You can use it to get notified about new posts automatically.
104
+          </p>
105
+          <p>
106
+            If you're already used to this and wondering why this looks strange, this is a styled RSS feed.
107
+            Just copy the URL into your newsreader.
108
+          </p>
109
+          <p>
110
+            If you don't know what RSS is check out <a href="https://aboutfeeds.com">About Feeds</a> to get started.
111
+          </p>
112
+          <h1>Recent Blog Posts</h1>
113
+          <ul>
114
+          <xsl:for-each select="/rss/channel/item">
115
+            <li>
116
+              <a>
117
+                <xsl:attribute name="href">
118
+                  <xsl:value-of select="link" />
119
+                </xsl:attribute>
120
+                <xsl:value-of select="title" />
121
+              </a>
122
+              <br />
123
+              <span class="listdesc">
124
+                Published: <xsl:value-of select="substring(pubDate, 1, string-length(pubDate) - 15)" />
125
+              </span>
126
+              <br />
127
+              <span class="listdesc">
128
+                Updated: <xsl:value-of select="substring(atom:updated, 1, string-length(atom:updated) - 15)" />
129
+              </span>
130
+            </li>
131
+          </xsl:for-each>
132
+          </ul>
133
+          <p>
134
+            Last rebuild at: <xsl:value-of select="substring(/rss/channel/lastBuildDate, 1, string-length(/rss/channel/lastBuildDate) - 6)" />
135
+          </p>
136
+          <p>
137
+            Styled RSS feed inspired by <a href="https://darekkay.com/blog/rss-styling/">Darek Kay</a> and <a href="https://github.com/genmon/aboutfeeds/blob/main/tools/pretty-feed-v3.xsl">pretty-feed-v3</a>.
138
+          </p>
139
+        </div>
140
+      </body>
141
+    </html>
142
+  </xsl:template>
143
+</xsl:stylesheet>

Loading…
Peruuta
Tallenna