-
Notifications
You must be signed in to change notification settings - Fork 0
/
templatehell.rb
189 lines (146 loc) · 4.37 KB
/
templatehell.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
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
string = "
=={{int:filedesc}}==
{{Map
|title=Map of England from new systeme of the mathematicks, Mass
|author=Flamsteed, John
|date=1681
|source=[http://digitalcollections.nypl.org/items/510d47e1-ceb9-a3d9-e040-e00a18064a99 NYPL Repo]
|location=England, Europe
|scale=10632
|institution={{Institution:New York Public Library}}
|dimensions={{Size|cm|87|70}}
}}
"
string2 = "
{{Map
|title=Map of England from new systeme of the mathematicks, Mass
|author=Flamsteed, John
|date=1681
|source=[http://digitalcollections.nypl.org/items/510d47e1-ceb9-a3d9-e040-e00a18064a99 NYPL Repo]
|location=England, Europe
|scale=10632
|institution=3
|dimensions=2
}}
"
string3 = "
=={{int:filedesc}}==
foo
{{
Map|title=Map of England from new systeme of the mathematicks, Mass
}}
#equals
#inner template
"
def change_template(str, bbox)
map_attrs = str.split("|")
puts " map_attrs = str.split('|')"
puts map_attrs.inspect
new_attrs = []
map_template_attrs = []
bbox_array = bbox.split(",").map{|s| s.to_f.round(7)}
longitude = "#{bbox_array[0]}/#{bbox_array[2]}"
latitude = "#{bbox_array[1]}/#{bbox_array[3]}"
something_changed = false
map_attrs.each do | map_attr |
if map_attr.split("=").size == 2 && !map_attr.include?("<!--")
key, value = map_attr.split("=")
if key.include? "warped"
unless value.include?("yes") # don't edit if it's already there
something_changed = true
map_attr = "warped=yes\n"
end
end
if key.include? "latitude"
if value.strip != latitude
something_changed = true
map_attr = "latitude=#{latitude}\n"
end
end
if key.include? "longitude"
if value.strip != longitude
something_changed = true
map_attr = "longitude=#{longitude}\n"
end
end
map_template_attrs << map_attr
end ## size > 2
new_attrs << map_attr
end
if map_template_attrs.none? {|s| (s.include? "warp status") || (s.include? "Warp status") || (s.include? "warp_status") || (s.include? "Warp_status")}
something_changed = true
insert_at = new_attrs.size# >= 1 ? new_attrs.size : 0
new_attrs.insert(insert_at, "warp_status=warped\n")
end
if map_template_attrs.none? {|s| (s.include? "latitude") || (s.include? "Latitude") }
something_changed = true
insert_at = new_attrs.size# >= 1 ? new_attrs.size : 0
new_attrs.insert(insert_at, "latitude=#{latitude}\n" )
end
if map_template_attrs.none? {|s| (s.include? "longitude") || (s.include? "Longitude") }
something_changed = true
insert_at = new_attrs.size #>= 1 ? new_attrs.size : 0
new_attrs.insert(insert_at, "longitude=#{longitude}\n")
end
map_template = new_attrs.join("|")
if something_changed
return "{{"+ map_template+ "}}"
else
return nil
end
end
#
# returns the index of the end of the first template in the passed in string
# will return false if no ending characters can be determined
#
def find_end_index(str)
open_bracket = ["{", "{"]
close_bracket = ["}", "}"]
stack = []
end_index = nil
str.each_char.each_cons(2).with_index.each do | pair, index |
if pair == open_bracket
stack.push pair
elsif pair == close_bracket
x = stack.pop
if stack.empty?
puts "end of template"
puts "index " + index.to_s
end_index = index + 1
return end_index
end
end
#puts "stack" + stack.to_s
end
return stack.empty?
end
map_match = /\{{2}\s*(Map|Template:map)(.*)}}/mi.match(string)
string_start_index = string.index(/\{{2}\s*(Map|Template:map)(.*)}}/mi)
template_end_index = nil
if map_match
map_template = map_match[0]
template_end_index = find_end_index(map_template)
end
string_stop_index = string_start_index+template_end_index
if map_template && template_end_index
map_template_string = string[string_start_index..(string_stop_index)]
puts "mpa template string="
puts map_template_string
puts " - "
trimmed = map_template_string.chomp[2..-3].lstrip
puts "trimmed"
puts trimmed
puts " - "
bbox = "123,345,567,789"
new_template = change_template(trimmed, bbox)
puts new_template
puts "---"
if new_template
string[string_start_index..(string_stop_index+1)] = new_template
puts "changing"
else
puts "nothing changed"
end
puts "full string"
puts string
end