-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
66 lines (50 loc) · 2.13 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import markdown
import jinja2
import yaml
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def load_config(config_path):
with open(config_path, 'r') as config_file:
config = yaml.safe_load(config_file)
return config
def read_markdown_file(file_path):
with open(file_path, 'r') as f:
return f.read()
def load_metadata(metadata_path):
metadata = {}
if os.path.exists(metadata_path):
with open(metadata_path, 'r') as md_file:
metadata = yaml.safe_load(md_file)
return metadata
def generate_html_content(markdown_content):
return markdown.markdown(markdown_content)
def generate_output(output_dir, output_filename, base_template, post_html):
final_html = base_template.render(content=post_html)
output_path = os.path.join(output_dir, output_filename)
with open(output_path, 'w') as f:
f.write(final_html)
logger.info(f"Generated: {output_path}")
def main():
config = load_config('config.yaml')
template_loader = jinja2.FileSystemLoader('templates')
template_env = jinja2.Environment(loader=template_loader)
base_template = template_env.get_template('base.html')
post_template = template_env.get_template('post.html')
if not os.path.exists(config['output_dir']):
os.makedirs(config['output_dir'])
for filename in os.listdir(config['input_dir']):
if filename.endswith('.md'):
input_file_path = os.path.join(config['input_dir'], filename)
markdown_content = read_markdown_file(input_file_path)
html_content = generate_html_content(markdown_content)
metadata_path = os.path.join(config['input_dir'], os.path.splitext(filename)[0] + '.yaml')
metadata = load_metadata(metadata_path)
post_html = post_template.render(content=html_content, metadata=metadata)
output_filename = os.path.splitext(filename)[0] + '.html'
generate_output(config['output_dir'], output_filename, base_template, post_html)
logger.info("Website generation complete.")
if __name__ == "__main__":
main()