code to change Windows-style backslashes in file paths to frontslashes
The task is to replace Windows backslashes in paths, without indiscriminately replacing backslashes in legitimate use as escapes (or bridge terrain). Breaking this down: * "no-syntax-rewrite": I don't think this is really necessary, but I will follow the practice of the hack_syntax section. * if lines[i].lstrip().startswith("#"): Excludes lines that are only comments or defines. * precomment: Originally, I simply excluded "#" during the while statement, but I realized that this could wrongly mistake a Pango color code (or old-style markup for green) as a comment. I now look for whitespace before "#", and rewrote this section to operate on precomment rather than the whole line. * comment: Simply going with the second field would exclude the separator itself, so I use len. * if '\\': Technically, this code worked going straight to the while statement, but running every line through the complicated regex made it sluggish. Faster to make sure the line meets a simple filter first. * while: It is possible, though very uncommon, that a line could contain more than one file path. Looking at the regex test: a) Match the backslash itself, then be on the lookout for a character used to set apart a file path from other text. By excluding these, we ensure that there is an unbroken chain from the backslash to the file extension. b) Then we have a list of file extensions, bracketed on the left by a period and the right by \b, to make sure they do not coincidentally match a string. These are the file types that might be referenced by a value (for instance, translation files are not referred to directly, so their extensions are not included). As a practical matter, EVERY instance in the wild I know of involves png, not counting one commented-out path in an ancient campaign. c) File extensions can include capitals, particularly on Windows, where the effects of DOS unicase linger. So we make the search case-insensitive. * regex object: This splits the line into groups. The differences from the regex used in the while statement: a) We also look for a non-pathbreaking string to the left of the backslash as well as the right. This means that group(1) will match the entire file path except the extension. b) The \b boundary has been made a non-consuming look-ahead assertion, to simplify future references to the regex object and its groups. * fronted: The regex object, except all backslashes in group(1) are replaced by frontslashes. * precomment: Here, we simply reconstruct precomment with the modified regex object. * print: Besides reporting the substitution to stdout, I include a plea for cross-platform compatibility. * lines[i]: Now it's time to rebuild the whole line.
This commit is contained in:
parent
4de6d2b0af
commit
8b01e0b19c
1 changed files with 18 additions and 0 deletions
|
@ -1479,6 +1479,24 @@ def hack_syntax(filename, lines):
|
|||
# array of strings in lines. Modify lines in place as needed;
|
||||
# changes will be detected by the caller.
|
||||
#
|
||||
# Deal with a few Windows-specific problems for the sake of cross-
|
||||
# platform harmony. First, the use of backslashes in file paths.
|
||||
for i in range(len(lines)):
|
||||
if "no-syntax-rewrite" in lines[i]:
|
||||
break
|
||||
if lines[i].lstrip().startswith("#"):
|
||||
pass
|
||||
# Looking out for "#" used for color markup
|
||||
precomment = re.split(r'\s#', lines[i], 1)[0]
|
||||
comment = lines[i][len(precomment):]
|
||||
if '\\' in precomment:
|
||||
while re.search(r'(?<!\\)\\(?!\\)[^ ={}"]+\.(png|ogg|wav|gif|jpe?g|map|mask|cfg)\b', precomment, flags=re.IGNORECASE):
|
||||
backslash = re.search(r'([^ ={}"]*(?<!\\)\\(?!\\)[^ ={}"]+\.)(png|ogg|wav|gif|jpe?g|map|mask|cfg)(?=\b)', precomment, flags=re.IGNORECASE)
|
||||
fronted = backslash.group(1).replace("\\","/") + backslash.group(2)
|
||||
precomment = precomment[:backslash.start()] + fronted + precomment[backslash.end():]
|
||||
print '"%s", line %d: %s -> %s -- please use frontslash (/) for cross-platform compatibility' \
|
||||
% (filename, i+1, backslash.group(), fronted)
|
||||
lines[i] = precomment + comment
|
||||
# Ensure that every attack has a translatable description.
|
||||
for i in range(len(lines)):
|
||||
if "no-syntax-rewrite" in lines[i]:
|
||||
|
|
Loading…
Add table
Reference in a new issue