add set-tiddler-values builder

This commit is contained in:
Soren I. Bjornstad 2021-08-26 15:49:54 -05:00
parent eac4a1a3ca
commit 8beda01e41
1 changed files with 17 additions and 0 deletions

View File

@ -258,3 +258,20 @@ def replace_private_people() -> None:
if dirty:
with tiddler.open("w") as f:
f.writelines(lines)
@tzk_builder
def set_tiddler_values(mappings: Dict[str, str]) -> None:
"Set the 'text' field of selected config or other tiddlers to new values"
assert 'public_wiki_folder' in build_state
for tiddler, new_text in mappings.items():
tiddler_path = (Path(build_state['public_wiki_folder']) / "tiddlers" / tiddler)
with tiddler_path.open("r") as f:
tiddler_lines = f.readlines()
first_blank_line_index = next(idx
for idx, value in enumerate(tiddler_lines)
if not value.strip())
with tiddler_path.open("w") as f:
f.writelines(tiddler_lines[0:first_blank_line_index+1])
f.write(new_text)