walk the markdown directory to find subfolders

This commit is contained in:
Colin McMillen 2021-06-28 16:16:43 -04:00
parent 9a0dd558bf
commit 0a4b13e397

View File

@ -14,7 +14,7 @@ md_extensions = ['fenced_code', 'codehilite', 'nl2br', 'toc', 'smarty', 'tables'
def print_file(in_file, out_file): def print_file(in_file, out_file):
print('%-53s -> %s' % (in_file, out_file)) print('%-62s -> %s' % (in_file, out_file))
template = open('template.html').read() template = open('template.html').read()
@ -34,41 +34,49 @@ for (dirpath, _, filenames) in os.walk(static_directory):
out_filenames = [] out_filenames = []
# TODO: also walk the markdown directory
for markdown_filename in glob.glob(os.path.join(input_directory, '*.md')):
markdown_file = open(markdown_filename)
text = markdown_file.read()
markdown_file.close()
if not text.startswith('# '): for (dirpath, _, filenames) in os.walk(input_directory):
text = '# ' + text for filename in filenames:
markdown_filename = os.path.join(dirpath, filename)
if not markdown_filename.endswith('.md'):
continue
match = re.match(r'^(.*?)\n', text) markdown_file = open(markdown_filename)
if match: text = markdown_file.read()
title = match.group(1).lstrip('#') markdown_file.close()
else:
title = text
title += ' | Colin McMillen'
if markdown_filename == os.path.join(input_directory, 'index.md'):
title = 'Colin McMillen'
out_filename = os.path.basename(markdown_filename).replace('.md', '.html') if not text.startswith('# '):
text = '# ' + text
html = markdown.markdown(text, extensions=md_extensions, output_format='html5') match = re.match(r'^(.*?)\n', text)
output = template.replace('__TITLE_GOES_HERE__', title) if match:
output = output.replace('__CONTENT_GOES_HERE__', html) title = match.group(1).lstrip('# ')
else:
title = text
title += ' | Colin McMillen'
if markdown_filename == os.path.join(input_directory, 'index.md'):
title = 'Colin McMillen'
page_url = out_filename out_filename = os.path.basename(markdown_filename).replace('.md', '.html')
if page_url.endswith('index.html'): # strip off index.html
page_url = page_url[:-len('index.html')]
output = output.replace('__PAGE_URL_GOES_HERE__', page_url)
out_filenames.append(out_filename) html = markdown.markdown(text, extensions=md_extensions, output_format='html5')
out_fullpath = os.path.join(output_directory, out_filename) output = template.replace('__TITLE_GOES_HERE__', title)
print_file(markdown_filename, out_fullpath) output = output.replace('__CONTENT_GOES_HERE__', html)
out_file = open(out_fullpath, 'w')
out_file.write(output) page_url = out_filename
out_file.close() if page_url.endswith('index.html'): # strip off index.html
page_url = page_url[:-len('index.html')]
output = output.replace('__PAGE_URL_GOES_HERE__', page_url)
out_filenames.append(out_filename)
out_dirpath = os.path.join(output_directory, dirpath)
out_dirpath = out_dirpath.replace('/content', '', 1)
os.makedirs(out_dirpath, exist_ok=True)
out_fullpath = os.path.join(out_dirpath, out_filename)
print_file(markdown_filename, out_fullpath)
out_file = open(out_fullpath, 'w')
out_file.write(output)
out_file.close()
# TODO: make a sitemap / RSS? # TODO: make a sitemap / RSS?
#index_filename = os.path.join(output_directory, 'index.html') #index_filename = os.path.join(output_directory, 'index.html')