-
Notifications
You must be signed in to change notification settings - Fork 35
/
tasks.py
338 lines (273 loc) · 10.1 KB
/
tasks.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# -*- coding: utf-8 -*-
'''
Build script for Science.md
Install Invoke using pip or pacman package manager, then run from cli:
jf@mymachine ~$ invoke all
author: JF
'''
# standard library modules
import os
import shutil
import tempfile
# third party modules
from invoke import task
import regex as re
from colorama import Fore, Back, init
# initialize colorama
init(autoreset=True)
# set filename
file_name = os.path.basename(os.path.abspath('.'))
# set document content, watch out: order matters
content = [
'title.md',
'abstract.md',
'introduction.md',
'methods.md',
'results.md',
'conclusion.md',
'appendix.md',
'acknowledgements.md',
'bib.md',
]
# emulate sed inplace edit using regular expresions
def sed_i(re_a: str, re_b: str, file_name: str):
# create a temporary file to store data, as file can be big
with tempfile.TemporaryFile(mode='w+', encoding='utf-8') as tmp:
with open(file_name, 'r') as fi:
for line in fi:
tmp.write(re.sub(re_a, re_b, line, flags=re.VERSION1))
# rewind
tmp.seek(0)
# write output on the same file
with open(file_name, 'w') as fo:
fo.writelines(tmp.readlines())
def wc_w(file_name: str) -> int:
''' FROM HOWTO: \\S Matches any non-whitespace character;
this is equivalent to the class [^ \\t\\n\\r\\f\\v].
Not very accurate, but this gives the same result as wc -w
'''
# use iterator to avoid using a big list in case of large files
n_words = sum(1 for _ in re.finditer(r'\S+', open(file_name).read(), flags=re.VERSION1))
# n_words = len(re.findall(r'\S+', open(file_name).read()))
return n_words
def human_readable(bsize: int) -> str:
"""This function will convert bytes to MB.... GB... etc"""
for unit in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if bsize < 1024:
bsize_str = f"{bsize:3.1f} {unit}"
break
bsize >>= 10
return bsize_str
# emulate du -bh functionality
def du_bh(file_name: str) -> str:
size = os.stat(file_name).st_size
return f'{human_readable(size)} {file_name}'
def confirm(prompt='Confirm', res=False):
"""Prompts confirmation from the user, returns True for yes.
The default value assumed when user types ENTER.
"""
prompt = f'{prompt} [Y/n] ' if res else f'{prompt} [y/N] '
while True:
ans = input(prompt).lower()
if not ans:
return res
if ans not in ['y', 'yes', 'n', 'no']:
print('please enter y(es) or n(o)!')
continue
return (ans == 'y')
def rm_i(file_name: str):
if confirm(f'Do you really want to delete {file_name}?'):
os.remove(file_name)
@task
def merge(c):
'''Meges all content file into one big file for later processing.'''
# create a text list with all content pages
# concatenate file contents, (it can be done with python later)
print(Fore.LIGHTMAGENTA_EX + f'cat content/*.md > release/{file_name}.md')
# read content files from content folder and write it
# into a one big file inside release folder
dst = os.path.join('release', f'{file_name}.md')
with open(dst, 'w') as fo:
for md_file in content:
src = os.path.join('content', md_file)
with open(src, 'r') as fi:
fo.writelines(fi.readlines())
# file space usage estimation
size = du_bh(f'release/{file_name}.md')
print(Fore.LIGHTGREEN_EX + f'> {size}')
# word count
n = wc_w(f'release/{file_name}.md')
print(f'i Less than {n} words')
# res = c.run(f'wc -w < release/{file_name}.md' , hide='both')
# print(f'i Less than {res.stdout.rstrip()} words')
@task(merge)
def tex(c):
'''From Markdown to LaTeX using pandoc'''
# create a temporary file
shutil.copyfile(f'release/{file_name}.md', f'release/{file_name}.temp.md')
# work on comments
print(Fore.LIGHTMAGENTA_EX + f'| sed s/==XX comment==/*\\XX comment*/ release/{file_name}.md')
# First change TODO tags
sed_i(r'==TODO==', r'\\TODO', f'release/{file_name}.temp.md')
# Then change all other tags
sed_i(r'==([a-zA-Z]+) ([^=]+)==', r'*\\\1 \2*', f'release/{file_name}.temp.md')
# change extension to .pdf in image strings
print(Fore.LIGHTMAGENTA_EX + f'| sed s/.png/.pdf/ release/{file_name}.md')
sed_i(r'.png', r'.pdf', f'release/{file_name}.temp.md')
# pandoc options, modify parameters here!
pandoc_opts = [
'--wrap=preserve',
'-s',
'--filter pandoc-crossref',
'--filter=pandoc-citeproc',
'-f markdown',
'-V colorlinks',
'-V papersize=a4',
'-V geometry=margin=1in',
'--number-sections',
'-M secPrefix=section',
'-M tblPrefix=Table',
'--template templates/pandoc.tex',
]
# run pandoc
print(Fore.LIGHTMAGENTA_EX + f'| pandoc release/{file_name}.md -o release/{file_name}.tex')
os.chdir('release')
c.run(f'pandoc {" ".join(pandoc_opts)} {file_name}.temp.md -o {file_name}.tex')
os.chdir('..')
# file space usage estimation
size = du_bh(f'release/{file_name}.tex')
print(Fore.LIGHTGREEN_EX + f'> {size}')
# delete auxiliary files
os.remove(f'release/{file_name}.temp.md')
@task(merge)
def pdf(c):
'''From Markdown to PDF using pandoc'''
# create a temporary file
shutil.copyfile(f'release/{file_name}.md', f'release/{file_name}.temp.md')
# work on comments
print(Fore.LIGHTMAGENTA_EX + f'| sed s/==XX comment==/*\\XX comment*/ release/{file_name}.md')
# First change TODO tags
sed_i(r'==TODO==', r'\\TODO', f'release/{file_name}.temp.md')
# Then change all other tags
sed_i(r'==([a-zA-Z]+) ([^=]+)==', r'*\\\1 \2*', f'release/{file_name}.temp.md')
# change extension to .pdf in image strings
print(Fore.LIGHTMAGENTA_EX + f'| sed s/.png/.pdf/ release/{file_name}.md')
sed_i(r'.png', r'.pdf', f'release/{file_name}.temp.md')
# pandoc options, modify parameters here!
pandoc_opts = [
'--wrap=preserve',
'-s',
'--filter pandoc-crossref',
'--filter=pandoc-citeproc',
'-f markdown',
'-V colorlinks',
'-V papersize=a4',
'-V geometry=margin=1in',
'--number-sections',
'-M secPrefix=section',
'-M tblPrefix=Table',
'--template templates/pandoc.tex',
'--csl templates/copernicus.csl',
]
# run pandoc
print(Fore.LIGHTMAGENTA_EX + f'| pandoc release/{file_name}.md -o release/{file_name}.pdf')
os.chdir('release')
c.run(f'pandoc {" ".join(pandoc_opts)} {file_name}.temp.md -o {file_name}.pdf')
os.chdir('..')
# file space usage estimation
size = du_bh(f'release/{file_name}.pdf')
print(Fore.LIGHTGREEN_EX + f'> {size}')
# delete auxiliary files
os.remove(f'release/{file_name}.temp.md')
@task(merge)
def docx(c):
'''From Markdown to Word using pandoc'''
# create a temporary file
shutil.copyfile(f'release/{file_name}.md', f'release/{file_name}.temp.md')
# work on comments
print(Fore.LIGHTMAGENTA_EX + f'| sed s/==XX comment==/*\\XX comment*/ release/{file_name}.md')
# First change TODO tags
sed_i(r'==TODO==', r'<span custom-style="TODO"> TODO </span>', f'release/{file_name}.temp.md')
# Then change all other tags
sed_i(
r'==([a-zA-Z]+) ([^=]+)==',
r'<span custom-style="comment-name"> \1 </span><span custom-style="comment"> \2</span>',
f'release/{file_name}.temp.md'
)
# pandoc options, modify parameters here!
pandoc_opts = [
'--wrap=preserve',
'-s',
'--filter pandoc-crossref',
'--filter=pandoc-citeproc',
'-f markdown',
'--number-sections',
'-M secPrefix=section',
'-M numberSections=true',
'-M tblPrefix=Table',
'--reference-doc=templates/reference.docx',
]
# run pandoc
print(Fore.LIGHTMAGENTA_EX + f'| pandoc release/{file_name}.md -o release/{file_name}.docx')
os.chdir('release')
c.run(f'pandoc {" ".join(pandoc_opts)} {file_name}.temp.md -o {file_name}.docx')
os.chdir('..')
# file space usage estimation
size = du_bh(f'release/{file_name}.docx')
print(Fore.LIGHTGREEN_EX + f'> {size}')
# delete auxiliary files
os.remove(f'release/{file_name}.temp.md')
@task(merge)
def html(c):
'''From Markdown to HTML using pandoc'''
# create temporary file
shutil.copyfile(f'release/{file_name}.md', f'release/{file_name}.temp.md')
# change comment strings
print(Fore.LIGHTMAGENTA_EX + f'| sed s/==XX comment==/[XX] comment/ release/{file_name}.md')
# First change TODO tags
sed_i(r'==TODO==', r'<span class="todo">TODO</span>', f'release/{file_name}.temp.md')
# Then change all other tags
sed_i(
r'==([a-zA-Z]+) ([^=]+)==',
r'<span class="comment \1"><b>\1</b> \2</span>',
f'release/{file_name}.temp.md'
)
# pandoc options, modify parameters here!
pandoc_opts = [
'--wrap=preserve',
'-s',
'--filter pandoc-crossref',
'--filter pandoc-citeproc',
'-f markdown',
'--template templates/pandoc.html',
'-t html5',
'--mathjax',
'--number-sections',
'-M secPrefix=section',
'-M tblPrefix=Table',
]
# run pandoc
print(Fore.LIGHTMAGENTA_EX + f'| pandoc release/{file_name}.md -o release/{file_name}.html')
os.chdir('release')
c.run(f'pandoc {" ".join(pandoc_opts)} {file_name}.temp.md -o {file_name}.html')
os.chdir('..')
# file space usage estimation
size = du_bh(f'release/{file_name}.html')
print(Fore.LIGHTGREEN_EX + f'> {size}')
# delete auxiliary files
os.remove(f'release/{file_name}.temp.md')
@task(pre=[merge, html, docx, tex, pdf])
def all(c):
'''From Markdown to Everything'''
pass
@task
def clean(c):
'''Clean `release/` folder from garbage. Remove `-i` flag if you know what you do.'''
os.chdir('release')
with os.scandir('.') as it:
for entry in it:
if not entry.name.startswith('.') and entry.is_file():
if entry.name.startswith(file_name):
rm_i(entry.name)
os.chdir('..')
# c.run(f'rm -i release/{file_name}.*')