Tomboy note taking program was working well for years.

It is time to convert my notes to markdown.

Here is how I did it.

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("&#x2028;", "\n", markdown)

    markdown = re.sub("&gt;", ">", markdown)
    markdown = re.sub("&lt;", "<", markdown)
    markdown = re.sub("&amp;", "&", 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)