-
Notifications
You must be signed in to change notification settings - Fork 0
/
liberal_uri.rb
164 lines (124 loc) · 3.75 KB
/
liberal_uri.rb
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
## This file is part of BrokenLinkBot
## (C) 2012 Blevintron
## This code is released under the CC-BY-SA 3.0 License. See LICENSE.txt
## Description: liberal URI extraction and parsing
require 'uri'
# not ready for prime time
EXPERIMENTAL_PARSER = false
module URI
LIBERAL_REGEX = /https?:[^\s"<>\[\]]+/
EXTERNAL_BRACKET_LINK_REGEX = /\[.*?\]/m
def self.liberal_extract(s, schemes=nil)
unless EXPERIMENTAL_PARSER
return URI.extract(s,schemes)
end
results = []
# Redact internal wikilinks
s.gsub!(/\[\[.*?\]\]/mi, '')
# For-each bracketted external link
while true
match = EXTERNAL_BRACKET_LINK_REGEX.match(s)
break if match == nil
offset = match.begin(0)
link = match.to_s
url,title = extract_bracket_link link
results << url if url
s[ offset, link.size ] = ''
end
# Bare external links are tricky, and context
# sensitive. We must parse templates to get them right.
remnants = s.each_template(:redact_nested=>true) do |tem|
tem.each_param_non_canon do |key,value|
URI.liberal_extract_no_templates(value||key,results)
end
end
URI.liberal_extract_no_templates(remnants, results)
results
end
def self.liberal_extract_no_templates(s, results=[])
while true
match = LIBERAL_REGEX.match(s)
break if match == nil
offset = match.begin(0)
url = match.to_s
results << url
s = s[ (offset+url.size) .. -1 ]
end
results
end
# RFCs 1738, 2396, and 3986 be damned.
# This is closer to what wikipedia allows
def self.liberal_parse(s)
unless s =~ LIBERAL_REGEX
raise "Bad URL #{s}"
end
scheme = userinfo = host = port = nil
path = query = fragment = nil
# First, the scheme
scheme,colon,remainder = s.partition ':'
unless colon == ':'
raise "Bad URL #{s}"
end
unless remainder.start_with? '//'
raise "Bad URL #{scheme}: *** #{remainder}"
end
remainder = remainder[2..-1]
first_at = remainder.index '@'
first_slash = remainder.index '/'
first_huh = remainder.index '?'
first_hash = remainder.index '#'
end_userinfo_host = remainder.index(/[\/?#]/)
if end_userinfo_host
# There is a path/query/fragment after hostinfo
if first_at and first_at < end_userinfo_host
userinfo = remainder[0 ... first_at]
hostinfo = remainder[first_at+1 ... end_userinfo_host]
else
hostinfo = remainder[0 ... end_userinfo_host]
end
remainder = remainder[end_userinfo_host .. -1]
host,colon,port = hostinfo.partition ':'
next_sep = remainder.index(/[#\?]/)
if next_sep == nil
path = remainder
else
sep = remainder[next_sep,1]
if sep == '?'
path,sep,remainder = remainder.partition '?'
if remainder.include? '#'
query,sep,fragment = remainder.partition '#'
else
query = remainder
end
else
path,sep,fragment = remainder.partition '#'
end
end
else
# This url ends with host info
if first_at
userinfo,at,hostinfo = remainder.partition '@'
else
hostinfo = remainder
end
host,colon,port = hostinfo.partition ':'
end
if scheme == 'http'
return URI::HTTP.new(scheme, userinfo, host, port, nil, path, nil, query, fragment)
elsif scheme == 'https'
return URI::HTTPS.new(scheme, userinfo, host, port, nil, path, nil, query, fragment)
end
end
end
def extract_bracket_link str
if (str.start_with? '[') and (str.end_with? ']')
innards = str[1...-1]
url,sep,title = innards.partition(/\s+/m)
if sep == ''
return [innards,nil]
else
return [url,title]
end
end
nil
end