PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/ruby/hooks.rb

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