# = hooks.rb - ELinks/Ruby hooks # # == Module Constants # # The following global module constants are defined # # * ELinks::VERSION - The ELinks version. :-P # # * ELinks::HOME - The path to ELinks configuration files # # == Debugging Scripts # # When debugging you can use either # # p(obj, ...) # # or # # message(string) # # to print message strings. The printed strings will be shown in a message # box. # ----- # LAST UPDATE: 2006/06/21 -- Thomas Adam # ----- # Called when the user enters something into the goto URL dialog. # # Arguments: # @url the URL entered in the goto URL dialog. # @current_url the URL currently being viewed in ELinks. # # Returns the URL to go to. Return nil to use the @url argument as is # (leave it untouched). def ELinks::goto_url_hook(url, current_url) case url when /^localhost$/ return "http://localhost/" when /test-ruby/ # Currently used for debugging the exported variables. message(ELinks::VERSION + " - " + ELinks::HOME); return current_url end return url end # Called when the user decides to load some document by following a link, # entering an URL in the goto URL dialog, loading frames from a frameset (?) # etc. # # Arguments: # @url the URL being followed. # # Returns the URL to be followed. Return nil to use the @url argument as is. def ELinks::follow_url_hook(url) return url end # Called when a HTML document has been loaded - before the document rendering # begins. Makes it possible to fix up bad HTML code, remove tags etc. # # Arguments: # @url the URL of the document being loaded. # @html the source of the document being loaded. # # Returns the preformatted source of the document. Return nil to leave the # document source untouched. def ELinks::pre_format_html_hook(url, html) # Things start here. Note the match alternation to define those sites # which you want to rerender. # This regexp object is for matching against specific domains. domains="fvwm.lair.be|troubledlands.com|forums.gentoo.org" r = Regexp.new('http:\/\/'"#{domains}"'\/.*') s = Regexp.new('http:\/\/'"#{domains}"'\/view(forum|topic)|posting.*\.(html|php).*') if url.match(/http:\/\/edulinux.homeunix.org\/elinks-render\.html/) html.gsub!(/(

)(.*?)(<\/p>)/, '

\2

') return html end if url.match(r) # Handle viewtopic.php -- this has some very slightly different # code to the other pages found on phpBB sites. Again, the match # alternations are important here -- I ought to define a Regexp # object to handle this eventually, which would save code # duplication, at any rate. if url.match(s) # Start with the menubar at the top, and remove the image # links. We still have to add the description back in so that # we know what the link references. :) html.gsub!(/<(img.*?)>(faq|search|memberlist|usergroups|register|profile|you have no new messages|log out \[.*?\]|chat room<\/a>)/i,'\2') # Remove the long list of relative links (I don't like them). html.gsub!(//,'') # Turn on borders for the "main" table. html.gsub!(/()/,'\11\3') # In the LHS of the table for each postee, remove their image # link, and just display their status instead. html.gsub!(/(.*?)()/i,'\1') # Handle the options for each post. Depending on the status # of the person viewing this site, they could have none or all # of these -- either way they've been shortened. html.gsub!(/()/i, '\1\2Q \4') html.gsub!(/()/i, '\1\2E/D \4') html.gsub!(/()/i, '\1\2D \4') html.gsub!(/()/i, '\1\2--> \4') html.gsub!(/()/i, '\1\2--> \4') html.gsub!(/()/i, '\1\2--> \4') html.gsub!(/()/i, '\1\2<-- \4') # See above. html.gsub!(/()/i, '\1\2IP />') html.gsub!(/
/, '
') html.gsub!(/()/i, '\1\2Profile />') html.gsub!(/()/i, '\1\2PM />') html.gsub!(/()/i, '\1\2email />') html.gsub!(/()/i, '\1\2Web /> ') html.gsub!(/()/i, '\1\2Yahoo />') html.gsub!(/()/i, '\1\2MSN />') end # PhpBB's index page defines its layout as a series of tables. # The main table has a class of 'forumline' -- hence setting its # border property to 1 helps readability and to proide distinct # lines between the table sections. html.gsub!(/()/,'\11\3') # Change the background colour of the forum pages to white -- the # default is grey. html.gsub!(/(body \{\n\tbackground-color: )(\#E5E5E5\;)/,'\1white;') # Assuming you've told ELinks to remember your login details (not # that it matters), the "New posts" and "No New Post" indicators # were typically just defined in words. This looked ugly. Hence # in this case, I've changed the table structure to change the # background colour of these cells. A red colour indicates "No new # posts", whereas a green colour indicates "New Posts". html.gsub!(/(') html.gsub!(/(') # These lines appear at the bottom of the pages as a kind of "key" # for the above changes. html.gsub!(/()/,"No New Posts") html.gsub!(/()/,"New Posts") # Remove duplicate lines within the HTML that would otherwise # repeat the same thing. (Damn annoying) html.gsub!(/") # As with the red/green background colour for (No|New) Posts # above, a blue colour in the table indicates that the selected # thread is locked. html.gsub!(/(') # Orange indicates that the selected thread has moved. html.gsub!(/(') # Remove image repetitions at the bottom of the page entirely (the # only ever serve as a key for the page above -- this is why I am # using colour here.) html.gsub!(/(
()/, '\1 bgcolor="red"> ()/i, '\1 bgcolor="green"> ([nN]o)|[Nn]ew posts\<\/span\>\<\/td\>/,"()/i, '\1 bgcolor="blue"> ()/i, '\1 bgcolor="orange"> (.*?<\/td>|<\/td>))/,'') html.gsub!(/()(<\/td>)/i, '') html.gsub!(/()(<\/td>)/,'') # At the very top of the pages, remove the images, but retain the # links by way of their alt attributes. (For things like FAQ, # MemeberList, etc.) # # DON'T do this for the pages matched by regexp 's' -- to do so # would remove many other images those pages rely on (and that # have been munged in the 's' section above, anyway.) html.gsub!(/<(img.*?)>(.*?<\/a>)/i, '\2') if not url.match(s) # OK -- "View Latest Post" becomes "[<--]" which hopefully points # to the person's name. html.gsub!(/()/i, '\1\2<-- \4') if not url.match(s) # And in a similar fashion, remove the "Goto Page" links. html.gsub!(/()/i,'') # No need to have this in the table at the bottom. html.gsub!(/(<\/td>)/i,'') # Remove all the Link: rel references html.gsub!(//,'') # Some phpBB sites use frames. Ugh html.gsub!(/(/,'\10>') html.gsub!(/()/i,'\1\2"0"\3\4"no"\6') end return html end # Determining what proxy, if any, should be used to load a requested URL. # # Arguments: # @url the URL to be proxied. # # The hook should return one of the following: # 1. ":" - to use the specified host and port # 2. "" - to not use any proxy # 3. nil - to use the default proxies def ELinks::proxy_hook(url) return nil end # Called when ELinks quits and can be used to do required clean-ups like # removing any temporary files created by the hooks. def ELinks::quit_hook end