File: hooks.rb

package info (click to toggle)
elinks 0.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,424 kB
  • sloc: ansic: 174,653; cpp: 31,967; sh: 7,841; python: 4,039; perl: 2,183; javascript: 1,794; pascal: 1,710; makefile: 1,006; yacc: 295; lisp: 125; awk: 79; ruby: 70
file content (229 lines) | stat: -rw-r--r-- 10,174 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# = 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 <thomas.adam22@gmail.com>
# -----

# 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 a 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 an 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 class=\"example\">)(.*?)(<\/p>)/, '<h2><font color="red">\2</font></h2>')
    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!(/<link rel.*? \/>/,'')

            # Turn on borders for the "main" table.
            html.gsub!(/(<table class=\"forumline\".*border=)(\".*?\")(.*?>)/,'\11\3')
            
            # In the LHS of the table for each postee, remove their image
            # link, and just display their status instead.
            html.gsub!(/(<span class=\"postdetails\">.*?)(<img src.*? \/>)/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!(/(<img src.*?)(alt=)(\"Reply With Quote\")\s*(title=\"reply with quote\")(.*? \/>)/i, '\1\2Q \4')
            html.gsub!(/(<img src.*?)(alt=)(\"edit\/delete this post\")\s*(title=\"edit\/delete this post\")(.*? \/>)/i, '\1\2E/D \4')
            html.gsub!(/(<img src.*?)(alt=)(\"delete this post\")\s*(title=\"delete this post\")(.*? \/>)/i, '\1\2D \4')
            html.gsub!(/(<img src.*?)(alt=)(\"post\")\s*(title=\"post\")(.*? \/>)/i, '\1\2--&gt; \4')
	    html.gsub!(/(<img src.*?)(alt=)(\"new post\")\s*(title=\"new post\")(.*? \/>)/i, '\1\2--&gt; \4')
	    html.gsub!(/(<img src.*?)(alt=)(\"view newest post\")\s*(title=\"view newest post\")(.*? \/>)/i, '\1\2--&gt; \4')
	    html.gsub!(/(<img src.*?)(alt=)(\"view latest post\")\s*(title=\"view latest post\")(.*? \/>)/i, '\1\2&lt;-- \4')


            # See above.
            html.gsub!(/(<img src.*?)(alt=)(\"view ip address of poster\")\s*(title=\"view ip address of poster\")(.*? \/>)/i, '\1\2IP />')
	    html.gsub!(/<table cellspacing=\"0\" cellpadding=\"0\"\s*border=\"0\"\s*height="18"\s*width=\"18\">/, '<table border="0">')
            html.gsub!(/(<img src.*?)(alt=)(\"view user\'s profile\")\s*(title=\"view user\'s profile\")(.*? \/>)/i, '\1\2Profile  />')
            html.gsub!(/(<img src.*?)(alt=)(\"send private message\")\s*(title=\"send private message\")(.*? \/>)/i, '\1\2PM />')
            html.gsub!(/(<img src.*?)(alt=)(\"send e-mail\")\s*(title=\"send e-mail\")(.*? \/>)/i, '\1\2email />')
            html.gsub!(/(<img src.*?)(alt=)(\"visit poster\'s website\")\s*(title=\"visit poster\'s website\")(.*? \/>)/i, '\1\2Web />&nbsp;')
            html.gsub!(/(<img src.*?)(alt=)(\"Yahoo Messenger\")\s*(title=\"Yahoo Messenger\")(.*? \/>)/i, '\1\2Yahoo />')
            html.gsub!(/(<img src.*?)(alt=)(\"Msn Messenger\")\s*(title=\"Msn Messenger\")(.*? \/>)/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!(/(<table.*border=)(\".*?\")\s*class=\"forumline\"\s*(.*?>)/,'\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!(/(<td class=\"row1\".*?)>(<img src.*No new posts.*>)/, '\1 bgcolor="red">&nbsp;</td>')
        html.gsub!(/(<td class=\"row1\".*?)>(<img src.*new posts.*>)/i, '\1 bgcolor="green">&nbsp;</td>')
        
        # These lines appear at the bottom of the pages as a kind of "key"
        # for the above changes.
        html.gsub!(/(<img src.*\"No new posts\".*\>)/,"<font color=\"red\">No New Posts</font></td>")
        html.gsub!(/(<img src.*\"New posts\".*\>)/,"<font color=\"green\">New Posts</font></td>")
        
        # Remove duplicate lines within the HTML that would otherwise
        # repeat the same thing.  (Damn annoying)
        html.gsub!(/<td><span class=\"gensmall\">([nN]o)|[Nn]ew posts\<\/span\>\<\/td\>/,"<td></td>")
        
        # 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!(/(<td class=\"row1\".*?)>(<img src.*locked.*>)/i, '\1 bgcolor="blue">&nbsp;</td>')
        
        # Orange indicates that the selected thread has moved.
        html.gsub!(/(<td class=\"row1\".*?)>(<img src.*moved.*>)/i, '\1 bgcolor="orange">&nbsp;</td>')
        
        # 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!(/(<td class=\"gensmall\">(.*?<\/td>|<img src.*?><\/td>))/,'')
        html.gsub!(/(<td.*?>)(<img src.*alt=\"(Sticky|Announcement|.*Popular.*)\".* \/><\/td>)/i, '')
        html.gsub!(/(<td.*?>)(<img src.*alt=\"Forum is locked\" \/><\/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!(/(<img src.*?)(alt=)(\"view (newest|latest) post\")\s*(title=\"view (newest|latest) post\")(.*? \/>)/i, '\1\2&lt;-- \4') if not url.match(s)
	
	# And in a similar fashion, remove the "Goto Page" links.
	html.gsub!(/(<img src.*?alt=\"goto page\".*? \/>)/i,'')
        
        # No need to have this in the table at the bottom.
        html.gsub!(/(<td class=\"row1\".*?><img src.*alt=\"who is online\".* \/><\/td>)/i,'')

        # Remove all the Link: rel references
        html.gsub!(/<link rel.*? \/>/,'')

        # Some phpBB sites use frames.  Ugh
        html.gsub!(/(<table width=\"100%\" cellpadding=\"4\" cellspacing=\"1\" border=)1>/,'\10>')
        html.gsub!(/(<frameset.*?)(border=)\"2\"(.*)(frameborder=)\"(.*?)\"(.*>)/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. "<host>:<port>" - 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