mirror of
https://github.com/rkd77/elinks.git
synced 2024-12-04 14:46:47 -05:00
lua-scripting: Revised examples for Lua 5.0. And other updates.
Use admonishment elements, especially WARNING for insecure temporary files. Don't say anything about enabling system functions. Linuxgames.com no longer uses <CENTER>. AppWatch.com was shut down on 2001-08-31.
This commit is contained in:
parent
689d80fada
commit
e27a3c3f58
@ -146,7 +146,7 @@ goto_url_hook (url, current_url)::
|
|||||||
that ELinks should follow, or `nil` to cancel the operation.
|
that ELinks should follow, or `nil` to cancel the operation.
|
||||||
|
|
||||||
follow_url_hook (url)::
|
follow_url_hook (url)::
|
||||||
This hook is passed the URL that Links is about to follow. It should
|
This hook is passed the URL that ELinks is about to follow. It should
|
||||||
return a string (the URL modified or unmodified), or `nil` to stop
|
return a string (the URL modified or unmodified), or `nil` to stop
|
||||||
ELinks following the URL
|
ELinks following the URL
|
||||||
|
|
||||||
@ -173,7 +173,7 @@ lua_console_hook (string)::
|
|||||||
`someprogram`.
|
`someprogram`.
|
||||||
- `return "eval", "somefunction(1+2)"` will attempt to call the Lua
|
- `return "eval", "somefunction(1+2)"` will attempt to call the Lua
|
||||||
function `somefunction` with an argument, 3.
|
function `somefunction` with an argument, 3.
|
||||||
- `return "goto_url", "http://www.bogus.com"` will ask Links to visit
|
- `return "goto_url", "http://www.bogus.com"` will ask ELinks to visit
|
||||||
the URL "http://www.bogus.com".
|
the URL "http://www.bogus.com".
|
||||||
- `return nil` will do nothing.
|
- `return nil` will do nothing.
|
||||||
|
|
||||||
@ -188,7 +188,7 @@ Functions
|
|||||||
As well as providing hooks, ELinks provides some functions in addition to the
|
As well as providing hooks, ELinks provides some functions in addition to the
|
||||||
standard Lua functions.
|
standard Lua functions.
|
||||||
|
|
||||||
Note: The standard Lua function `os.setlocale` affects ELinks' idea of
|
NOTE: The standard Lua function `os.setlocale` affects ELinks' idea of
|
||||||
the system locale, which ELinks uses for the "System" charset, for the
|
the system locale, which ELinks uses for the "System" charset, for the
|
||||||
"System" language, and for formatting dates. This may however have to
|
"System" language, and for formatting dates. This may however have to
|
||||||
be changed in a future version of ELinks, in order to properly support
|
be changed in a future version of ELinks, in order to properly support
|
||||||
@ -219,21 +219,28 @@ current_document_formatted ([width])::
|
|||||||
pipe_read (command)::
|
pipe_read (command)::
|
||||||
Executes `command` and reads in all the data from stdout, until there
|
Executes `command` and reads in all the data from stdout, until there
|
||||||
is no more. This is a hack, because for some reason the standard Lua
|
is no more. This is a hack, because for some reason the standard Lua
|
||||||
function `read` seems to crash ELinks when used in pipe-reading mode.
|
function `file:read` seems to crash ELinks when used in pipe-reading
|
||||||
|
mode.
|
||||||
|
|
||||||
execute (string)::
|
execute (string)::
|
||||||
Executes shell commands `string` without waiting for it to exit. Beware
|
Executes shell commands `string` without waiting for it to exit. Beware
|
||||||
that you must not read or write to stdin and stdout. And unlike the
|
that you must not read or write to stdin and stdout. And unlike the
|
||||||
standard Lua function of the same name, the return value is
|
standard Lua function `os.execute`, the return value is meaningless.
|
||||||
meaningless.
|
|
||||||
|
|
||||||
tmpname ()::
|
tmpname ()::
|
||||||
Returns a unique name for a temporary file, or `nil` if no
|
Returns a unique name for a temporary file, or `nil` if no
|
||||||
such name is available. The returned string includes the
|
such name is available. The returned string includes the
|
||||||
directory name. This function does not create the file and
|
directory name. Unlike the standard Lua function
|
||||||
does not guarantee exclusive access to it: the caller must
|
`os.tmpname`, this one generates ELinks-related names
|
||||||
handle the possibility that another process creates the file
|
(currently with "elinks" at the beginning of the name).
|
||||||
and begins using it while this function is returning.
|
+
|
||||||
|
WARNING: The `tmpname` function does not create the file and does not
|
||||||
|
guarantee exclusive access to it: the caller must handle the
|
||||||
|
possibility that another process creates the file and begins
|
||||||
|
using it while this function is returning. Failing to do this
|
||||||
|
may expose you to symlink attacks by other users. To avoid
|
||||||
|
the risk, use `io.tmpfile` instead; unfortunately, it does not
|
||||||
|
tell you the name of the file.
|
||||||
|
|
||||||
bind_key (keymap, keystroke, function)::
|
bind_key (keymap, keystroke, function)::
|
||||||
Currently, `keymap` must be the string `"main"`. Keystroke is a
|
Currently, `keymap` must be the string `"main"`. Keystroke is a
|
||||||
@ -329,49 +336,48 @@ dialog box, and it will convert them to the URL I actually want to visit. As
|
|||||||
a bonus, it allows me perform some searches on sites like Google without
|
a bonus, it allows me perform some searches on sites like Google without
|
||||||
loading up the front page first.
|
loading up the front page first.
|
||||||
|
|
||||||
|
TIP: The ``URI rewriting'' feature of ELinks handles many of the same
|
||||||
|
tasks as the Lua hook shown here, and you can conveniently configure
|
||||||
|
it via the option manager. It is not quite as versatile, though.
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
function match (prefix, url)
|
function match (prefix, url)
|
||||||
return strsub (url, 1, strlen (prefix)) == prefix
|
return string.sub (url, 1, string.len (prefix)) == prefix
|
||||||
end
|
end
|
||||||
|
|
||||||
function strip (str)
|
function strip (str)
|
||||||
return gsub (str, "^%s*(.-)%s*$", "%1")
|
return string.gsub (str, "^%s*(.-)%s*$", "%1")
|
||||||
end
|
end
|
||||||
|
|
||||||
function plusify (str)
|
function plusify (str)
|
||||||
return gsub (str, "%s", "+")
|
return string.gsub (str, "%s", "+")
|
||||||
end
|
end
|
||||||
|
|
||||||
function goto_url_hook (url, current_url)
|
function goto_url_hook (url, current_url)
|
||||||
-- Google search (e.g. ,gg unix browsers).
|
-- Google search (e.g. ,gg unix browsers).
|
||||||
if match (",gg", url) then
|
if match (",gg", url) then
|
||||||
url = plusify (strip (strsub (url, 4)))
|
url = plusify (strip (string.sub (url, 4)))
|
||||||
return "http://www.google.com/search?q="..url.."&btnG=Google+Search"
|
return "http://www.google.com/search?q="..url.."&btnG=Google+Search"
|
||||||
|
|
||||||
-- Freshmeat search.
|
-- Freshmeat search.
|
||||||
elseif match (",fm", url) then
|
elseif match (",fm", url) then
|
||||||
url = plusify (strip (strsub (url, 4)))
|
url = plusify (strip (string.sub (url, 4)))
|
||||||
return "http://www.freshmeat.net/search/?q="..url
|
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).
|
-- Dictionary.com search (e.g. ,dict congenial).
|
||||||
elseif match (",dict", url) then
|
elseif match (",dict", url) then
|
||||||
url = plusify (strip (strsub (url, 6)))
|
url = plusify (strip (string.sub (url, 6)))
|
||||||
return "http://www.dictionary.com/cgi-bin/dict.pl?db=%2A&term="..url
|
return "http://www.dictionary.com/cgi-bin/dict.pl?db=%2A&term="..url
|
||||||
|
|
||||||
-- RPM search (e.g. ,rpm links).
|
-- RPM search (e.g. ,rpm links).
|
||||||
elseif match (",rpm", url) then
|
elseif match (",rpm", url) then
|
||||||
url = plusify (strip (strsub (url, 5)))
|
url = plusify (strip (string.sub (url, 5)))
|
||||||
return "http://www.rpmfind.net/linux/rpm2html/search.php?query="
|
return "http://www.rpmfind.net/linux/rpm2html/search.php?query="
|
||||||
..url.."&submit=Search+..."
|
..url.."&submit=Search+..."
|
||||||
|
|
||||||
-- Netcraft.com search (e.g. ,whatis www.google.com).
|
-- Netcraft.com search (e.g. ,whatis www.google.com).
|
||||||
elseif match (",whatis", url) then
|
elseif match (",whatis", url) then
|
||||||
url = plusify (strip (strsub (url, 8)))
|
url = plusify (strip (string.sub (url, 8)))
|
||||||
return "http://uptime.netcraft.com/up/graph/?host="..url
|
return "http://uptime.netcraft.com/up/graph/?host="..url
|
||||||
|
|
||||||
-- LinuxToday home page.
|
-- LinuxToday home page.
|
||||||
@ -380,7 +386,7 @@ function goto_url_hook (url, current_url)
|
|||||||
|
|
||||||
-- Weather forecast for Melbourne, Australia.
|
-- Weather forecast for Melbourne, Australia.
|
||||||
elseif match (",forecast", url) then
|
elseif match (",forecast", url) then
|
||||||
return "http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDF02V00.txt"
|
return "http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDV10450.txt"
|
||||||
|
|
||||||
-- Unmatched
|
-- Unmatched
|
||||||
else
|
else
|
||||||
@ -389,9 +395,6 @@ function goto_url_hook (url, current_url)
|
|||||||
end
|
end
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
(Note that this was noticably enhanced and rewritten in the ELinks standart
|
|
||||||
hooks.)
|
|
||||||
|
|
||||||
|
|
||||||
Expanding ~ (tilde)
|
Expanding ~ (tilde)
|
||||||
^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^
|
||||||
@ -402,21 +405,16 @@ and `~user/zappo`, like in the shell
|
|||||||
and other Unix programs.
|
and other Unix programs.
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
-- 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"
|
|
||||||
|
|
||||||
function goto_url_hook (url, current_url)
|
function goto_url_hook (url, current_url)
|
||||||
.
|
.
|
||||||
.
|
.
|
||||||
|
|
||||||
-- Expand ~ to home directories.
|
-- Expand ~ to home directories.
|
||||||
elseif match ("~", url) then
|
elseif match ("~", url) then
|
||||||
if strsub(url, 2, 2) == "/" then -- ~/foo
|
if string.sub(url, 2, 2) == "/" then -- ~/foo
|
||||||
return home_dir..strsub(url, 2)
|
return os.getenv ("HOME")..string.sub(url, 2)
|
||||||
else -- ~foo/bar
|
else -- ~foo/bar
|
||||||
return "/home/"..strsub(url, 2)
|
return "/home/"..string.sub(url, 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
.
|
.
|
||||||
@ -436,16 +434,16 @@ use ELinks Lua support to manipulate the HTML before it reaches the parser.
|
|||||||
linuxtoday.com
|
linuxtoday.com
|
||||||
++++++++++++++
|
++++++++++++++
|
||||||
|
|
||||||
Note: This recipe is out of date.
|
NOTE: This recipe is out of date for the web site.
|
||||||
|
|
||||||
Linux Today has two problems when viewed in ELinks: the useless columns on the
|
Linux Today has two problems when viewed in ELinks: the useless columns on the
|
||||||
left and the right and all the text appears in cyan. Here is a quick recipe
|
left and the right and all the text appears in cyan. Here is a quick recipe
|
||||||
to fix that:
|
to fix that:
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
-- Plain strfind (no metacharacters)
|
-- Plain string.find (no metacharacters)
|
||||||
function sstrfind (s, pattern)
|
function sstrfind (s, pattern)
|
||||||
return strfind (s, pattern, 1, 1)
|
return string.find (s, pattern, 1, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
function pre_format_html_hook (url, html)
|
function pre_format_html_hook (url, html)
|
||||||
@ -453,15 +451,15 @@ function pre_format_html_hook (url, html)
|
|||||||
-- and change the font colour to white.
|
-- and change the font colour to white.
|
||||||
if sstrfind (url, "linuxtoday.com") then
|
if sstrfind (url, "linuxtoday.com") then
|
||||||
if sstrfind (url, "news_story") then
|
if sstrfind (url, "news_story") then
|
||||||
html = gsub (html, '<TABLE CELLSPACING="0".-</TABLE>', '', 1)
|
html = string.gsub (html, '<TABLE CELLSPACING="0".-</TABLE>', '', 1)
|
||||||
html = gsub (html, '<TR BGCOLOR="#FFF.-</TR></TABLE>', '', 1)
|
html = string.gsub (html, '<TR BGCOLOR="#FFF.-</TR></TABLE>', '', 1)
|
||||||
else
|
else
|
||||||
html = gsub (html, 'WIDTH="120">\n<TR.+</TABLE></TD>', '>', 1)
|
html = string.gsub (html, 'WIDTH="120">\n<TR.+</TABLE></TD>', '>', 1)
|
||||||
end
|
end
|
||||||
html = gsub (html, '<A HREF="http://www.internet.com.-</A>', '')
|
html = string.gsub (html, '<A HREF="http://www.internet.com.-</A>', '')
|
||||||
html = gsub (html, "<IFRAME.-</IFRAME>", "")
|
html = string.gsub (html, "<IFRAME.-</IFRAME>", "")
|
||||||
-- emphasis in text is lost
|
-- emphasis in text is lost
|
||||||
return gsub (html, 'text="#002244"', 'text="#001133"', 1)
|
return string.gsub (html, 'text="#002244"', 'text="#001133"', 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -471,6 +469,8 @@ end
|
|||||||
linuxgames.com
|
linuxgames.com
|
||||||
++++++++++++++
|
++++++++++++++
|
||||||
|
|
||||||
|
NOTE: This recipe is out of date for the web site.
|
||||||
|
|
||||||
Here is a simpler example, for link:http://www.linuxgames.com/[].
|
Here is a simpler example, for link:http://www.linuxgames.com/[].
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
@ -478,8 +478,8 @@ function pre_format_html_hook (url, html)
|
|||||||
.
|
.
|
||||||
.
|
.
|
||||||
|
|
||||||
elseif strfind (url, "linuxgames.com", 1, 1) then
|
elseif string.find (url, "linuxgames.com", 1, true) then
|
||||||
return gsub (html, "<CENTER>.-</center>", "", 1)
|
return string.gsub (html, "<CENTER>.-</center>", "", 1)
|
||||||
|
|
||||||
.
|
.
|
||||||
.
|
.
|
||||||
@ -489,25 +489,27 @@ function pre_format_html_hook (url, html)
|
|||||||
Reading gzipped files
|
Reading gzipped files
|
||||||
^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
Note: ELinks already supports gzipped files natively.
|
NOTE: ELinks already supports gzipped files natively.
|
||||||
|
|
||||||
Sometimes documents come gzipped in order to save space, but then you need to
|
Sometimes documents come gzipped in order to save space, but then you need to
|
||||||
uncompress them to read them with ELinks. Here is a recipe to handle gzipped
|
uncompress them to read them with ELinks. Here is a recipe to handle gzipped
|
||||||
files on a Unix system.
|
files on a Unix system.
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
WARNING: This recipe opens a temporary file insecurely.
|
||||||
-- This script requires system functions.
|
|
||||||
|
|
||||||
|
-------------------------------------------------------------------------------
|
||||||
function pre_format_html_hook (url, html)
|
function pre_format_html_hook (url, html)
|
||||||
.
|
.
|
||||||
.
|
.
|
||||||
|
|
||||||
-- Handle gzip'd files within reasonable size.
|
-- Handle gzip'd files within reasonable size.
|
||||||
if strfind (url, "%.gz$") and strlen (html) < 65536 then
|
if string.find (url, "%.gz$") and string.len (html) < 65536 then
|
||||||
local tmp = tmpname ()
|
local name = tmpname ()
|
||||||
writeto (tmp) write (html) writeto ()
|
local file = io.open (name, "wb")
|
||||||
html = pipe_read ("(gzip -dc "..tmp.." || cat "..tmp..") 2>/dev/null")
|
file:write (html)
|
||||||
remove (tmp)
|
file:close ()
|
||||||
|
html = pipe_read ("(gzip -dc "..name.." || cat "..name..") 2>/dev/null")
|
||||||
|
os.remove (name)
|
||||||
return html
|
return html
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -530,25 +532,27 @@ using `lpr' or `enscript'. Type `lpr()` or `enscript()` in the Lua Console to
|
|||||||
run them. (In the `hooks.lua`, I have also made it so you can just type `lpr`
|
run them. (In the `hooks.lua`, I have also made it so you can just type `lpr`
|
||||||
or `enscript`.)
|
or `enscript`.)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
NOTE: The `io.popen` function is not available on all platforms.
|
||||||
-- This script requires system functions.
|
|
||||||
|
|
||||||
function catto (output)
|
-------------------------------------------------------------------------------
|
||||||
writeto (output)
|
function pipe_formatted_to (program)
|
||||||
write (current_document_formatted (79))
|
local lp, errmsg = io.popen (program, "w")
|
||||||
writeto ()
|
if lp == nil then
|
||||||
|
error (errmsg)
|
||||||
|
else
|
||||||
|
lp:write (current_document_formatted (79))
|
||||||
|
lp:close ()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Send the current document to `lpr'.
|
-- Send the current document to `lpr'.
|
||||||
function lpr ()
|
function lpr ()
|
||||||
-- You must compile Lua with `popen' support for pipes to work.
|
pipe_formatted_to ("lpr")
|
||||||
-- See `config' in the Lua distribution.
|
|
||||||
catto ("|lpr")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Send the current document to `enscript'.
|
-- Send the current document to `enscript'.
|
||||||
function enscript ()
|
function enscript ()
|
||||||
catto ("|enscript -fCourier8")
|
pipe_formatted_to ("enscript -fCourier8")
|
||||||
end
|
end
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
@ -560,9 +564,9 @@ If you come across a brain-dead web page that is totally unreadable with
|
|||||||
ELinks, you'd probably want to open it with a graphical browser. The
|
ELinks, you'd probably want to open it with a graphical browser. The
|
||||||
following function opens the current document in Netscape.
|
following function opens the current document in Netscape.
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
TIP: You can also use the built-in ``URI passing'' feature for this.
|
||||||
-- This function requires `execute', a system function.
|
|
||||||
|
|
||||||
|
-------------------------------------------------------------------------------
|
||||||
-- When starting Netscape: Set to `nil' if you do not want
|
-- When starting Netscape: Set to `nil' if you do not want
|
||||||
-- to open a new window for each document.
|
-- to open a new window for each document.
|
||||||
netscape_new_window = 1
|
netscape_new_window = 1
|
||||||
@ -580,13 +584,12 @@ Alternative bookmark system
|
|||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
Many people would like to have a bookmark system with categories (note that
|
Many people would like to have a bookmark system with categories (note that
|
||||||
ELinks already supports that, marketing name Hiearchical bookmarks), and also
|
ELinks already supports that, marketing name Hierarchical bookmarks), and also
|
||||||
to be able to view them and search for them in an HTML page. I have written
|
to be able to view them and search for them in an HTML page. I have written
|
||||||
an alternative bookmark system (for ELinks), which some people may like better
|
an alternative bookmark system (for ELinks), which some people may like better
|
||||||
than the standard bookmark system.
|
than the standard bookmark system.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
More ideas
|
More ideas
|
||||||
^^^^^^^^^^
|
^^^^^^^^^^
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user