mirror of
https://github.com/venthur/blag.git
synced 2025-11-25 20:52:43 +00:00
differentiate between pages and articles
This commit is contained in:
34
blag/blag.py
34
blag/blag.py
@@ -89,15 +89,35 @@ def build(args):
|
|||||||
path = os.path.relpath(f'{root}/{dirname}', start=args.input_dir)
|
path = os.path.relpath(f'{root}/{dirname}', start=args.input_dir)
|
||||||
os.makedirs(f'{args.output_dir}/{path}', exist_ok=True)
|
os.makedirs(f'{args.output_dir}/{path}', exist_ok=True)
|
||||||
|
|
||||||
convert_to_html(
|
process_markdown(
|
||||||
convertibles, args.input_dir,
|
convertibles, args.input_dir,
|
||||||
args.output_dir,
|
args.output_dir,
|
||||||
args.template_dir
|
args.template_dir
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def convert_to_html(convertibles, input_dir, output_dir, template_dir):
|
def process_markdown(convertibles, input_dir, output_dir, template_dir):
|
||||||
|
"""Process markdown files.
|
||||||
|
|
||||||
|
This method processes the convertibles, converts them to html and
|
||||||
|
saves them to the respective destination paths.
|
||||||
|
|
||||||
|
If a markdown file has a `date` metadata field it will be recognized
|
||||||
|
as article otherwise as page.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
convertibles : List[Tuple[str, str]]
|
||||||
|
relative paths to markdown- (src) html- (dest) files
|
||||||
|
input_dir : str
|
||||||
|
output_dir : str
|
||||||
|
template_dir : str
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
articles, pages : List[Tuple[str, Dict]]
|
||||||
|
|
||||||
|
"""
|
||||||
env = Environment(
|
env = Environment(
|
||||||
loader=ChoiceLoader([
|
loader=ChoiceLoader([
|
||||||
FileSystemLoader([template_dir]),
|
FileSystemLoader([template_dir]),
|
||||||
@@ -108,7 +128,7 @@ def convert_to_html(convertibles, input_dir, output_dir, template_dir):
|
|||||||
md = markdown_factory()
|
md = markdown_factory()
|
||||||
|
|
||||||
articles = []
|
articles = []
|
||||||
|
pages = []
|
||||||
for src, dst in convertibles:
|
for src, dst in convertibles:
|
||||||
logger.debug(f'Processing {src}')
|
logger.debug(f'Processing {src}')
|
||||||
with open(f'{input_dir}/{src}', 'r') as fh:
|
with open(f'{input_dir}/{src}', 'r') as fh:
|
||||||
@@ -123,8 +143,10 @@ def convert_to_html(convertibles, input_dir, output_dir, template_dir):
|
|||||||
# everything else are just pages
|
# everything else are just pages
|
||||||
if meta and 'date' in meta:
|
if meta and 'date' in meta:
|
||||||
articles.append((dst, context))
|
articles.append((dst, context))
|
||||||
|
template = env.get_template('article.html')
|
||||||
template = env.get_template('article.html')
|
else:
|
||||||
|
pages.append((dst, content))
|
||||||
|
template = env.get_template('page.html')
|
||||||
result = template.render(context)
|
result = template.render(context)
|
||||||
with open(f'{output_dir}/{dst}', 'w') as fh_dest:
|
with open(f'{output_dir}/{dst}', 'w') as fh_dest:
|
||||||
fh_dest.write(result)
|
fh_dest.write(result)
|
||||||
@@ -163,6 +185,8 @@ def convert_to_html(convertibles, input_dir, output_dir, template_dir):
|
|||||||
with open('build/index.html', 'w') as fh:
|
with open('build/index.html', 'w') as fh:
|
||||||
fh.write(result)
|
fh.write(result)
|
||||||
|
|
||||||
|
return articles, pages
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
<h1>Archive</h1>
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block title %}{{ title }}{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
{% for entry in archive %}
|
{% for entry in archive %}
|
||||||
|
|
||||||
{% if entry.title %}
|
{% if entry.title %}
|
||||||
<h1><a href="{{entry.dst}}">{{entry.title}}</a></h1>
|
<h1><a href="{{entry.dst}}">{{entry.title}}</a></h1>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if entry.date %}
|
|
||||||
<p>Written on {{ entry.date.date() }}.</p>
|
<p>Written on {{ entry.date.date() }}.</p>
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
{% endblock %}
|
||||||
|
|||||||
@@ -1 +1,7 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block title %}{{ title }}{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
{{ content }}
|
{{ content }}
|
||||||
|
{% endblock %}
|
||||||
|
|||||||
27
blag/templates/base.html
Normal file
27
blag/templates/base.html
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>{% block title %}{% endblock %}</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>A Blog</h1>
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
|
<li><a href="/">Blog</a></li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<main>
|
||||||
|
{% block content %}
|
||||||
|
{% endblock %}
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
|
||||||
7
blag/templates/page.html
Normal file
7
blag/templates/page.html
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{% extends "base.html" %}
|
||||||
|
|
||||||
|
{% block title %}{{ title }}{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
{{ content }}
|
||||||
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user