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
|
assert 'public_wiki_folder' in build_state
|
||||||
|
|
||||||
replacement_table = _private_people_replacement_table(initialer)
|
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")
|
tid_files = (Path(build_state['public_wiki_folder']) / "tiddlers").glob("**/*.tid")
|
||||||
|
|
||||||
for tiddler in tid_files:
|
for tiddler in tid_files:
|
||||||
dirty = False
|
dirty = False
|
||||||
with tiddler.open() as f:
|
with tiddler.open() as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
for idx, line in enumerate(lines):
|
for i in range(len(lines)):
|
||||||
for replace_person, replace_initials in replacement_table.items():
|
for replace_person, replace_initials in replacement_table.items():
|
||||||
if replace_person in line:
|
if replace_person in lines[i]:
|
||||||
if '|' + replace_person + ']]' in line:
|
if '|' + replace_person + ']]' in lines[i]:
|
||||||
# link with the person as the target only;
|
# link with the person as the target only;
|
||||||
# beware that you might have put something private in the text
|
# beware that you might have put something private in the text
|
||||||
lines[idx] = line.replace(replace_person,
|
lines[i] = lines[i].replace(replace_person, 'PrivatePerson')
|
||||||
'PrivatePerson')
|
elif '[[' + replace_person + ']]' in lines[i]:
|
||||||
elif '[[' + replace_person + ']]' in line:
|
|
||||||
# link with the person as the target and text
|
# link with the person as the target and text
|
||||||
lines[idx] = line.replace(replace_person,
|
lines[i] = lines[i].replace(
|
||||||
replace_initials + '|PrivatePerson')
|
replace_person,
|
||||||
|
replace_initials + '|PrivatePerson')
|
||||||
else:
|
else:
|
||||||
# camel-case link or unlinked reference in text;
|
# camel-case link or unlinked reference in text;
|
||||||
# or spurious substring, so rule that out with the '\b' search
|
# 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",
|
r"\b" + re.escape(replace_person) + r"\b",
|
||||||
f'<<privateperson "{replace_initials}">>',
|
f'<<privateperson "{replace_initials}">>',
|
||||||
line
|
lines[i]
|
||||||
)
|
)
|
||||||
dirty = True
|
dirty = True
|
||||||
if dirty:
|
if dirty:
|
||||||
|
Loading…
Reference in New Issue
Block a user