-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown_converter.py
More file actions
96 lines (82 loc) · 2.6 KB
/
markdown_converter.py
File metadata and controls
96 lines (82 loc) · 2.6 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
import argparse
import markdown
import sass
import os
from jinja2 import Environment, FileSystemLoader
from pathlib import Path
class MarkdownConverter:
def __init__(self, template_dir="templates"):
self.template_dir = template_dir
self.env = Environment(loader=FileSystemLoader(template_dir))
def compile_sass(self, sass_file):
"""Compile SASS to CSS"""
if not os.path.exists(sass_file):
return ""
return sass.compile(filename=sass_file)
def convert(self, markdown_file, output_file, sass_file=None, title=None):
"""Convert markdown to HTML with optional styling"""
# Read markdown content
with open(markdown_file, 'r') as f:
md_content = f.read()
# Convert markdown to HTML
html_content = markdown.markdown(
md_content,
extensions=['fenced_code', 'tables', 'toc']
)
# Compile SASS if provided
css_content = self.compile_sass(sass_file) if sass_file else ""
# Render template
template = self.env.get_template('base.html')
output = template.render(
content=html_content,
css=css_content,
title=title or Path(markdown_file).stem
)
# Write output
with open(output_file, 'w') as f:
f.write(output)
return output_file
def main():
parser = argparse.ArgumentParser(
description='Convert markdown to styled HTML documents'
)
parser.add_argument(
'input',
help='Input markdown file'
)
parser.add_argument(
'-o', '--output',
help='Output HTML file (default: input_file.html)',
default=None
)
parser.add_argument(
'-s', '--style',
help='SASS style file to apply',
default=None
)
parser.add_argument(
'-t', '--title',
help='Document title',
default=None
)
parser.add_argument(
'--template-dir',
help='Directory containing HTML templates',
default='templates'
)
args = parser.parse_args()
# Set default output filename if not provided
if not args.output:
args.output = str(Path(args.input).with_suffix('.html'))
# Create converter and process file
converter = MarkdownConverter(template_dir=args.template_dir)
output_file = converter.convert(
args.input,
args.output,
args.style,
args.title
)
print(f"Created {output_file}")
if __name__ == '__main__':
main()