2017-09-29 13:38:38 +02:00
|
|
|
#!/usr/bin/env python
|
2016-09-22 01:18:13 +02:00
|
|
|
#
|
|
|
|
# Executes python-readability on current page and opens the summary as new tab.
|
|
|
|
#
|
2017-07-20 14:36:27 +02:00
|
|
|
# Depends on the python-readability package, or its fork:
|
|
|
|
#
|
|
|
|
# - https://github.com/buriy/python-readability
|
|
|
|
# - https://github.com/bookieio/breadability
|
|
|
|
#
|
2016-09-22 01:18:13 +02:00
|
|
|
# Usage:
|
|
|
|
# :spawn --userscript readability
|
|
|
|
#
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import codecs, os
|
|
|
|
|
|
|
|
tmpfile=os.path.expanduser('~/.local/share/qutebrowser/userscripts/readability.html')
|
|
|
|
if not os.path.exists(os.path.dirname(tmpfile)):
|
|
|
|
os.makedirs(os.path.dirname(tmpfile))
|
|
|
|
|
|
|
|
with codecs.open(os.environ['QUTE_HTML'], 'r', 'utf-8') as source:
|
2017-07-20 16:21:47 +02:00
|
|
|
data = source.read()
|
|
|
|
|
|
|
|
try:
|
|
|
|
from breadability.readable import Article as reader
|
|
|
|
doc = reader(data)
|
|
|
|
content = doc.readable
|
|
|
|
except ImportError:
|
|
|
|
from readability import Document
|
|
|
|
doc = Document(data)
|
|
|
|
content = doc.summary().replace('<html>', '<html><head><title>%s</title></head>' % doc.title())
|
2016-09-22 01:18:13 +02:00
|
|
|
|
|
|
|
with codecs.open(tmpfile, 'w', 'utf-8') as target:
|
|
|
|
target.write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />')
|
|
|
|
target.write(content)
|
|
|
|
|
|
|
|
with open(os.environ['QUTE_FIFO'], 'w') as fifo:
|
|
|
|
fifo.write('open -t %s' % tmpfile)
|