Skip to content

Commit 9b7c95c

Browse files
committed
news2rss: add a stylesheet for the xml
1 parent 0045181 commit 9b7c95c

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

news2rss.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!#!/usr/bin/env python3
22
# Converts the news page to an RSS feed
33

4+
import os
5+
import base64
46
import re
57
import argparse
68
from bs4 import BeautifulSoup
@@ -103,7 +105,19 @@ def html_to_rss(html_content, feed_title, feed_description):
103105
description = ET.SubElement(item, "description")
104106
description.text = content
105107

106-
return ET.tostring(rss, encoding="unicode", xml_declaration=True)
108+
# Embed XSLT stylesheet
109+
script_dir = os.path.dirname(os.path.abspath(__file__))
110+
xsl_file = os.path.join(script_dir, "news2rss.xsl")
111+
with open(xsl_file, 'r', encoding='utf-8') as f:
112+
xsl_content = f.read()
113+
xsl_b64 = base64.b64encode(xsl_content.encode('utf-8')).decode('ascii')
114+
data_uri = f"data:text/xsl;base64,{xsl_b64}"
115+
116+
xml_str = ET.tostring(rss, encoding="unicode")
117+
full_xml = '<?xml version="1.0" encoding="UTF-8"?>\n'
118+
full_xml += f'<?xml-stylesheet type="text/xsl" href="{data_uri}"?>\n'
119+
full_xml += xml_str
120+
return full_xml
107121

108122

109123
def main():

news2rss.xsl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3+
<xsl:output method="html" encoding="UTF-8" indent="yes"/>
4+
5+
<xsl:template match="/rss/channel">
6+
<html>
7+
<head>
8+
<title><xsl:value-of select="title"/></title>
9+
<style>
10+
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
11+
.rss-info { background: #f0f8ff; border: 1px solid #d0e7ff; border-radius: 5px; padding: 15px; margin-bottom: 25px; }
12+
.rss-info h3 { margin-top: 0; color: #0066cc; }
13+
.rss-info p { margin-bottom: 8px; }
14+
.rss-url {
15+
font-family: monospace;
16+
font-size: 0.9em;
17+
color: #333;
18+
background: #fff;
19+
padding: 8px;
20+
border: 1px solid #ccc;
21+
border-radius: 3px;
22+
word-break: break-all;
23+
margin: 5px 0;
24+
display: block;
25+
}
26+
h1 { color: #333; border-bottom: 2px solid #ddd; }
27+
.item { margin-bottom: 30px; padding-bottom: 20px; border-bottom: 1px solid #eee; }
28+
.item h2 { color: #0066cc; }
29+
.item h2 a { color: #0066cc; text-decoration: none; }
30+
.item h2 a:hover { text-decoration: underline; }
31+
.date { color: #666; font-size: 0.9em; margin-bottom: 10px; }
32+
.description { line-height: 1.6; }
33+
</style>
34+
</head>
35+
<body>
36+
<div class="rss-info">
37+
<h3>📡 RSS Feed</h3>
38+
<p>This is a web view of an RSS feed. To subscribe to updates in your RSS reader, copy the current page URL from your browser's address bar.</p>
39+
</div>
40+
41+
<h1><xsl:value-of select="title"/></h1>
42+
<p><xsl:value-of select="description"/></p>
43+
44+
<xsl:for-each select="item">
45+
<div class="item">
46+
<h2>
47+
<a href="{link}">
48+
<xsl:value-of select="title"/>
49+
</a>
50+
</h2>
51+
<div class="date">
52+
<xsl:value-of select="pubDate"/>
53+
</div>
54+
<div class="description">
55+
<xsl:value-of select="description" disable-output-escaping="yes"/>
56+
</div>
57+
</div>
58+
</xsl:for-each>
59+
</body>
60+
</html>
61+
</xsl:template>
62+
63+
</xsl:stylesheet>

0 commit comments

Comments
 (0)