-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown_to_html.py
More file actions
165 lines (139 loc) · 4.78 KB
/
Copy pathmarkdown_to_html.py
File metadata and controls
165 lines (139 loc) · 4.78 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import markdown
import re
import subprocess
from rich import print
from paths import ProjectPaths
def add_header_anchors(markdown_content):
"""Convert markdown headers to HTML with anchor IDs"""
def process_header(match):
header_text = match.group(2)
# Generate ID using same logic as convert_links
# Generate ID by keeping only alphanumeric chars and spaces
header_id = (
"".join(c for c in header_text if c.isalnum() or c == " ")
.lower()
.replace(" ", "-")
)
# Remove any remaining special characters
header_id = "".join(c for c in header_id if c.isalnum() or c == "-")
# Convert markdown header to HTML with anchor
if match.group(1) == "#":
return f'<h1 id="{header_id}">{header_text}</h1>'
elif match.group(1) == "##":
return f'<h2 id="{header_id}">{header_text}</h2>'
elif match.group(1) == "###":
return f'<h3 id="{header_id}">{header_text}</h3>'
return match.group(0)
# Match markdown headers (1-3 # symbols)
return re.sub(
r"^(#{1,3})\s+(.*)$", process_header, markdown_content, flags=re.MULTILINE
)
def convert_links(match):
# group(1) is always the target
# group(2) is the display text (optional)
target = match.group(1)
display_text = match.group(2) or target
if "#" in target:
id_with_hash = target.split("#", 1)[1]
id = id_with_hash.replace("#", "")
# Generate ID by keeping only alphanumeric chars and spaces
id = "".join(c for c in id if c.isalnum() or c == " ").lower().replace(" ", "-")
# Remove any remaining special characters
id = "".join(c for c in id if c.isalnum() or c == "-")
return f'<a href="#{id}">{display_text}</a>'
else:
return display_text
def convert_markdown_to_html(pth: ProjectPaths):
try:
with open(pth.output_markdown_file, "r", encoding="utf-8") as f:
markdown_content = f.read()
except FileNotFoundError:
print(f"Error: File not found: {pth.output_markdown_file}")
return
link_pattern = r"\[\[([^\]|]+)(?:\|([^\]]+))?\]\]"
markdown_content = re.sub(link_pattern, convert_links, markdown_content)
# First add header anchors to markdown
markdown_content = add_header_anchors(markdown_content)
# Then convert to HTML
html_content = markdown.markdown(markdown_content)
html_template = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Meditation Course on the Six Senses</title>
<style>
body {{
background-color: #000022;
color: #ffffff;
font-family: 'Noto Serif', serif;
line-height: 1.6;
margin: 0;
padding: 20px;
}}
blockquote {{
color: lightblue;
}}
.container {{
max-width: 800px;
margin: 0 auto;
}}
details {{
margin-bottom: 10px;
}}
summary {{
padding: 10px;
border: 1px solid #555;
cursor: pointer;
}}
a {{
color: lightblue;
text-decoration: underline;
}}
a:hover {{
color: white;
}}
</style>
</head>
<body>
<div class="container">
{html_content}
</div>
</body>
</html>
"""
try:
with open(pth.output_html_file, "w", encoding="utf-8") as f:
f.write(html_template)
print("Converting to html")
except Exception as e:
print(f"Error writing to file: {e}")
def convert_html_to_ebooks(pth: ProjectPaths):
try:
resource_path_arg = f"--resource-path={pth.output_dir}:{pth.output_site_dir}"
# Convert to EPUB
print("Converting to epub")
subprocess.run(
["pandoc", resource_path_arg, pth.output_html_file, "-o", pth.output_epub_file],
check=True,
capture_output=True,
text=True,
)
# Convert to .odt
print("Converting to docx")
try:
subprocess.run(
["pandoc", resource_path_arg, pth.output_html_file, "-o", pth.output_docx_file],
check=True,
)
except subprocess.CalledProcessError as e:
print(f"[red]Error code: {e.returncode}")
print(f"[red]Standard Output: {e.stdout}")
print(f"[red]Standard Error: {e.stderr}")
except subprocess.CalledProcessError as e:
print(f"[red]Error during conversion: {e}")
print(f"[red]Stdout: {e.stdout}")
print(f"[red]Stderr: {e.stderr}")
except Exception as e:
print(f"[red]An unexpected error occurred: {e}")