-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfrontmatter.py
More file actions
179 lines (166 loc) · 9.27 KB
/
frontmatter.py
File metadata and controls
179 lines (166 loc) · 9.27 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import yaml
def parse_front_matter(path):
title = None
weight = 9999
draft = False
publish = True
export_pdf = True
inside = False
with open(path, 'r', encoding='utf-8') as f:
for line in f:
if line.strip() == '---':
if not inside:
inside = True
continue
else:
break
if inside:
if line.startswith('title:'):
title = line.split(':', 1)[1].strip().strip('"')
elif line.startswith('weight:'):
try:
weight = int(line.split(':', 1)[1].strip())
except ValueError:
pass
elif line.startswith('draft:'):
draft_value = line.split(':', 1)[1].strip().lower()
draft = draft_value in ['true', '1', 'yes']
elif line.startswith('publish:'):
publish_value = line.split(':', 1)[1].strip().lower()
publish = publish_value in ['true', '1', 'yes']
elif line.startswith('export_pdf:'):
export_pdf_value = line.split(':', 1)[1].strip().lower()
export_pdf = export_pdf_value in ['true', '1', 'yes']
elif line.startswith('pdf:'):
pdf_value = line.split(':', 1)[1].strip().lower()
export_pdf = pdf_value in ['true', '1', 'yes']
return title, weight, draft, publish, export_pdf
def should_include(path: str, metadata: tuple = None, include_drafts: bool = False) -> bool:
if metadata is None:
import frontmatter
_, _, draft, publish, export_pdf = parse_front_matter(path)
else:
_, _, draft, publish, export_pdf = metadata
if draft and not include_drafts:
return False
if not publish:
return False
if not export_pdf:
return False
return True
def load_book_config(book_dir):
"""Load book configuration from the _index.md front matter."""
import os
from tree import find_asset
index_path = None
for name in ("_index.md", "index.md"):
candidate = os.path.join(book_dir, name)
if os.path.exists(candidate):
index_path = candidate
break
if not index_path:
return {}
config = {}
with open(index_path, 'r', encoding='utf-8') as f:
content = f.read()
# Extract front matter
if content.startswith('---'):
try:
# Find the end of front matter
end_pos = content.find('\n---\n', 3)
if end_pos != -1:
front_matter = content[3:end_pos]
metadata = yaml.safe_load(front_matter)
# Extract book configuration if it exists
if isinstance(metadata, dict) and 'book' in metadata:
book_config = metadata['book']
# Map book config to expected format
config = {
'title': book_config.get('title', metadata.get('title', 'Book')),
'author': book_config.get('author', 'Author'),
'date': str(book_config.get('date', metadata.get('date', '2024'))).split('T')[0],
'description': book_config.get('description', metadata.get('description', '')),
'language': book_config.get('language', 'zh-hans'),
'cover': book_config.get('cover', None),
'website': book_config.get('website', ''),
'appendix': book_config.get('appendix', False),
'subject': book_config.get('subject', book_config.get('description', metadata.get('description', ''))),
'keywords': book_config.get('keywords', ''),
'creator': book_config.get('creator', 'LaTeX with hyperref'),
'producer': book_config.get('producer', 'XeLaTeX'),
# Enhanced cover configuration
'cover_config': {
'image': book_config.get('cover', None),
'title_text': book_config.get('cover_title_text', book_config.get('title', metadata.get('title', 'Book'))),
'author_text': book_config.get('cover_author_text', book_config.get('author', 'Author')),
'subtitle_text': book_config.get('cover_subtitle_text', ''),
'title_color': book_config.get('cover_title_color', '#000000'),
'author_color': book_config.get('cover_author_color', '#333333'),
'subtitle_color': book_config.get('cover_subtitle_color', '#666666'),
'title_font_size': book_config.get('cover_title_font_size', 48),
'author_font_size': book_config.get('cover_author_font_size', 24),
'subtitle_font_size': book_config.get('cover_subtitle_font_size', 18),
'title_position': book_config.get('cover_title_position', 'center'),
'author_position': book_config.get('cover_author_position', 'bottom'),
'overlay_enabled': book_config.get('cover_overlay_enabled', True),
'text_shadow': book_config.get('cover_text_shadow', True),
'background_overlay': book_config.get('cover_background_overlay', False),
'overlay_opacity': book_config.get('cover_overlay_opacity', 0.7)
},
# Back-cover configuration (enhanced with text, QR code, and link)
'backcover_image': book_config.get('backcover_image', None),
'backcover_text': book_config.get('backcover_text', None),
'qrcode_image': book_config.get('qrcode_image', None),
'backcover_link_text': book_config.get('backcover_link_text', None),
'backcover_link_url': book_config.get('backcover_link_url', None),
# Back-cover styling options
'backcover_text_color': book_config.get('backcover_text_color', '#000000'),
'backcover_link_color': book_config.get('backcover_link_color', '#0066CC'),
'backcover_text_font_size': book_config.get('backcover_text_font_size', 16),
'backcover_link_font_size': book_config.get('backcover_link_font_size', 14),
'qrcode_size': book_config.get('qrcode_size', '0.15\\paperwidth'),
'backcover_top_margin': book_config.get('backcover_top_margin', '0.2\\textheight'),
'backcover_bottom_margin': book_config.get('backcover_bottom_margin', '0.2\\textheight'),
'backcover_spacing_1': book_config.get('backcover_spacing_1', '1.5cm'),
'backcover_spacing_2': book_config.get('backcover_spacing_2', '1cm'),
# Typography and styling configuration
'typography': {
'body_color': book_config.get('body_color', '#000000'),
'heading_color': book_config.get('heading_color', '#000000'),
'link_color': book_config.get('link_color', '#0066cc'),
'code_color': book_config.get('code_color', '#d14'),
'quote_color': book_config.get('quote_color', '#666666'),
'caption_color': book_config.get('caption_color', '#666666')
}
}
else:
# Fallback to using main front matter
config = {
'title': metadata.get('title', 'Book'),
'author': metadata.get('author', 'Author'),
'date': str(metadata.get('date', '2024')).split('T')[0] if metadata.get('date') else '2024',
'description': metadata.get('description', ''),
'language': 'zh-hans',
'cover_config': {
'overlay_enabled': True,
'title_color': '#000000',
'author_color': '#333333'
},
'typography': {
'body_color': '#000000',
'heading_color': '#000000'
}
}
except Exception as e:
print(f"Warning: Failed to parse front matter: {e}")
config = {
'title': 'Book',
'author': 'Author',
'date': '2024',
'language': 'zh-hans',
'cover_config': {'overlay_enabled': True},
'typography': {'body_color': '#000000'}
}
# Print config for debugging
print(f"Loaded book configuration with backcover: {config.get('backcover_image', 'None')}")
return config