2
2
import os
3
3
import sys
4
4
import subprocess
5
+ import tempfile
5
6
import multiprocessing
6
7
from pathlib import Path
7
8
9
+ """
10
+ Keys for content information in title page file.
11
+ If no key is specified on a line in the file,
12
+ the first item in this list will be used as the key.
13
+ """
14
+ TITLE_KEYS = ["COPYRIGHT" , "TITLE" , "PRESENTER" , "DATE" , "OTHER" ]
15
+
16
+ TITLE_FILE_NAME = None
17
+
8
18
9
19
def windows ():
10
20
return sys .platform .startswith ("win" )
@@ -23,6 +33,135 @@ def fix_file_case(filename):
23
33
return filename
24
34
25
35
36
+ def title_parse (line ):
37
+ key = "COPYRIGHT"
38
+ pieces = line .split (":" , 1 )
39
+ if pieces [0 ] in TITLE_KEYS :
40
+ return pieces [0 ], pieces [1 ].strip ()
41
+ else :
42
+ return TITLE_KEYS [0 ], line .strip ()
43
+
44
+
45
+ def default_title (source ):
46
+ """
47
+ If not title is specified, use the folder name to determine title
48
+ """
49
+
50
+ title = os .path .basename (os .path .dirname (os .path .abspath (source )))
51
+ pieces = title .split ("_" )
52
+ title = ""
53
+ for piece in pieces :
54
+ title = title + piece [0 ].upper () + piece [1 :] + " "
55
+ return title .strip ()
56
+
57
+
58
+ def parse_title_file (args ):
59
+ title_source = args .title .strip ()
60
+
61
+ retval = {}
62
+ for key in TITLE_KEYS :
63
+ if key == "COPYRIGHT" :
64
+ retval [key ] = []
65
+ else :
66
+ retval [key ] = ""
67
+
68
+ try :
69
+ fp = None
70
+ if os .path .exists (title_source ):
71
+ fp = open (title_source , "r" )
72
+ else :
73
+ fp = open (os .path .join (os .path .dirname (__file__ ), "title_page.txt" ), "r" )
74
+ for line in fp :
75
+ key , content = title_parse (line .strip ())
76
+ if len (content ) > 0 or key == "COPYRIGHT" :
77
+ if key == "COPYRIGHT" :
78
+ retval [key ].append (content )
79
+ else :
80
+ retval [key ] = content
81
+
82
+ if len (retval ["TITLE" ]) == 0 :
83
+ retval ["TITLE" ] = default_title (args .source [0 ])
84
+
85
+ except :
86
+ pass
87
+
88
+ return retval
89
+
90
+
91
+ def print_content (tfp , title_info , prefix = "" ):
92
+ if len (title_info ) > 0 :
93
+ if title_info is list :
94
+ for item in title_info :
95
+ tfp .write (
96
+ " \\ small{\\ textit{" + prefix + "} \\ textbf{" + item + "}}\n \n "
97
+ )
98
+ else :
99
+ tfp .write (
100
+ " \\ small{\\ textit{" + prefix + "} \\ textbf{" + title_info + "}}\n \n "
101
+ )
102
+
103
+
104
+ def prepare_title_file (args ):
105
+ """
106
+ If the "source" command line argument is only one TXT file, then we
107
+ assume we are producing a slide deck (anything else is just trying
108
+ to build from individual RST files).
109
+ In that case, we want to build a title slide for the deck,
110
+ consisting of the title for the course, possibly the presenter, date
111
+ and/or other information, and then copyright information.
112
+ This information will come from the "title_page.txt" file in the
113
+ "pandoc" folder OR from the "--title" option from the command line.
114
+ """
115
+
116
+ global TITLE_FILE_NAME
117
+
118
+ TITLE_FILE_NAME = None
119
+ if len (args .source ) == 1 and args .source [0 ].lower ().endswith (".txt" ):
120
+ tmp = tempfile .NamedTemporaryFile (delete = False )
121
+ TITLE_FILE_NAME = tmp .name + ".rst"
122
+ title_info = parse_title_file (args )
123
+
124
+ with open (TITLE_FILE_NAME , "w" ) as tfp :
125
+
126
+ """
127
+ This is the actual title page for the beamer slide deck
128
+ """
129
+ separator = "#" * (2 + len (title_info ["TITLE" ]))
130
+ tfp .write (separator + "\n " )
131
+ tfp .write (title_info ["TITLE" ] + "\n " )
132
+ tfp .write (separator + "\n \n " )
133
+
134
+ """
135
+ This is the copyright page (which will include any other user-specified
136
+ information) that appears after the document title page and before
137
+ the document actually begins.
138
+ """
139
+
140
+ tfp .write (".. raw:: latex\n \n " )
141
+ tfp .write (" \\ begin{center}\n " )
142
+
143
+ tfp .write (" \\ colorbox{adacore3}\n " )
144
+ tfp .write (
145
+ " {\\ color{adacore1}{\\ huge{\\ textit{\\ textbf{"
146
+ + title_info ["TITLE" ]
147
+ + "}}}}}\n \n "
148
+ )
149
+
150
+ print_content (tfp , title_info ["PRESENTER" ], prefix = "Presented by" )
151
+ print_content (tfp , title_info ["DATE" ])
152
+ print_content (tfp , title_info ["OTHER" ])
153
+
154
+ tfp .write (" \\ end{center}\n \n " )
155
+
156
+ tfp .write (" \\ vspace{3cm}\n \n " )
157
+
158
+ if len (title_info ["COPYRIGHT" ]) > 0 :
159
+ tfp .write (" \\ tiny{\n " )
160
+ for line in title_info ["COPYRIGHT" ]:
161
+ tfp .write (" " + line + "\n " )
162
+ tfp .write (" }\n " )
163
+
164
+
26
165
def to_texinputs_path (path ):
27
166
abspath = fix_file_case (os .path .abspath (path ))
28
167
if path .endswith ("//" ):
@@ -182,13 +321,17 @@ def parse_rst_list_file(dirname, f):
182
321
183
322
184
323
def expand_source (source_file ):
324
+ global TITLE_FILE_NAME
325
+
185
326
if source_file .lower ().endswith (".rst" ):
186
327
return [os .path .abspath (source_file )]
187
328
else :
188
329
dirname = os .path .dirname (source_file )
189
330
# Read lines from source file
190
331
with open (source_file ) as f :
191
332
files = parse_rst_list_file (dirname , f )
333
+ if TITLE_FILE_NAME != None :
334
+ files .insert (0 , TITLE_FILE_NAME )
192
335
return files
193
336
194
337
@@ -212,11 +355,7 @@ def pandoc_prepare_run_single(n, source_or_source_list, args):
212
355
if len (color ) > 0 :
213
356
color = " -V colortheme=" + color
214
357
215
- pandoc_title_arg = args .title
216
- if args .title :
217
- pandoc_title_arg = ' -V title="' + args .title .replace ("_" , " " ) + '"'
218
-
219
- input_file = args .title or source_or_source_list
358
+ input_file = source_or_source_list
220
359
221
360
extension = args .extension
222
361
if extension is None :
@@ -258,7 +397,6 @@ def pandoc_prepare_run_single(n, source_or_source_list, args):
258
397
"--resource-path" ,
259
398
texinputs ,
260
399
filter ,
261
- pandoc_title_arg ,
262
400
theme ,
263
401
color ,
264
402
"--fail-if-warnings" ,
@@ -301,6 +439,13 @@ def run_pandoc(args):
301
439
print (f"[end ] { task_name } " )
302
440
303
441
442
+ def delete_temp_file ():
443
+ global TITLE_FILE_NAME
444
+ if TITLE_FILE_NAME != None and os .path .exists (TITLE_FILE_NAME ):
445
+ os .remove (TITLE_FILE_NAME )
446
+ # print (TITLE_FILE_NAME)
447
+
448
+
304
449
if __name__ == "__main__" :
305
450
PANDOC = Path (sys .argv [0 ]).resolve ().parent
306
451
ROOT = PANDOC .parent
@@ -371,10 +516,14 @@ def run_pandoc(args):
371
516
372
517
args = parser .parse_args ()
373
518
519
+ prepare_title_file (args )
520
+
374
521
pandoc_prepared = pandoc_prepare_run (args )
375
522
376
523
try :
377
524
with multiprocessing .Pool (None if args .jobs == 0 else args .jobs ) as p :
378
525
p .map (run_pandoc , pandoc_prepared )
526
+ delete_temp_file ()
379
527
except subprocess .CalledProcessError as e :
528
+ delete_temp_file ()
380
529
sys .exit (2 )
0 commit comments