summaryrefslogtreecommitdiff
path: root/support/textoolspro/sectioner.py
blob: 2aa4f210c32150b08bd9cf8ab0abbf8a3c149f1e (plain)
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
"""
1999 Manuel Gutierrez Algaba
You are free to use , modify, distribute and copy this piece of
python code, if you keep this free copyright notice in it.

sectioner.py Version 1.0

relative sectioning of code: 
Given a source in LaTeX, it generates the \sections, \subsections,
..., in an automatic way.
It abstracts the idea of absolute sectioning of LaTeX , and supplies
relative positioning. It makes document more independent of their
type. The decission article-report-miarticle can be done in the end
, not when you start to write the document.
Possibly you can use it ( adapting it a bit ) in TeX, or another
sorts of languages of typesseting.

how to use:
python  sectioner.py method inputfile outputfile
"""

"""
You use a command in a single line, and in the next line you
put the title of that (sub)section or whatever. And it should
be in the first column of the line.

For example:

\n
Coding Decissions.
<Body of this part>
...

It uses the following commands:

\n : This says the next section is at the same current level.
\+ : This goes a level down
\- : A level up
\+4 ( or any other number ) : 4 levels down
\-4: 4 levels up
\minput{name of file} : This includes another file of sectioner-LaTeX
style.

For more details see the example:
highnest.tex ( a source of sectioner-LaTeX )

"""

import sys
import re
import string

print sys.argv
if not sys.argv[1:] or not sys.argv[2:]  or not sys.argv[3:]:
    print "usage: python  sectioner.py method inputfile outputfile"
    print "method can be 'manolo' or it can be article or report"
    print "And you should include all the parameters"
    sys.exit(2)

method = sys.argv[1]
inputfile = sys.argv[2] 
outputfile = sys.argv[3] 

i = open(inputfile,"r")
lines_of_the_main_file =i.readlines()
i.close()

outputfile = open(outputfile,"w")


# This counters what section we are in
counters = [ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
dic_sections_manolo = { 0:'\\part', 1:'\\section', 2:'\\subsection',
		  3:'\\subsubsection', 4:'\\sssection', 5:'\\ssssection',
		  6:'\\Section', 7:'\\Ssection', 8:'\\Sssection',
		  9:'\\Ssssection', 10: '\\Sssssection',
		  11:'\\SSection', 12:'\\SSsection', 13:'\\SSssection',
		  14:'\\SSsssection'}

dic_sections_article = { 0:'\\section', 1:'\\subsection',
		  2:'\\subsubsection', 3:'\\paragraph', 4:'\\subparagraph'}

dic_sections_report = { 0:'\\chapter', 1:'\\section', 2:'\\subsection',
		  3:'\\subsubsection', 4:'\\paragraph', 5:'\\subparagraph'}

if method=='manolo':
    dic_sections = dic_sections_manolo
elif method =='article':
    dic_sections = dic_sections_article
elif method =='report':
    dic_sections = dic_sections_report
		  
level = 0
next = 0

#print y[240]=="\\n\n"
#print y[274]=="\\+\n"

def process_file(file):
    i = open(file,"r")
    v =i.readlines()
    i.close()
    process_lines_in_file(v)
    del v

minputfile= re.compile("/minput\{(?P<file>[^\}]+)\}.*")

def process_lines_in_file(lines_in_file):
    next = 0
    global level
    for i in lines_in_file:
	if i[0]=="\\"  and i[1] in "+-" and i[2] in "123456789":
	    signo = i[1]
	    if signo == '+':
		level = level + eval(i[2])
	    else:
		level = level - eval(i[2])
	    counters[level] = counters[level] + 1
	    next = 0
	    continue
	if i[0]=="\\" and i[1]=="n":
	    counters[level] = counters[level] + 1
	    next = 1
	    continue
	if i[0]=="\\" and i[1]=="+":
	    level = level + 1
	    counters[level] = 1
	    next = 1
	    continue
	if i[0]=="\\" and i[1]=="-":
	#counters[level] = 0
	    level = level - 1
	    counters[level] = counters[level] + 1
	    next = 1
	    continue
	mmatch = minputfile.match(i)
	if not mmatch is None:
	    print mmatch.group('file')
	    process_file(mmatch.group('file'))	    
	    continue
	if next == 1:
	#print counters[level]
	#print range(0,1)
	    t= ""
	    for k in range(0,level+1):
		t = t+ `counters[k]`+"."
		#print t,i, # por culpa de tener antes una linea
	    outputfile.write(dic_sections[level]+"{"+string.strip(i)+"}\n")
	    next = 0
	    continue
	outputfile.write(i)

process_lines_in_file(lines_of_the_main_file)

outputfile.close()