url2markdown/url2markdown/file.py

30 lines
884 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os.path
import markdownify
def _clean_title(raw_title):
title = str(raw_title)
bad_characters = ['«', '»', ',', '.', "'", "`",
"?", "", "{", "}", "(", ")", "[", "]", "!", "$"]
for character in bad_characters:
title = title.replace(character, '')
return title
def _build_path(title):
ressources_folder = "~/03_documents/obsidian/articles/"
base_filename = _clean_title(title)
short_path = os.path.join(ressources_folder, base_filename + ".md")
return os.path.expanduser(short_path)
def write_to_file(article, header):
file_path = _build_path(article.title)
markdown = markdownify.markdownify(article.html, heading_style='ATX')
with open(file_path, 'w') as f:
f.write(header)
f.write('------')
f.write('\n')
with open(file_path, 'a') as f:
f.write(markdown)