-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippetbuilder.py
73 lines (61 loc) · 2.19 KB
/
snippetbuilder.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Questo script è utilizzato creare automaticamente i file snippets corrispondenti
ai blocchi di codice della guida in modo che possano essere visualizzati
cliccando sul pulsantino di fianco al titolo
"""
from __future__ import print_function
import io,os
import shutil
TARGET_DIR = "snippets"
# Reset the output folder
if os.path.isdir(TARGET_DIR):
for the_file in os.listdir(TARGET_DIR):
file_path = os.path.join(TARGET_DIR, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception as e:
print(e)
else:
os.makedirs(TARGET_DIR)
def save_file(current_lines, chapter_count, section_count, subsect_count):
if len(current_lines) == 0:
current_lines.append("Il paragrafo non conteneva codice.")
# Save the current code
filename=TARGET_DIR+os.sep+str(chapter_count)+"."+str(section_count)+"."+str(subsect_count)+".txt"
with open(filename, 'w') as output_file:
for current_line in current_lines:
output_file.write(current_line)
with io.open('guida.tex', mode='r', encoding="utf-8") as tex_file:
current_lines = []
chapter_count = 0
section_count = 0
subsect_count = 0
inside_code = False
for line in tex_file.readlines():
line = line.replace("\r","")
if inside_code and not line.strip().startswith("\\end{lstlisting}"):
current_lines.append(line)
if line.startswith("\\chapter"):
save_file(current_lines, chapter_count, section_count, subsect_count)
current_lines = []
chapter_count+=1
section_count=0
subsect_count=0
elif line.startswith("\\section"):
save_file(current_lines, chapter_count, section_count, subsect_count)
current_lines = []
section_count+=1
subsect_count=0
elif line.startswith("\\subsection"):
save_file(current_lines, chapter_count, section_count, subsect_count)
current_lines = []
subsect_count+=1
elif line.strip().startswith("\\begin{lstlisting}"):
inside_code = True
current_lines.append("########################### CODE ###########################\n")
elif line.strip().startswith("\\end{lstlisting}"):
inside_code = False
current_lines.append("############################################################\n")