-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuildPDF.py
More file actions
244 lines (200 loc) · 8.06 KB
/
Copy pathbuildPDF.py
File metadata and controls
244 lines (200 loc) · 8.06 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import os
import re
import shutil
import subprocess
import sys
from typing import List, Tuple, Match, Optional
def run_sphinx_build() -> bool:
"""Run sphinx-build to generate LaTeX files."""
print("Running sphinx-build...")
os.environ['SPHINX_BUILDER'] = 'latex'
result = subprocess.run(['sphinx-build', '--jobs', 'auto', '-b', 'latex', '.', '_build/en'], capture_output=True, text=True)
if result.returncode != 0:
print(f"Error running sphinx-build: {result.stderr}")
return False
print("Sphinx build complete.")
return True
def read_tex_file(tex_file: str) -> Optional[str]:
"""Read a TeX file and return its contents as a string."""
if not os.path.exists(tex_file):
print(f"Error: {tex_file} does not exist.")
return None
try:
with open(tex_file, 'r', encoding='utf-8') as file:
return file.read()
except Exception as e:
print(f"Error reading {tex_file}: {e}")
return None
def write_tex_file(tex_file: str, content: str) -> bool:
"""Write content to a TeX file."""
try:
with open(tex_file, 'w', encoding='utf-8') as file:
file.write(content)
return True
except Exception as e:
print(f"Error writing to {tex_file}: {e}")
return False
def remove_svg_from_tex(content: str) -> str:
"""Remove SVG image references from the content."""
lines = content.splitlines()
filtered_lines = [line for line in lines
if not (r'\sphinxincludegraphics' in line and '.svg' in line)]
print("SVG image references removed.")
return '\n'.join(filtered_lines)
def convert_tabulary_to_longtable(content: str) -> str:
"""Convert tabulary tables to longtable format."""
new_content, count = replace_tabulary_tables(content)
if count == 0:
print("No tabulary tables found.")
else:
print(f"Converted {count} tabulary table(s) to longtable.")
return new_content
def replace_tabulary_tables(tex_content: str) -> Tuple[str, int]:
"""Identifies and replaces tabulary tables in the LaTeX content."""
table_pattern = re.compile(
r'\\begin\{tabulary\}\{.*?\}\[.*?\]\{(.*?)\}(.*?)\\end\{tabulary\}',
re.DOTALL
)
def table_replacement(match: Match[str]) -> str:
col_spec = match.group(1)
table_body = match.group(2)
col_spec = map_column_spec(col_spec)
rows = parse_table_body(table_body)
return encode_longtable(rows, col_spec)
return table_pattern.subn(table_replacement, tex_content)
def map_column_spec(tab_spec: str) -> str:
"""Maps the column specification for tabulary to longtable."""
page_width = 8.5 # Example: standard US Letter width in inches
margin_left_right = 1 # Example: 1-inch margin on both sides
usable_width = page_width - 2 * margin_left_right
num_columns = sum(1 for col in tab_spec if col == 'T')
if num_columns > 0:
base_column_width = usable_width / num_columns
# Linear adjustment factor per column to account for borders/spacing
factor = 0.0045
adjusted_width = base_column_width - (num_columns * factor)
adjusted_width = max(0.25, adjusted_width)
column_width_str = f"{adjusted_width:.2f}in"
else:
column_width_str = '4in'
return '|' + '|'.join(f'p{{{column_width_str}}}' if col == 'T' else col for col in tab_spec) + '|'
def parse_table_body(table_body: str) -> List[List[str]]:
"""Parses the table body into rows and cells."""
rows: List[List[str]] = []
current_row_lines: List[str] = []
for line in table_body.splitlines():
stripped_line = line.strip()
if not stripped_line:
continue
current_row_lines.append(stripped_line)
if stripped_line.endswith(r'\\'): # End of row
full_row = ' '.join(current_row_lines)[:-2].strip()
full_row = clean_sphinx_commands(full_row)
cells = [cell.strip() for cell in re.split(r'(?<!\\)&', full_row)]
rows.append(cells)
current_row_lines = []
return rows
def clean_sphinx_commands(text: str) -> str:
"""Replace Sphinx commands with LaTeX equivalents."""
replacements = {
r'\sphinxstylestrong': r'\textbf',
r'\sphinxAtStartPar': '',
r'\sphinxmidrule': r'\hline',
r'\sphinxtoprule': r'\hline',
r'\sphinxhline': r'\hline'
}
for old, new in replacements.items():
text = text.replace(old, new)
return text
def encode_longtable(rows: List[List[str]], col_spec: str = 'll') -> str:
"""Create longtable LaTeX code from rows and column specification."""
header = "\\begin{longtable}{" + col_spec + "}"
body = "\n".join(
" & ".join(row) + r"\\"
for row in rows
)
footer = "\\end{longtable}"
return "\n".join([header, body, footer])
emoji_ranges: List[Tuple[int, int]] = [
(0x1F300, 0x1F5FF),
(0x1F600, 0x1F64F),
(0x1F680, 0x1F6FF),
(0x1F700, 0x1F77F),
(0x1F780, 0x1F7FF),
(0x1F900, 0x1F9FF),
(0x1FA00, 0x1FA6F),
(0x1FA70, 0x1FAFF),
]
symbol_ranges: List[Tuple[int, int]] = [
(0x1F800, 0x1F8FF),
]
def is_emoji(char: str) -> bool:
"""Check if a character is an emoji."""
codepoint = ord(char)
return any(start <= codepoint <= end for start, end in emoji_ranges)
def is_symbol(char: str) -> bool:
"""Check if a character is a symbol that needs special handling."""
codepoint = ord(char)
return any(start <= codepoint <= end for start, end in symbol_ranges)
def wrap_special_chars_in_tex(content: str) -> str:
"""Wrap emoji and symbol characters with appropriate LaTeX commands."""
new_content: List[str] = []
emoji_count: int = 0
symbol_count: int = 0
for char in content:
if is_emoji(char):
new_content.append(f"\\emoji{{{char}}}")
emoji_count += 1
elif is_symbol(char):
new_content.append(f"\\symbolchar{{{char}}}")
symbol_count += 1
else:
new_content.append(char)
print(f"Wrapped {emoji_count} emoji(s) with \\emoji{{}} and {symbol_count} symbol(s) with \\symbolchar{{}}.")
return ''.join(new_content)
def run_latexmk(tex_file: str) -> bool:
"""Run latexmk to build the PDF."""
build_dir = os.path.dirname(tex_file)
tex_filename = os.path.basename(tex_file)
print("Running latexmk...")
try:
subprocess.run(["latexmk", "-silent", "-lualatex", tex_filename], check=True, cwd=build_dir)
except subprocess.CalledProcessError as e:
print(f"Error running latexmk: {e}")
log_path = os.path.join(build_dir, 'emeditor.log')
if os.path.exists(log_path):
with open(log_path, 'r') as log_file:
print("emeditor.log contents:")
print(log_file.read())
else:
print(f"Log file not found: {log_path}")
return False
print("PDF build complete.")
return True
def main() -> None:
# Clean build directory
build_folder = '_build'
if os.path.exists(build_folder):
shutil.rmtree(build_folder)
print(f"Removed directory: {build_folder}")
# Run sphinx-build to generate LaTeX files
if not run_sphinx_build():
sys.exit(1)
tex_file = '_build/en/emeditor.tex'
# Read the TeX file
content = read_tex_file(tex_file)
if content is None:
sys.exit(1)
# Apply transformations to the content
content = remove_svg_from_tex(content)
content = convert_tabulary_to_longtable(content)
content = wrap_special_chars_in_tex(content)
# Write the modified content back to the file
if not write_tex_file(tex_file, content):
sys.exit(1)
# Run latexmk to build the PDF
if not run_latexmk(tex_file):
sys.exit(1)
print("All steps completed successfully.")
if __name__ == '__main__':
main()