-- Example hooks.lua file, put in ~/.links/ as hooks.lua.
-- $Id: hooks.lua,v 1.1 2001/05/12 03:06:31 tjaden Exp $
----------------------------------------------------------------------
-- Local configuration
----------------------------------------------------------------------
-- ** IMPORTANT **
-- For security reasons, systems functions are not enabled by default.
-- To do so, uncomment the following line, but be careful about
-- including unknown code. Individual functions may be disabled by
-- assigning them to `nil'.
enable_systems_functions ()
-- openfile = nil -- may open files in write mode
-- readfrom = nil -- reading from pipe can execute commands
-- writeto = nil
-- appendto = nil
-- pipe_read = nil
-- remove = nil
-- rename = nil
-- execute = nil
-- exit = nil
-- Home directory: If you do not enable system functions, you will
-- need to set the following to your home directory.
home_dir = (getenv and getenv ("HOME")) or "/home/MYSELF"
hooks_file = home_dir.."/.links/hooks.lua"
-- Pausing: When external programs are run, sometimes we need to pause
-- to see the output. This is the string we append to the command
-- line to do that. You may customise it if you wish.
pause = '; echo -ne "\\n\\e[1;32mPress ENTER to continue...\\e[0m"; read'
-- When starting Netscape: Set to `nil' if you do not want to open a
-- new window for each document.
netscape_new_window = 1
-- Make ALT="" into ALT=" ": Makes web pages with superfluous
-- images look better. However, even if you disable the "Display links
-- to images" option, single space links to such images will appear.
-- To enable, set the following to 1. If necessary, you can change
-- this while in Links using the Lua Console, then reload the page.
-- See also the keybinding section at the end of the file.
mangle_blank_alt = nil
-- WWWoffle is a proxy/cache system designed for dial-up users.
-- If you have it, set this to the machine:port where it is installed
-- (e.g. "http://localhost:8080")
wwwoffle = nil
-- If you set this to non-`nil', the bookmark addon will be loaded,
-- and actions will be bound to my key bindings. Change them at the
-- bottom of the file.
bookmark_addon = nil
----------------------------------------------------------------------
-- case-insensitive gsub
----------------------------------------------------------------------
-- Please note that this is not completely correct yet.
-- It will not handle pattern classes like %a properly.
-- XXX: Fix this to handle pattern classes.
function gisub (s, pat, repl, n)
pat = gsub (pat, '(%a)',
function (v) return '['..strupper(v)..strlower(v)..']' end)
if n then
return gsub (s, pat, repl, n)
else
return gsub (s, pat, repl)
end
end
----------------------------------------------------------------------
-- goto_url_hook
----------------------------------------------------------------------
function match (prefix, url)
return strsub (url, 1, strlen (prefix)) == prefix
end
function strip (str)
return gsub (str, "%s*(%S.+%S)%s*", "%1")
end
function plusify (str)
return gsub (str, "%s", "+")
end
function goto_url_hook (url, current_url)
-- XXX: Use a table instead of if ... else ... else ...
-- Google search (e.g. ,gg unix browsers).
if match (",gg", url) then
url = plusify (strip (strsub (url, 4)))
return "http://www.google.com/search?q="..url.."&btnG=Google+Search"
-- Freshmeat search.
elseif match (",fm", url) then
url = plusify (strip (strsub (url, 4)))
return "http://www.freshmeat.net/search/?q="..url
-- Appwatch search (e.g. ,aw lynx).
elseif match (",aw", url) then
url = plusify (strip (strsub (url, 4)))
return "http://www.appwatch.com/Linux/Users/find?q="..url
-- Dictionary.com search (e.g. ,dict congenial).
elseif match (",dict", url) then
url = plusify (strip (strsub (url, 6)))
return "http://www.dictionary.com/cgi-bin/dict.pl?db=%2A&term="..url
-- Debian bug search
elseif match (",bts", url) then
url = plusify (strip (strsub (url, 5)))
return "http://bugs.debian.org/cgi-bin/bugreport.cgi?bug="
..url.."&submit=Search+..."
-- Netcraft.com search (e.g. ,whatis www.google.com).
elseif match (",whatis", url) then
url = plusify (strip (strsub (url, 8)))
return "http://uptime.netcraft.com/up/graph/?host="..url
-- LinuxToday home page.
elseif match (",lt", url) then
return "http://linuxtoday.com/"
-- User Friendly daily static.
elseif match (",uf", url) then
return "http://www.userfriendly.org/static/"
-- Weather forecast for Melbourne, Australia.
elseif match (",forecast", url) then
return "http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDF02V00.txt"
-- Local network web server.
elseif match (",local", url) then
return "http://desky/"
-- WWWoffle caches.
elseif match (",cache", url) and wwwoffle then
return wwwoffle.."/#indexes"
-- Expand ~ to home directories.
elseif match ("~", url) then
if strsub(url, 2, 2) == "/" then -- ~/foo
return home_dir..strsub(url, 2)
else -- ~foo/bar
return "/home/"..strsub(url, 2)
end
-- Unmatched.
else
return url
end
end
-----------------------------------------------------------------------
-- follow_url_hook
---------------------------------------------------------------------
function follow_url_hook (url)
-- Using bookmark addon.
if bookmark_addon then
if bm_is_category (url) then
return nil
else
return bm_get_bookmark_url (url) or url
end
-- Not using bookmark addon.
else
return url
end
end
----------------------------------------------------------------------
-- pre_format_html_hook
----------------------------------------------------------------------
-- Plain strfind (no metacharacters).
function sstrfind (s, pattern)
return strfind (s, pattern, 1, 1)
end
function pre_format_html_hook (url, html)
local ret = nil
-- Handle gzip'd files within reasonable size.
if strfind (url, "%.gz$") and strlen (html) < 4194304 then
local tmp = tmpname ()
writeto (tmp) write (html) writeto ()
html = pipe_read ("(gzip -dc "..tmp.." || cat "..tmp..") 2>/dev/null")
remove (tmp)
ret = 1
end
-- Mangle ALT="" in IMG tags.
if mangle_blank_alt then
local n
html, n = gisub (html, '(', '', 1)
html = gsub (html, '
\n