1 # = hooks.rb - ELinks/Ruby hooks
5 # The following global module constants are defined
7 # * ELinks::VERSION - The ELinks version. :-P
9 # * ELinks::HOME - The path to ELinks configuration files
11 # == Debugging Scripts
13 # When debugging you can use either
21 # to print message strings. The printed strings will be shown in a message
25 # LAST UPDATE: 2006/06/21 -- Thomas Adam <thomas.adam22@gmail.com>
28 # Called when the user enters something into the goto URL dialog.
31 # @url the URL entered in the goto URL dialog.
32 # @current_url the URL currently being viewed in ELinks.
34 # Returns the URL to go to. Return nil to use the @url argument as is
35 # (leave it untouched).
37 def ELinks::goto_url_hook(url, current_url)
40 return "http://localhost/"
43 # Currently used for debugging the exported variables.
44 message(ELinks::VERSION + " - " + ELinks::HOME);
52 # Called when the user decides to load some document by following a link,
53 # entering a URL in the goto URL dialog, loading frames from a frameset (?)
57 # @url the URL being followed.
59 # Returns the URL to be followed. Return nil to use the @url argument as is.
61 def ELinks::follow_url_hook(url)
66 # Called when an HTML document has been loaded - before the document rendering
67 # begins. Makes it possible to fix up bad HTML code, remove tags etc.
70 # @url the URL of the document being loaded.
71 # @html the source of the document being loaded.
73 # Returns the preformatted source of the document. Return nil to leave the
74 # document source untouched.
76 def ELinks::pre_format_html_hook(url, html)
77 # Things start here. Note the match alternation to define those sites
78 # which you want to rerender.
80 # This regexp object is for matching against specific domains.
81 domains="fvwm.lair.be|troubledlands.com|forums.gentoo.org"
82 r = Regexp.new('http:\/\/'"#{domains}"'\/.*')
83 s = Regexp.new('http:\/\/'"#{domains}"'\/view(forum|topic)|posting.*\.(html|php).*')
85 if url.match(/http:\/\/edulinux.homeunix.org\/elinks-render\.html/)
86 html.gsub!(/(<p class=\"example\">)(.*?)(<\/p>)/, '<h2><font color="red">\2</font></h2>')
92 # Handle viewtopic.php -- this has some very slightly different
93 # code to the other pages found on phpBB sites. Again, the match
94 # alternations are important here -- I ought to define a Regexp
95 # object to handle this eventually, which would save code
96 # duplication, at any rate.
99 # Start with the menubar at the top, and remove the image
100 # links. We still have to add the description back in so that
101 # we know what the link references. :)
102 html.gsub!(/<(img.*?)>(faq|search|memberlist|usergroups|register|profile|you have no new messages|log out \[.*?\]|chat room<\/a>)/i,'\2')
104 # Remove the long list of relative links (I don't like them).
105 html.gsub!(/<link rel.*? \/>/,'')
107 # Turn on borders for the "main" table.
108 html.gsub!(/(<table class=\"forumline\".*border=)(\".*?\")(.*?>)/,'\11\3')
110 # In the LHS of the table for each postee, remove their image
111 # link, and just display their status instead.
112 html.gsub!(/(<span class=\"postdetails\">.*?)(<img src.*? \/>)/i,'\1')
114 # Handle the options for each post. Depending on the status
115 # of the person viewing this site, they could have none or all
116 # of these -- either way they've been shortened.
117 html.gsub!(/(<img src.*?)(alt=)(\"Reply With Quote\")\s*(title=\"reply with quote\")(.*? \/>)/i, '\1\2Q \4')
118 html.gsub!(/(<img src.*?)(alt=)(\"edit\/delete this post\")\s*(title=\"edit\/delete this post\")(.*? \/>)/i, '\1\2E/D \4')
119 html.gsub!(/(<img src.*?)(alt=)(\"delete this post\")\s*(title=\"delete this post\")(.*? \/>)/i, '\1\2D \4')
120 html.gsub!(/(<img src.*?)(alt=)(\"post\")\s*(title=\"post\")(.*? \/>)/i, '\1\2--> \4')
121 html.gsub!(/(<img src.*?)(alt=)(\"new post\")\s*(title=\"new post\")(.*? \/>)/i, '\1\2--> \4')
122 html.gsub!(/(<img src.*?)(alt=)(\"view newest post\")\s*(title=\"view newest post\")(.*? \/>)/i, '\1\2--> \4')
123 html.gsub!(/(<img src.*?)(alt=)(\"view latest post\")\s*(title=\"view latest post\")(.*? \/>)/i, '\1\2<-- \4')
127 html.gsub!(/(<img src.*?)(alt=)(\"view ip address of poster\")\s*(title=\"view ip address of poster\")(.*? \/>)/i, '\1\2IP />')
128 html.gsub!(/<table cellspacing=\"0\" cellpadding=\"0\"\s*border=\"0\"\s*height="18"\s*width=\"18\">/, '<table border="0">')
129 html.gsub!(/(<img src.*?)(alt=)(\"view user\'s profile\")\s*(title=\"view user\'s profile\")(.*? \/>)/i, '\1\2Profile />')
130 html.gsub!(/(<img src.*?)(alt=)(\"send private message\")\s*(title=\"send private message\")(.*? \/>)/i, '\1\2PM />')
131 html.gsub!(/(<img src.*?)(alt=)(\"send e-mail\")\s*(title=\"send e-mail\")(.*? \/>)/i, '\1\2email />')
132 html.gsub!(/(<img src.*?)(alt=)(\"visit poster\'s website\")\s*(title=\"visit poster\'s website\")(.*? \/>)/i, '\1\2Web /> ')
133 html.gsub!(/(<img src.*?)(alt=)(\"Yahoo Messenger\")\s*(title=\"Yahoo Messenger\")(.*? \/>)/i, '\1\2Yahoo />')
134 html.gsub!(/(<img src.*?)(alt=)(\"Msn Messenger\")\s*(title=\"Msn Messenger\")(.*? \/>)/i, '\1\2MSN />')
137 # PhpBB's index page defines its layout as a series of tables.
138 # The main table has a class of 'forumline' -- hence setting its
139 # border property to 1 helps readability and to proide distinct
140 # lines between the table sections.
141 html.gsub!(/(<table.*border=)(\".*?\")\s*class=\"forumline\"\s*(.*?>)/,'\11\3')
143 # Change the background colour of the forum pages to white -- the
145 html.gsub!(/(body \{\n\tbackground-color: )(\#E5E5E5\;)/,'\1white;')
147 # Assuming you've told ELinks to remember your login details (not
148 # that it matters), the "New posts" and "No New Post" indicators
149 # were typically just defined in words. This looked ugly. Hence
150 # in this case, I've changed the table structure to change the
151 # background colour of these cells. A red colour indicates "No new
152 # posts", whereas a green colour indicates "New Posts".
153 html.gsub!(/(<td class=\"row1\".*?)>(<img src.*No new posts.*>)/, '\1 bgcolor="red"> </td>')
154 html.gsub!(/(<td class=\"row1\".*?)>(<img src.*new posts.*>)/i, '\1 bgcolor="green"> </td>')
156 # These lines appear at the bottom of the pages as a kind of "key"
157 # for the above changes.
158 html.gsub!(/(<img src.*\"No new posts\".*\>)/,"<font color=\"red\">No New Posts</font></td>")
159 html.gsub!(/(<img src.*\"New posts\".*\>)/,"<font color=\"green\">New Posts</font></td>")
161 # Remove duplicate lines within the HTML that would otherwise
162 # repeat the same thing. (Damn annoying)
163 html.gsub!(/<td><span class=\"gensmall\">([nN]o)|[Nn]ew posts\<\/span\>\<\/td\>/,"<td></td>")
165 # As with the red/green background colour for (No|New) Posts
166 # above, a blue colour in the table indicates that the selected
168 html.gsub!(/(<td class=\"row1\".*?)>(<img src.*locked.*>)/i, '\1 bgcolor="blue"> </td>')
170 # Orange indicates that the selected thread has moved.
171 html.gsub!(/(<td class=\"row1\".*?)>(<img src.*moved.*>)/i, '\1 bgcolor="orange"> </td>')
173 # Remove image repetitions at the bottom of the page entirely (the
174 # only ever serve as a key for the page above -- this is why I am
175 # using colour here.)
176 html.gsub!(/(<td class=\"gensmall\">(.*?<\/td>|<img src.*?><\/td>))/,'')
177 html.gsub!(/(<td.*?>)(<img src.*alt=\"(Sticky|Announcement|.*Popular.*)\".* \/><\/td>)/i, '')
178 html.gsub!(/(<td.*?>)(<img src.*alt=\"Forum is locked\" \/><\/td>)/,'')
180 # At the very top of the pages, remove the images, but retain the
181 # links by way of their alt attributes. (For things like FAQ,
184 # DON'T do this for the pages matched by regexp 's' -- to do so
185 # would remove many other images those pages rely on (and that
186 # have been munged in the 's' section above, anyway.)
187 html.gsub!(/<(img.*?)>(.*?<\/a>)/i, '\2') if not url.match(s)
189 # OK -- "View Latest Post" becomes "[<--]" which hopefully points
190 # to the person's name.
191 html.gsub!(/(<img src.*?)(alt=)(\"view (newest|latest) post\")\s*(title=\"view (newest|latest) post\")(.*? \/>)/i, '\1\2<-- \4') if not url.match(s)
193 # And in a similar fashion, remove the "Goto Page" links.
194 html.gsub!(/(<img src.*?alt=\"goto page\".*? \/>)/i,'')
196 # No need to have this in the table at the bottom.
197 html.gsub!(/(<td class=\"row1\".*?><img src.*alt=\"who is online\".* \/><\/td>)/i,'')
199 # Remove all the Link: rel references
200 html.gsub!(/<link rel.*? \/>/,'')
202 # Some phpBB sites use frames. Ugh
203 html.gsub!(/(<table width=\"100%\" cellpadding=\"4\" cellspacing=\"1\" border=)1>/,'\10>')
204 html.gsub!(/(<frameset.*?)(border=)\"2\"(.*)(frameborder=)\"(.*?)\"(.*>)/i,'\1\2"0"\3\4"no"\6')
210 # Determining what proxy, if any, should be used to load a requested URL.
213 # @url the URL to be proxied.
215 # The hook should return one of the following:
216 # 1. "<host>:<port>" - to use the specified host and port
217 # 2. "" - to not use any proxy
218 # 3. nil - to use the default proxies
220 def ELinks::proxy_hook(url)
225 # Called when ELinks quits and can be used to do required clean-ups like
226 # removing any temporary files created by the hooks.
228 def ELinks::quit_hook