"""Example Python hooks for ELinks.
If ELinks is compiled with an embedded Python interpreter, it will try
to import a Python module called hooks when the browser starts up. To
use Python code from within ELinks, create a file called hooks.py in
the ~/.config/elinks directory, or in the system-wide configuration directory
(defined when ELinks was compiled), or in the standard Python search path.
An example hooks.py file can be found in the contrib/python directory of
the ELinks source distribution.
The hooks module may implement any of several functions that will be
called automatically by ELinks in appropriate circumstances; it may also
bind ELinks keystrokes to callable Python objects so that arbitrary Python
code can be invoked at the whim of the user.
Functions that will be automatically called by ELinks (if they're defined):
follow_url_hook() -- Rewrite a URL for a link that's about to be followed.
goto_url_hook() -- Rewrite a URL received from a "Go to URL" dialog box.
pre_format_html_hook() -- Rewrite a document's body before it's formatted.
proxy_for_hook() -- Determine what proxy server to use for a given URL.
quit_hook() -- Clean up before ELinks exits.
"""
import elinks
import base64
import os
import subprocess
from urllib.parse import urlparse, parse_qs, urlunparse
from bs4 import BeautifulSoup
from importlib import reload
dumbprefixes = {
"7th" : "http://7thguard.net/",
"b" : "http://babelfish.altavista.com/babelfish/tr/",
"bz" : "http://bugzilla.elinks.cz/",
"bug" : "http://bugzilla.elinks.cz/",
"d" : "http://www.dict.org/",
"g" : "http://www.google.com/",
"gg" : "http://www.google.com/",
"go" : "http://www.google.com/",
"fm" : "http://www.freshmeat.net/",
"sf" : "http://www.sourceforge.net/",
"dbug" : "http://bugs.debian.org/",
"dpkg" : "http://packages.debian.org/",
"pycur" : "http://www.python.org/doc/current/",
"pydev" : "http://www.python.org/dev/doc/devel/",
"pyhelp" : "http://starship.python.net/crew/theller/pyhelp.cgi",
"pyvault" : "http://www.vex.net/parnassus/",
"e2" : "http://www.everything2.org/",
"sd" : "http://www.slashdot.org/"
}
def goto_url_hook(url):
"""Rewrite a URL that was entered in a "Go to URL" dialog box.
This function should return a string containing a URL for ELinks to
follow, or an empty string if no URL should be followed, or None if
ELinks should follow the original URL.
Arguments:
url -- The URL provided by the user.
"""
if url in dumbprefixes:
return dumbprefixes[url]
def follow_url_hook(url):
"""Rewrite a URL for a link that's about to be followed.
This function should return a string containing a URL for ELinks to
follow, or an empty string if no URL should be followed, or None if
ELinks should follow the original URL.
Arguments:
url -- The URL of the link.
"""
google_redirect = 'https://www.google.com/url?'
if url.startswith(google_redirect):
try:
return parse_qs(urlparse(url).query)['q'][0]
except:
pass
def img2sixel(url):
data = subprocess.check_output(f"curl -s {url} | img2sixel", shell=True)
return base64.b64encode(data).decode('ascii')
def build_url(base_url, path):
# Returns a list in the structure of urlparse.ParseResult
url_parts = list(urlparse(base_url))
url_parts[2] = path
return urlunparse(url_parts)
def sixel(url, html):
if not html.startswith('