fix only one PrivatePerson replacement working per line
Because the 'line' variable was already bound in the outer loop, changes to the line in the inner loop were lost in subsequent inner-loop iterations, if required. Change to always indexing the array (which is defined outside the scope of the loops) directly on every reference. (This is why it's usually best not to modify things while looping over them. Maybe a refactor could be in order.)
This commit is contained in:
parent
f8edb70065
commit
0705c55e77
@ -388,31 +388,32 @@ def replace_private_people(initialer: Callable[[str], str] = None) -> None:
|
||||
assert 'public_wiki_folder' in build_state
|
||||
|
||||
replacement_table = _private_people_replacement_table(initialer)
|
||||
from pprint import pprint; pprint(replacement_table)
|
||||
tid_files = (Path(build_state['public_wiki_folder']) / "tiddlers").glob("**/*.tid")
|
||||
|
||||
for tiddler in tid_files:
|
||||
dirty = False
|
||||
with tiddler.open() as f:
|
||||
lines = f.readlines()
|
||||
for idx, line in enumerate(lines):
|
||||
for i in range(len(lines)):
|
||||
for replace_person, replace_initials in replacement_table.items():
|
||||
if replace_person in line:
|
||||
if '|' + replace_person + ']]' in line:
|
||||
if replace_person in lines[i]:
|
||||
if '|' + replace_person + ']]' in lines[i]:
|
||||
# link with the person as the target only;
|
||||
# beware that you might have put something private in the text
|
||||
lines[idx] = line.replace(replace_person,
|
||||
'PrivatePerson')
|
||||
elif '[[' + replace_person + ']]' in line:
|
||||
lines[i] = lines[i].replace(replace_person, 'PrivatePerson')
|
||||
elif '[[' + replace_person + ']]' in lines[i]:
|
||||
# link with the person as the target and text
|
||||
lines[idx] = line.replace(replace_person,
|
||||
replace_initials + '|PrivatePerson')
|
||||
lines[i] = lines[i].replace(
|
||||
replace_person,
|
||||
replace_initials + '|PrivatePerson')
|
||||
else:
|
||||
# camel-case link or unlinked reference in text;
|
||||
# or spurious substring, so rule that out with the '\b' search
|
||||
lines[idx] = re.sub(
|
||||
lines[i] = re.sub(
|
||||
r"\b" + re.escape(replace_person) + r"\b",
|
||||
f'<<privateperson "{replace_initials}">>',
|
||||
line
|
||||
lines[i]
|
||||
)
|
||||
dirty = True
|
||||
if dirty:
|
||||
|
Loading…
Reference in New Issue
Block a user