[ShioriFeed] XSL style for Atom feed

This commit is contained in:
octospacc 2023-03-01 10:10:38 +01:00
parent ed2324e4d1
commit 7694d2310e
1 changed files with 116 additions and 17 deletions

View File

@ -4,7 +4,7 @@
# | [ ShioriFeed 🔖 (OctoSpacc) ] | #
# | Simple service for getting an Atom/RSS feed from your Shiori profile | #
# *----------------------------------------------------------------------* #
Version = '2023-02-16'
Version = '2023-02-28'
# *----------------------------------------------------------------------* #
# *-------------------------------------------* #
@ -20,6 +20,7 @@ DefFeedType = 'atom'
# TODO:
# - Cheking if Content mode content is actually present, otherwise fall back to Archive mode or original link (using API data is unreliable it seems)
# - HTML proxy (direct access to web UI, without JS)
# - Actually valid RSS
# - XML stylesheet
# - Filtering (tags, etc.)
@ -39,11 +40,8 @@ from socketserver import ThreadingMixIn
from urllib.parse import unquote as UrlUnquote
from urllib.request import urlopen, Request
HomeTemplate = '''\
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
HtmlHead = '''
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<!--
"bookmark" Emoji icon - Copyright 2021 Google Inc. All Rights Reserved.
@ -55,6 +53,13 @@ HomeTemplate = '''\
<meta name="description" content="Simple service for getting an Atom/RSS feed from your Shiori profile"/>
<meta property="og:title" content="ShioriFeed 🔖"/>
<meta property="og:description" content="Simple service for getting an Atom/RSS feed from your Shiori profile"/>
'''
HomeTemplate = '''\
<!DOCTYPE html>
<html lang="en">
<head>
{{HtmlHead}}
<style>
:root {
--cFore0: #232323;
@ -222,11 +227,103 @@ HomeTemplate = '''\
//};
} catch(e) {};
};
BtnChangeFeed.onclick = function() {
var CurType = Box.value.split('/')[-1].split('?')[0].split('.')[0];
if (CurType == 'atom') {
} else
if (CurType == 'rss') {
};
};
};
</script>
</body>
</html>
'''.replace('{{Version}}', Version)
'''.replace('{{HtmlHead}}', HtmlHead).replace('{{Version}}', Version)
XmlHead = '''\
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="#stylesheet"?>
<!DOCTYPE xml [<!ATTLIST xsl:stylesheet id ID #REQUIRED>]>
'''
XmlStyle='''
<!-- Partially derived from https://gist.github.com/andrewstiefel/57a0a400aa2deb6c9fe18c6da4e16e0f -->
<xsl:stylesheet
id="stylesheet"
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:atom="http://www.w3.org/2005/Atom"
exclude-result-prefixes="atom"
>
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
{{HtmlHead}}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style type="text/css">
* { max-width: 100%; height: auto; box-sizing: border-box; }
body { overflow-wrap: break-word; background: #ffffff; color: #000000; }
details { border: 2px solid gray; margin: 16px; }
details > summary { padding: 8px; background: #dddddd; }
details > summary > h2 { display: inline; }
details > summary img { max-height: 50vh; }
details > div { padding: 8px; }
</style>
<script type="application/javascript">
// TODO: Output escaping doesn't work on Firefox so we must parse XML via scripts to properly display it
//var Req = new XMLHttpRequest();
//Req.open('GET', window.location, false);
//Req.send();
//var Xml = Req.responseXML;
//var XsltProc = new XSLTProcessor(); // Get only the stylesheet from the XML
//XsltProc.importStylesheet(Xsl);
//var Result = XsltProc.transformToFragment(Xml, document);
//body.innerHTML = '';
//document.body.appendChild(Result);
//alert(1);
</script>
</head>
<body>
<section>
<xsl:apply-templates select="atom:feed" />
</section>
<section>
<xsl:apply-templates select="atom:feed/atom:entry" />
</section>
</body>
</html>
</xsl:template>
<xsl:template match="atom:feed"></xsl:template>
<xsl:template match="atom:entry">
<details class="entry">
<summary>
<h2>
<a>
<xsl:attribute name="href">
<xsl:value-of select="atom:id"/>
</xsl:attribute>
<xsl:value-of select="atom:title"/>
</a>
</h2>
<p>
<small>
Date: <xsl:value-of select="atom:updated"/>
</small>
<xsl:value-of select="atom:summary" disable-output-escaping="yes"/>
</p>
</summary>
<div>
<p>
<xsl:value-of select="atom:content" disable-output-escaping="yes"/>
</p>
</div>
</details>
</xsl:template>
</xsl:stylesheet>
'''.replace('{{HtmlHead}}', HtmlHead)
def RetDebugIf():
return f'\n\n{traceback.format_exc()}' if Debug else ''
@ -278,8 +375,9 @@ def MkFeed(Data, Remote, Username, Session, Type=DefFeedType):
'''
if Type == 'atom':
return f'''\
<?xml version="1.0" encoding="utf-8"?>
{XmlHead}
<feed xmlns="http://www.w3.org/2005/Atom">
{XmlStyle}
{FeedTitle}
{Generator}
<updated>{FeedDate}</updated>
@ -288,8 +386,9 @@ def MkFeed(Data, Remote, Username, Session, Type=DefFeedType):
'''
elif Type == 'rss':
return f'''\
<?xml version="1.0" encoding="utf-8"?>
{XmlHead}
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:media="http://search.yahoo.com/mrss/">
{XmlStyle}
<channel>
{FeedTitle}
{Generator}
@ -393,7 +492,7 @@ class Handler(BaseHTTPRequestHandler):
Post = self.rfile.read(int(self.headers['Content-Length'])).decode()
Body = HomeTemplate.replace('<!-- {{PostResult}} -->', f'''
<p>
Here's your <button>Atom</button> feed:
Here's your <button id="BtnChangeFeed">Atom</button> feed:
<textarea class="Visible" readonly="true">{MkUrl(Post, 'atom')}</textarea>
<textarea class="Hidden" hidden="true" readonly="true">{MkUrl(Post, 'rss')}</textarea>
</p>