Skip to content

Commit

Permalink
specify encoding and use path objects when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
marph91 committed Jan 1, 2025
1 parent 65b1446 commit 20e997f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
17 changes: 9 additions & 8 deletions examples/custom_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,18 @@ def create_files(api, tree, output_dir: Path):
create_files(api, item.child_items, current_directory)

for note in item.child_notes:
with open(
(current_directory / replacements(note.title)).with_suffix(".md"), "w"
) as outfile:
outfile.write(note.body)
note_path = (current_directory / replacements(note.title)).with_suffix(
".md"
)
note_path.write_text(note.body, encoding="utf-8")

for resource in item.child_resources:
# TODO: do this first and replace resource paths in the note body
resource_binary = api.get_resource_file(id_=resource.id)
with open(
current_directory / replacements(resource.title or resource.id), "wb"
) as outfile:
outfile.write(resource_binary)
resource_path = current_directory / replacements(
resource.title or resource.id
)
resource_path.write_bytes(resource_binary)


def main():
Expand Down
17 changes: 8 additions & 9 deletions examples/note_tree_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def main():
help="Title of the root notebooks. By default all notebooks are selected.",
)
parser.add_argument(
"--output", help="Path to the output file.", default="note_tree.txt"
"--output", type=Path, help="Path to the output file.", default="note_tree.txt"
)
args = parser.parse_args()

Expand Down Expand Up @@ -216,22 +216,22 @@ def main():
html = html.replace(f'href=":/{resource.id}"', "")
elif resource.mime.startswith("image"):
resource_binary = api.get_resource_file(resource.id)
resource_path = str(Path(tmpdirname) / resource.id)
with open(resource_path, "wb") as outfile:
outfile.write(resource_binary)
resource_path = Path(tmpdirname) / resource.id
resource_path.write_bytes(resource_binary)
# Replace joplin's local link with the path to the just
# downloaded resource. Use the "file:///" protocol:
# https://stackoverflow.com/a/18246357/7410886
html = html.replace(f":/{resource.id}", f"file:///{resource_path}")
html = html.replace(
f":/{resource.id}", f"file:///{str(resource_path)}"
)

# Replace remaining note links.
# - Joplin ID: ":/"
# - HTML ID: "#"
html = html.replace('href=":/', 'href="#')

if output_format == ".html":
with open(args.output, "w") as outfile:
outfile.write(html)
args.output.write_text(html, encoding="utf-8")
else:
from weasyprint import CSS, HTML

Expand All @@ -241,8 +241,7 @@ def main():

if output_format == ".txt":
txt_tree = item_tree_to_txt(sorted_item_tree)
with open(args.output, "w") as outfile:
outfile.write(txt_tree)
args.output.write_text(txt_tree, encoding="utf-8")


if __name__ == "__main__":
Expand Down

0 comments on commit 20e997f

Please sign in to comment.