|
|
@ -2,6 +2,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from bs4 import BeautifulSoup as bs
|
|
|
|
from bs4 import BeautifulSoup as bs
|
|
|
|
|
|
|
|
import frontmatter
|
|
|
|
import magic
|
|
|
|
import magic
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
import pathlib
|
|
|
|
import pathlib
|
|
|
@ -42,6 +43,9 @@ def get_dirs(folder):
|
|
|
|
|
|
|
|
|
|
|
|
return r
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_required(src_filename, output_filename):
|
|
|
|
|
|
|
|
return not os.path.exists(output_filename) or os.path.getmtime(src_filename) > os.path.getmtime(output_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_args():
|
|
|
|
def get_args():
|
|
|
|
""" Get command line arguments """
|
|
|
|
""" Get command line arguments """
|
|
|
@ -58,6 +62,7 @@ def get_args():
|
|
|
|
parser.add_argument('-s', '--stylesheet', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/styles.css'))
|
|
|
|
parser.add_argument('-s', '--stylesheet', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/styles.css'))
|
|
|
|
parser.add_argument('-e', '--extra-index-content', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/extra_index_content.html'))
|
|
|
|
parser.add_argument('-e', '--extra-index-content', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/extra_index_content.html'))
|
|
|
|
parser.add_argument('-n', '--index-article-names', action='append', default=['index.md'])
|
|
|
|
parser.add_argument('-n', '--index-article-names', action='append', default=['index.md'])
|
|
|
|
|
|
|
|
parser.add_argument('-F', '--force', action="store_true", help="Generate new output html even if source file was modified before output html")
|
|
|
|
return parser.parse_args()
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -93,10 +98,11 @@ def main(args):
|
|
|
|
dirs_with_index_article = []
|
|
|
|
dirs_with_index_article = []
|
|
|
|
|
|
|
|
|
|
|
|
print(f"{markdown_files=}")
|
|
|
|
print(f"{markdown_files=}")
|
|
|
|
|
|
|
|
tag_dict = {}
|
|
|
|
for filename in markdown_files:
|
|
|
|
for filename in markdown_files:
|
|
|
|
print(f"{filename=}")
|
|
|
|
print(f"{filename=}")
|
|
|
|
print(f"{os.path.basename(filename)=}")
|
|
|
|
print(f"{os.path.basename(filename)=}")
|
|
|
|
html = pypandoc.convert_file(filename, 'html', extra_args=[f'--template={args.template}'])
|
|
|
|
|
|
|
|
if os.path.basename(filename) in args.index_article_names:
|
|
|
|
if os.path.basename(filename) in args.index_article_names:
|
|
|
|
output_filename = os.path.join(
|
|
|
|
output_filename = os.path.join(
|
|
|
|
os.path.dirname(re.sub(f"^{args.notes.name}", args.output_dir.name, filename)),
|
|
|
|
os.path.dirname(re.sub(f"^{args.notes.name}", args.output_dir.name, filename)),
|
|
|
@ -105,7 +111,24 @@ def main(args):
|
|
|
|
dirs_with_index_article.append(os.path.dirname(re.sub(f"^{args.notes.name}", args.output_dir.name, filename)))
|
|
|
|
dirs_with_index_article.append(os.path.dirname(re.sub(f"^{args.notes.name}", args.output_dir.name, filename)))
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
output_filename = os.path.splitext(re.sub(f"^{args.notes.name}", args.output_dir.name, filename))[0] + '.html'
|
|
|
|
output_filename = os.path.splitext(re.sub(f"^{args.notes.name}", args.output_dir.name, filename))[0] + '.html'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fm = frontmatter.load(filename)
|
|
|
|
|
|
|
|
if isinstance(fm.get('tags'), list):
|
|
|
|
|
|
|
|
for tag in fm.get('tags'):
|
|
|
|
|
|
|
|
if tag in tag_dict.keys():
|
|
|
|
|
|
|
|
tag_dict[tag].append({
|
|
|
|
|
|
|
|
'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])),
|
|
|
|
|
|
|
|
'title': fm.get('title')
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
tag_dict[tag] = [ {
|
|
|
|
|
|
|
|
'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])),
|
|
|
|
|
|
|
|
'title': fm.get('title')
|
|
|
|
|
|
|
|
} ]
|
|
|
|
print(f"{output_filename=}")
|
|
|
|
print(f"{output_filename=}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if update_required(filename, output_filename) or args.force:
|
|
|
|
|
|
|
|
html = pypandoc.convert_file(filename, 'html', extra_args=[f'--template={args.template}'])
|
|
|
|
os.makedirs(os.path.dirname(output_filename), exist_ok=True)
|
|
|
|
os.makedirs(os.path.dirname(output_filename), exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
with open(output_filename, 'w+') as fp:
|
|
|
|
with open(output_filename, 'w+') as fp:
|
|
|
@ -117,6 +140,7 @@ def main(args):
|
|
|
|
output_filename = re.sub(f"^{args.notes.name}", args.output_dir.name, filename) + '.html'
|
|
|
|
output_filename = re.sub(f"^{args.notes.name}", args.output_dir.name, filename) + '.html'
|
|
|
|
os.makedirs(os.path.dirname(output_filename), exist_ok=True)
|
|
|
|
os.makedirs(os.path.dirname(output_filename), exist_ok=True)
|
|
|
|
html = re.sub(r'\$title\$', title, TEXT_ARTICLE_TEMPLATE_HEAD)
|
|
|
|
html = re.sub(r'\$title\$', title, TEXT_ARTICLE_TEMPLATE_HEAD)
|
|
|
|
|
|
|
|
html = re.sub(r'\$h1title\$', title, html)
|
|
|
|
html = re.sub(r'\$raw\$', os.path.basename(filename), html)
|
|
|
|
html = re.sub(r'\$raw\$', os.path.basename(filename), html)
|
|
|
|
with open(filename) as fp:
|
|
|
|
with open(filename) as fp:
|
|
|
|
html += fp.read()
|
|
|
|
html += fp.read()
|
|
|
@ -131,6 +155,22 @@ def main(args):
|
|
|
|
os.makedirs(os.path.dirname(output_filename), exist_ok=True)
|
|
|
|
os.makedirs(os.path.dirname(output_filename), exist_ok=True)
|
|
|
|
shutil.copyfile(filename, output_filename)
|
|
|
|
shutil.copyfile(filename, output_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tagdir = os.path.join(args.output_dir, '.tags')
|
|
|
|
|
|
|
|
os.makedirs(tagdir, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for tag in tag_dict.keys():
|
|
|
|
|
|
|
|
html = re.sub(r'\$title\$', f'{tag}', INDEX_TEMPLATE_HEAD)
|
|
|
|
|
|
|
|
html = re.sub(r'\$h1title\$', f'tag: {tag}', html)
|
|
|
|
|
|
|
|
html = re.sub(r'\$extra_content\$', '', html)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for entry in tag_dict[tag]:
|
|
|
|
|
|
|
|
html += f"<div class=\"article\"><a href=\"/{entry['path']}\">{entry['title']}</a></div>"
|
|
|
|
|
|
|
|
html += INDEX_TEMPLATE_FOOT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with open(os.path.join(tagdir, f'{tag}.html'), 'w+') as fp:
|
|
|
|
|
|
|
|
fp.write(html)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dirs_to_index = [args.output_dir.name] + get_dirs(args.output_dir)
|
|
|
|
dirs_to_index = [args.output_dir.name] + get_dirs(args.output_dir)
|
|
|
|
print(f"{dirs_to_index=}")
|
|
|
|
print(f"{dirs_to_index=}")
|
|
|
@ -177,6 +217,7 @@ def main(args):
|
|
|
|
indexentries.sort(key=lambda entry: entry['isdirectory'], reverse=True)
|
|
|
|
indexentries.sort(key=lambda entry: entry['isdirectory'], reverse=True)
|
|
|
|
|
|
|
|
|
|
|
|
html = re.sub(r'\$title\$', directory, INDEX_TEMPLATE_HEAD)
|
|
|
|
html = re.sub(r'\$title\$', directory, INDEX_TEMPLATE_HEAD)
|
|
|
|
|
|
|
|
html = re.sub(r'\$h1title\$', directory, html)
|
|
|
|
html = re.sub(r'\$extra_content\$',
|
|
|
|
html = re.sub(r'\$extra_content\$',
|
|
|
|
EXTRA_INDEX_CONTENT if directory == os.path.commonpath(dirs_to_index) else '',
|
|
|
|
EXTRA_INDEX_CONTENT if directory == os.path.commonpath(dirs_to_index) else '',
|
|
|
|
html
|
|
|
|
html
|
|
|
@ -190,6 +231,7 @@ def main(args):
|
|
|
|
fp.write(html)
|
|
|
|
fp.write(html)
|
|
|
|
|
|
|
|
|
|
|
|
shutil.copyfile(args.stylesheet, os.path.join(args.output_dir.name, 'styles.css'))
|
|
|
|
shutil.copyfile(args.stylesheet, os.path.join(args.output_dir.name, 'styles.css'))
|
|
|
|
|
|
|
|
print(tag_dict)
|
|
|
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|