Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > edce90235d3141ac622d60d60f5d922e > files > 90

elinks-0.11.4-1mdv2008.1.x86_64.rpm

<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Example recipes</title><meta name="generator" content="DocBook XSL Stylesheets V1.68.1"><link rel="start" href="index.html" title="The ELinks Manual"><link rel="up" href="ch14.html" title="Chapter 14. Scripting ELinks with Lua"><link rel="prev" href="ch14s03.html" title="Using ELinks with Lua"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Example recipes</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch14s03.html">Prev</a> </td><th width="60%" align="center">Chapter 14. Scripting ELinks with Lua</th><td width="20%" align="right"> </td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2523435"></a>Example recipes</h2></div></div></div><p>This chapter contains some example scripts that you can use.  All of them come
from <code class="literal">contrib/lua/hooks.lua</code>.  I really recommend you to see it directly
instead of copying code out of this document.  Also, not everything in there is
covered here.</p><p>If you would like to contribute scripts, that would be great!  Please send
them to me at <a href="mailto:tjaden@users.sourceforge.net" target="_top">tjaden@users.sourceforge.net</a>.  Cliff and I plan to
start a script repository, provided we get some contributions.  As for script
ideas, you'll just have to be a little creative :-)</p><p>Also take a look at the <code class="literal">contrib/lua/</code> directory in the ELinks distribution.
Note that Peter and Cliff don't maintain the Lua support intensively anymore,
thus it would be probably nice to Cc me (<a href="mailto:pasky@ucw.cz" target="_top">pasky@ucw.cz</a>) if you want
to contribute some patch, so that I would be able to add it to the ELinks
distribution.</p><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2523497"></a>Go to URL on steroids</h3></div></div></div><p>There are some web sites that I visit often.  Bookmarks are okay, but they are
separate from the "Go to URL" dialog box, so I keep forgetting to use them.
Also, when I visit a search engine home page, all I really want to do is enter
a search term.</p><p>The following script allows me to type certain strings into the "Go to URL"
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
loading up the front page first.</p><div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Tip</h3><p>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.</p></div><pre class="screen">function match (prefix, url)
    return string.sub (url, 1, string.len (prefix)) == prefix
end

function strip (str)
    return string.gsub (str, "^%s*(.-)%s*$", "%1")
end

function plusify (str)
    return string.gsub (str, "%s", "+")
end

function goto_url_hook (url, current_url)
    -- Google search (e.g. ,gg unix browsers).
    if match (",gg", url) then
        url = plusify (strip (string.sub (url, 4)))
        return "http://www.google.com/search?q="..url.."&amp;btnG=Google+Search"

    -- Freshmeat search.
    elseif match (",fm", url) then
        url = plusify (strip (string.sub (url, 4)))
        return "http://www.freshmeat.net/search/?q="..url

    -- Dictionary.com search (e.g. ,dict congenial).
    elseif match (",dict", url) then
        url = plusify (strip (string.sub (url, 6)))
        return "http://www.dictionary.com/cgi-bin/dict.pl?db=%2A&amp;term="..url

    -- RPM search (e.g. ,rpm links).
    elseif match (",rpm", url) then
        url = plusify (strip (string.sub (url, 5)))
        return "http://www.rpmfind.net/linux/rpm2html/search.php?query="
                ..url.."&amp;submit=Search+..."

    -- Netcraft.com search (e.g. ,whatis www.google.com).
    elseif match (",whatis", url) then
        url = plusify (strip (string.sub (url, 8)))
        return "http://uptime.netcraft.com/up/graph/?host="..url

    -- LinuxToday home page.
    elseif match (",lt", url) then
        return "http://linuxtoday.com/"

    -- Weather forecast for Melbourne, Australia.
    elseif match (",forecast", url) then
        return "http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDV10450.txt"

    -- Unmatched
    else
        return url
    end
end</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2523574"></a>Expanding ~ (tilde)</h3></div></div></div><p>By adding an extra snippet of code to the previous example, we can make ELinks
expand pathnames such as <code class="literal">~/foo/bar</code>
and <code class="literal">~user/zappo</code>, like in the shell
and other Unix programs.</p><pre class="screen">function goto_url_hook (url, current_url)
                .
                .

    -- Expand ~ to home directories.
    elseif match ("~", url) then
        if string.sub(url, 2, 2) == "/" then    -- ~/foo
            return os.getenv ("HOME")..string.sub(url, 2)
        else                                    -- ~foo/bar
            return "/home/"..string.sub(url, 2)
        end

                .
                .</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2523621"></a>Filtering crap</h3></div></div></div><p>Many web pages nowadays have columns to the left and right of the text, which
are utterly useless.  If you happen to be viewing the page in a 80x25 screen,
the text you want to read ends up crammed into a tiny space in the centre.  We
use ELinks Lua support to manipulate the HTML before it reaches the parser.</p><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2523636"></a>linuxtoday.com</h4></div></div></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>This recipe is out of date for the web site.</p></div><p>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
to fix that:</p><pre class="screen">-- Plain string.find (no metacharacters)
function sstrfind (s, pattern)
    return string.find (s, pattern, 1, true)
end

