|
18 | 18 |
|
19 | 19 | import os |
20 | 20 | import sys |
| 21 | +import re |
21 | 22 |
|
22 | 23 | from functools import reduce |
23 | 24 | from json import dumps |
@@ -136,13 +137,24 @@ def read_stdin(): |
136 | 137 | return buffer |
137 | 138 |
|
138 | 139 |
|
139 | | -# Replace a substring from an HTML body, where the substring might be encoded |
| 140 | +# Replace a path from an HTML body, where the path might be encoded/decoded |
140 | 141 | # in many different ways (URL encoding, HTML escaping, ...). |
141 | | -def replace_from_all_encodings(string, to_replace, replace_with): |
142 | | - string = string.replace(quote(to_replace), replace_with) |
143 | | - string = string.replace(quote(quote(to_replace)), replace_with) |
144 | | - string = string.replace(unquote(to_replace), replace_with) |
145 | | - string = string.replace(unquote(unquote(to_replace)), replace_with) |
146 | | - string = string.replace(escape(to_replace), replace_with) |
147 | | - string = string.replace(dumps(to_replace), replace_with) |
148 | | - return string.replace(to_replace, replace_with) |
| 142 | +# |
| 143 | +# Note: |
| 144 | +# - :path: argument must not start with an "/". |
| 145 | +# - The path in the body followed by an alphanumeric character won't |
| 146 | +# be replaced. For example, "abc" will be replaced from "abc def" but |
| 147 | +# not "abcdef". |
| 148 | +def replace_path(string, path, replace_with): |
| 149 | + def sub(string, to_replace, replace_with): |
| 150 | + regex = re.escape(to_replace) + "(?=[^\\w]|$)" |
| 151 | + return re.sub(to_replace, replace_with, string) |
| 152 | + |
| 153 | + path = "/" + path |
| 154 | + string = sub(string, quote(path), replace_with) |
| 155 | + string = sub(string, quote(quote(path)), replace_with) |
| 156 | + string = sub(string, unquote(path), replace_with) |
| 157 | + string = sub(string, unquote(unquote(path)), replace_with) |
| 158 | + string = sub(string, escape(path), replace_with) |
| 159 | + string = sub(string, dumps(path), replace_with) |
| 160 | + return sub(string, path, replace_with) |
0 commit comments