Skip to content

Instantly share code, notes, and snippets.

Last active Jun 21, 2020
What would you like to do?
#!/usr/bin/env python
"""Executes python-readability on current page and opens the summary
as new tab.
Depends on the python-readability package found at .
Based off of
with fixes for various crashes and fixes URLs.
:spawn --userscript readability
from __future__ import absolute_import
import codecs
import os
from urllib.parse import urlparse, urljoin
from bs4 import BeautifulSoup
from readability import Document
# Styling for dynamic window margin scaling and line height
HEADER = """
<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
body {
margin: 40px auto;
max-width: 700px;
line-height: 1.4;
padding: 0 10px;
background: #111;
color: #eee;
font-size: larger;
a {
color: #0099ff;
a:visited {
color: #6666cc;
text-decoration: dashed underline;
a:visited:hover {
color: #111;
background: #6666cc;
text-decoration: none;
a:hover {
color: #111;
background: #0099ff;
text-decoration: none;
h1, h2, h3 {
line-height: 1.2;
img {
max-width: 100%%;
pre {
background: #1a1a1a;
padding: 7px;
border-radius: 10px;
table {
border-style: solid;
padding: 7px;
border-color: #222;
def tidy_url(s):
"""Take an URL-string and tidy it up for addressing resources from a
different host."""
u = urlparse(s)
if u.fragment and not (u.scheme or u.netloc or u.path or u.params or u.query):
return u.geturl()
return urljoin(os.environ['QUTE_URL'], s)
def setup_tmpfile():
"""Setup a temporary file for this script."""
tmpfile = os.path.join(
if not os.path.exists(os.path.dirname(tmpfile)):
return tmpfile
if __name__ == '__main__':
tmpfile = setup_tmpfile()
with['QUTE_HTML'], 'r', 'utf-8') as source:
data =
doc = Document(data)
title = doc.title()
content = doc.summary().replace('<html>', HEADER % ('📖 ' + title))
soup = BeautifulSoup(content, 'html.parser')
h1 = soup.new_tag('h1')
a = soup.new_tag('a', href=os.environ['QUTE_URL'])
a.string = title
h1.insert(0, a)
body = soup.find('body')
if body:
body.insert(0, h1)
for a in soup.find_all('a', attrs={'href': True}):
a['href'] = tidy_url(a['href'])
for img in soup.find_all('img', attrs={'src': True}):
img['src'] = tidy_url(img['src'])
with, 'w', 'utf-8') as target:
with open(os.environ['QUTE_FIFO'], 'w') as fifo:
fifo.write('open --related --tab %s' % tmpfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment