From 503016694a65332e063e063668aca88daf0b9dc0 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sun, 29 Oct 2023 23:07:27 +0100 Subject: [PATCH] Rename markdown --- .gitignore | 2 ++ notion-to-joplin.py | 76 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 .gitignore mode change 100644 => 100755 notion-to-joplin.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..97aaa26 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/backup-notion-test.zip +/import to joplin/ diff --git a/notion-to-joplin.py b/notion-to-joplin.py old mode 100644 new mode 100755 index a2f69bc..4746fa0 --- a/notion-to-joplin.py +++ b/notion-to-joplin.py @@ -1,11 +1,75 @@ #!/usr/bin/env python3 +from argparse import ArgumentParser +import sys +import zipfile +import glob +import shutil +from os import path +import ntpath +import urllib.parse -# Step 1: Unzip the notion export zip file -# Step 2: Run this script in the same directory as the unzipped files + +# Step 1: Get the notion export zip file +# Step 2: Unzip the notion export zip file # Step 3: Rename every file with a .md extension with the heading of the file and fix all the links -# Step 4: Delete every heading file -# Step 5: Delete blank lines in beginning of every file -# Step 6: Rename all folder. Remove the "hash" from the ending of the folder name +# Step 4: Rename all folder. Remove the "hash" from the ending of the folder name -## Step 1 +# VARIABLES +FOLDER_EXTRACTION = "import to joplin" +MARKDOWN_EXTENSION = "md" +filename_backup = "" + +## Step 1: Get the notion export zip file +# Delete the folder if it exists +if path.exists(FOLDER_EXTRACTION): + shutil.rmtree(FOLDER_EXTRACTION) # Get parameter +parser = ArgumentParser( + prog="ProgramName", + description="What the program does", + epilog="Text at the bottom of help", +) +parser.add_argument("-f", "--file", help="File to be processed", required=True) +try: + args = parser.parse_args() + filename_backup = args.file +except: + parser.print_help() + sys.exit(0) + +## Step 2: Unzip the file +print("Unzipping backup file...") +with zipfile.ZipFile(filename_backup, "r") as zip_ref: + zip_ref.extractall(FOLDER_EXTRACTION) +print("Unzipping done.") + +## Step 3: Rename every file with a .md extension with the heading of the file and fix all the links +print("Renaming files and fixing links...") +# Get all markdown files +path_to_files = path.join(FOLDER_EXTRACTION, "**/*." + MARKDOWN_EXTENSION) +for filename in glob.iglob(path_to_files, recursive=True): + # Get the heading of the file + with open(filename, "r") as file: + # Get the heading + first_line = file.readline() + heading = first_line.replace("# ", "").replace("\n", "") + # Delete two first lines + lines = file.readlines() + lines_without_heading = lines[1:] + # Write the file without the heading + with open(filename, "w") as file: + file.write("".join(lines_without_heading)) + # Rename the file + new_filename = path.join(path.dirname(filename), heading + "." + MARKDOWN_EXTENSION) + shutil.move(filename, new_filename) + # Fix all the links + old_filename_encoded = urllib.parse.quote(ntpath.basename(filename)) + heading_encoded = urllib.parse.quote(heading) + "." + MARKDOWN_EXTENSION + for filename_to_fix in glob.iglob(path_to_files, recursive=True): + with open(filename_to_fix, "r") as file: + lines_to_fix = file.readlines() + with open(filename_to_fix, "w") as file: + text_to_write = "".join(lines_to_fix).replace( + old_filename_encoded, heading_encoded + ) + file.write(text_to_write)