-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
94 lines (82 loc) · 3.93 KB
/
config.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
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
from latex import LatexCommand, LatexDoc, LatexLengthOption
import yaml
class Config:
def __init__(self, config_file):
with open(config_file, 'r') as file:
try:
self.config = yaml.safe_load(file)
except yaml.YAMLError as err:
print(err)
self.floats = []
if 'floats' in self.config:
for floatOpt in self.config['floats']:
for opt, val in floatOpt.items():
if len(val) > 0 and val != 'default':
self.floats += [LatexLengthOption(opt, val)]
self.compact_items = False
self.compact_enums = False
if 'lists' in self.config:
list_config = self.config['lists']
if 'compact-items' in list_config:
self.compact_items = list_config['compact-items']
if 'compact-enums' in list_config:
self.compact_enums = list_config['compact-enums']
self.headings = []
if 'headings' in self.config:
heads_config = self.config['headings']
for head in heads_config:
head_config = heads_config[head]
left = head_config['left'] or "0pt"
above = head_config['above'] or "0pt"
below = head_config['below'] or "0pt"
self.headings += [LatexCommand('titlespacing', None,
[f'\\{head}', left, above, below])]
self.paragraphs = []
if 'paragraphs' in self.config:
for opt, val in self.config['paragraphs'].items():
if len(val) > 0 and val != 'default':
self.paragraphs += [LatexLengthOption(opt, val)]
self.equations = []
if 'equations' in self.config:
for opt, val in self.config['equations'].items():
if len(val) > 0 and val != 'default':
self.equations += [LatexLengthOption(opt, val)]
spread_factor = 1.0
if 'line-spread' in self.config:
spread_factor = self.config['line-spread']
self.line_spread = [LatexCommand('linespread', [], str(spread_factor))]
self.caption_opts = []
if 'captions' in self.config:
if 'above-skip' in self.config['captions']:
aboveskip = self.config['captions']['above-skip']
if aboveskip != 'default':
self.caption_opts += [f'above-skip={aboveskip}']
if 'below-skip' in self.config['captions']:
belowskip = self.config['captions']['below-skip']
if belowskip != 'default':
self.caption_opts += [f'below-skip={belowskip}']
if 'skip' in self.config['captions']:
skip = self.config['captions']['skip']
if skip != 'default':
self.caption_opts += [f'skip={skip}']
if 'font' in self.config['captions']:
font = self.config['captions']['font']
if font != 'default':
self.caption_opts += [f'font={font}']
def apply_to(self, doc):
if self.compact_items or self.compact_enums:
doc.add_package('paralist')
if self.compact_items:
doc.replace_in_body('\\begin{itemize}', '\\begin{compactitem}')
doc.replace_in_body('\\end{itemize}', '\\end{compactitem}')
if self.compact_items:
doc.replace_in_body('\\begin{enumerate}', '\\begin{compactenum}')
doc.replace_in_body('\\end{enumerate}', '\\end{compactenum}')
if len(self.headings) > 0:
doc.add_package('titlesec', 'compact')
if len(self.caption_opts) > 0:
doc.add_package('caption', ','.join(self.caption_opts))
header_opts = self.floats + self.equations + self.paragraphs + self.headings + self.line_spread
for opt in header_opts:
doc.add_header_opt(opt)
pass