function pre_format_html_hook (url, html)
    -- Strip the left and right columns from Linux Today pages
    -- and change the font colour to white.
    if sstrfind (url, "linuxtoday.com") then
        if sstrfind (url, "news_story") then
            html = string.gsub (html, '&lt;TABLE CELLSPACING="0".-&lt;/TABLE&gt;', '', 1)
            html = string.gsub (html, '&lt;TR BGCOLOR="#FFF.-&lt;/TR&gt;&lt;/TABLE&gt;', '', 1)
        else
            html = string.gsub (html, 'WIDTH="120"&gt;\n&lt;TR.+&lt;/TABLE&gt;&lt;/TD&gt;', '&gt;', 1)
        end
        html = string.gsub (html, '&lt;A HREF="http://www.internet.com.-&lt;/A&gt;', '')
        html = string.gsub (html, "&lt;IFRAME.-&lt;/IFRAME&gt;", "")
        -- emphasis in text is lost
        return string.gsub (html, 'text="#002244"', 'text="#001133"', 1)
    end

    return nil
end</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2523693"></a>linuxgames.com</h4></div></div></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>This recipe is out of date for the web site.</p></div><p>Here is a simpler example, for <a href="http://www.linuxgames.com/" target="_top">http://www.linuxgames.com/</a>.</p><pre class="screen">function pre_format_html_hook (url, html)
                .
                .

    elseif string.find (url, "linuxgames.com", 1, true) then
        return string.gsub (html, "&lt;CENTER&gt;.-&lt;/center&gt;", "", 1)

                .
                .</pre></div></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2523730"></a>Reading gzipped files</h3></div></div></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>ELinks already supports gzipped files natively.</p></div><p>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
files on a Unix system.</p><div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Warning</h3><p>This recipe opens a temporary file insecurely.</p></div><pre class="screen">function pre_format_html_hook (url, html)
                .
                .

    -- Handle gzip'd files within reasonable size.
    if string.find (url, "%.gz$") and string.len (html) &lt; 65536 then
        local name = tmpname ()
        local file = io.open (name, "wb")
        file:write (html)
        file:close ()
        html = pipe_read ("(gzip -dc "..name.." || cat "..name..") 2&gt;/dev/null")
        os.remove (name)
        return html
    end

                .
                .</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2523778"></a>Printing</h3></div></div></div><p>Printing a web page with ELinks usually involves quite a few steps: Save the
current document onto disk.  Run it through ELinks on the command-line (so it
fits into 80 columns) to generate a plain text version.  Remove the 80th
column from the text version, as it will make printers wrap down to the next
line.  Finally, run the processed file through `lpr', then delete it.</p><p>The following functions allow you to print web pages directly from ELinks,
using <code class="literal">lpr' or `enscript'.  Type `lpr()</code> or <code class="literal">enscript()</code> in the Lua Console to
run them.  (In the <code class="literal">hooks.lua</code>, I have also made it so you can just type <code class="literal">lpr</code>
or <code class="literal">enscript</code>.)</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>The <code class="literal">io.popen</code> function is not available on all platforms.</p></div><pre class="screen">function pipe_formatted_to (program)
    local lp, errmsg = io.popen (program, "w")
    if lp == nil then
        error (errmsg)
    else
        lp:write (current_document_formatted (79))
        lp:close ()
    end
end

-- Send the current document to `lpr'.
function lpr ()
    pipe_formatted_to ("lpr")
end

-- Send the current document to `enscript'.
function enscript ()
    pipe_formatted_to ("enscript -fCourier8")
end</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2523864"></a>Deferring to Netscape</h3></div></div></div><p>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
following function opens the current document in Netscape.</p><div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Tip</h3><p>You can also use the built-in “URI passing” feature for this.</p></div><pre class="screen">-- When starting Netscape: Set to `nil' if you do not want
-- to open a new window for each document.
netscape_new_window = 1

-- Open current document in Netscape.
function netscape ()
    local new = netscape_new_window and ",new_window" or ""
    execute ("( netscape -remote 'openURL("..current_url ()..new..")'"
             .." || netscape '"..current_url ().."' ) 2&gt;/dev/null &amp;")
end</pre></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2523905"></a>Alternative bookmark system</h3></div></div></div><p>Many people would like to have a bookmark system with categories (note that
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
an alternative bookmark system (for ELinks), which some people may like better
than the standard bookmark system.</p></div><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2523929"></a>More ideas</h3></div></div></div><div class="itemizedlist"><ul type="disc"><li>
The Lua interface needs to be redesigned to provide more flexible, coherent
   and usable interface to the scripts.
</li><li>
Cliff Cunnington had a neat idea of clipping text that you see in web pages
   (you enter a regexp that will match the start and end of the text you want
   to clip), and saving the text to disk, along with the URL and timestamp.
   This would help if you find that you can't ever remember where you had seen
   a piece of text, or if you want to keep a piece of information but don't
   need to save the entire page.
</li><li>
People who use download management programs could write a function to send
   the current link to their favourite downloading program.
</li><li>
If you wrote a small C program to put text into the X11 selection
   clipboard, you could pass the current link or URL to that program, to make
   it easier to paste URLs into other windows.  It might be possible to do the
   same with GPM, or the KDE/GNOME equivalents.
</li><li>
Send the current page to Babelfish for translation.
</li><li>
Look for stupid JavaScript URLs and convert them to something usable.
</li><li>
More things are possible, I'm sure.  If you have an idea that requires
   another hook or function, contact me (Peter Wang) and I'll see what I can
   do.
</li></ul></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch14s03.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch14.html">Up</a></td><td width="40%" align="right"> </td></tr><tr><td width="40%" align="left" valign="top">Using ELinks with Lua </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> </td></tr></table></div></body></html>