"""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 ~/.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 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 = 'http://www.google.com/url?sa=D&q=' if url.startswith(google_redirect): return url.replace(google_redirect, '') def pre_format_html_hook(url, html): """Rewrite the body of a document before it's formatted. This function should return a string for ELinks to format, or None if ELinks should format the original document body. It can be used to repair bad HTML, alter the layout or colors of a document, etc. Arguments: url -- The URL of the document. html -- The body of the document. """ if "cygwin.com" in url: return html.replace('