Created
January 21, 2023 12:39
-
-
Save gudata/594a526a3f5ad44d35ccd9b228bf0762 to your computer and use it in GitHub Desktop.
Conver the tomboy notes xml files to markdown format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import xml.etree.ElementTree as ET | |
import re | |
directory = "./" | |
output_directory = "./markdown" | |
def read_file_as_string(file_path): | |
with open(file_path, "r") as file: | |
return file.read() | |
def process_file(filename): | |
xml_file = os.path.join(directory, filename) | |
file_contents = read_file_as_string(xml_file) | |
match = re.search('<title>(.*?)</title>', file_contents) | |
if match: | |
original_filename = match.group(1) | |
filename = original_filename.replace('http://', '') | |
filename = re.sub(r'[^a-zA-Z а-яА-Я]', '', filename) | |
filename = re.sub(r'\s+', ' ', original_filename) | |
filename = filename.replace('/', '-') | |
filename += ".md" | |
markdown = file_contents.replace('<?xml version="1.0" encoding="utf-8"?>', "") | |
markdown = re.sub("<note[^>]*>", "", markdown) | |
markdown = re.sub("\s*<title>", "# ", markdown) | |
markdown = re.sub("</title>", "", markdown) | |
markdown = re.sub('<text xml:space="preserve">', "", markdown) | |
markdown = re.sub("</text>", "", markdown) | |
markdown = re.sub("\s*<text[^>]*>", "", markdown) | |
markdown = re.sub("<link:internal>", "", markdown) | |
markdown = re.sub("</link:internal>", "", markdown) | |
markdown = re.sub("<link:url>", "", markdown) | |
markdown = re.sub("</link:url>", "", markdown) | |
markdown = re.sub("<link:broken>", "", markdown) | |
markdown = re.sub("</link:broken>", "", markdown) | |
markdown = re.sub("<list>", "", markdown) | |
markdown = re.sub("</list>", "", markdown) | |
markdown = re.sub("<list-item[^>]*>", "- ", markdown) | |
markdown = re.sub("</list-item>", "", markdown) | |
markdown = re.sub("</note-content>.*", "", markdown, flags=re.DOTALL) | |
markdown = re.sub("<bold>", "**", markdown) | |
markdown = re.sub("</bold>", "**", markdown) | |
markdown = re.sub("<underline>", "_", markdown) | |
markdown = re.sub("</underline>", "_", markdown) | |
markdown = re.sub("
", "\n", markdown) | |
markdown = re.sub(">", ">", markdown) | |
markdown = re.sub("<", "<", markdown) | |
markdown = re.sub("&", "&", markdown) | |
with open(os.path.join(output_directory, filename.replace(".xml", ".md")), "w") as f: | |
f.write(markdown) | |
for filename in os.listdir(directory): | |
if filename.endswith(".note"): | |
process_file(filename) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment