summaryrefslogtreecommitdiff
path: root/support/tab4tex/source/tab4tex.sno
blob: 81f71ac071fc7041f8ab92800346b1cc6b75b7db (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
* TAB4TEX
* Guido Milanese 2007 <guido.milanese@unicatt.it>
* 
* Usage:  tab2tex.sno rows, align, vertical_bar, hline, complete
* First and second argument are compulsory, 3rd, 4th and 5th optional (simple y/n choice)
* Example: snobol4 -b tab4tex.sno r=7 a=c b=y h=y c=y <tab.txt >tab.tex
* outputs a table planned for 7 rows, centered, with '|' and hline, as a simple complete LaTeX document

* Sets digits, newline and tabulator
 	digits = '1234567890'
-include "newline.inc"
	(~(nl  = newline()) (terminal = "Cannot set newline. Abort")) 	:s(end)
	tab = char(9)
*****************************************
* Internal functions	
* 
* -- FORMAT LINES - Changes '*' and '!' to Bold and Emphasized
	Define("format(datum)")		:(format_end)
format
* Bold values	
	( (datum ? pos(0) '*' = ) (datum ? rpos(1)  '*' = )
+		(datum = "\textbf{" datum '}'))		:s(format_ret)
* Emphasized
	( (datum ? pos(0) '!' = ) (datum ? rpos(1)  '!' = )
+		(datum = "\emph{"   datum '}'))		:s(format_ret)
* Small caps
	( (datum ? pos(0) '^' = ) (datum ? rpos(1)  '^' = )
+		(datum = "\textsc{"   datum '}'))	:s(format_ret)

format_ret	format = datum	:(return)
format_end

****************************************

* Error messages
	Em = "snobol4 -b tab4tex.sno r=7 a=c b=y h=y c=y <tab.txt >tab.tex"
	Optmust = "You must set at least (1) number of rows and (2) alignment"
+		  nl "e.g.: r=4 a=c. 'r' must be a number and 'a' one of l,r,c (= left, right, centre)" 	
* Options:
* Rows, alignment, separator '|' or empty, hlines or empty, complete LaTeX or not
	Options = host(0) 	
	s_Options = size(Options)
 	(~(gt(s_Options,0)) (terminal = Optmust)) :s(end)
* Rows
	(~(Options ? "r=" span(digits) . nrows)	(terminal = Optmust)) 	:s(end)
* Align	
	(~(Options ? "a=" any("lrcLRC") . align) 	(terminal = Optmust))	:s(end)
* Vertical bars (separator)
	(~((Options ? "b=" any("yY") . sep) (sep = '|') ) (terminal = "No vertical bar required") )
* Hlines
	(~((Options ? "h=" any("yY") . hline) (hline = "\hline") (hline = nl hline)) (terminal = "No hline required") )
* Complete LaTeX?
	Complete = 'N'
	(~((Options ? "c=" any("yY") . complete) ) (terminal = "Complete LaTeX document not required") )
	Complete = replace(Complete,&ucase,&lcase)

* Builds data
	data1  = sep align				;* separator and alignment
	data   = dupl(data1,nrows)		;* data (e.g. |c|c|c| )
	
* Complete LaTeX document required?	
	(leq(complete,('y')))
+	(output = "\documentclass{article}" nl "\usepackage[T1]{fontenc}" nl "\begin{document}" nl)
* Table header
	output = "\begin{tabular}" '{' data sep '}' tab nl hline

* Main
MAIN
read	line = trim(input)		:f(endenv)
		line ? (tab | "<M")		:f(read)
		line = line tab
split	line ? break(tab) . cell len(1) rem . line	:f(outrow)

* Multicolumn cells (uses function Format)
	(
+	(cell ? pos(0) ( "<M"  (span(digits) . number) '>' ) =  )
+	(cell = format(cell))
+	(cell = "\multicolumn" '{' number '}' '{' sep align sep "}{" cell '}')
+	)
* Normal cells (uses function Format)	
	cell = format(cell)

* Builds row	
	(row = row cell tab '&' tab) 		:(split)
* Writes row
outrow 
	row ? rpos(3) (tab '&' tab) =
clean	row "<E>" = ' '	:s(clean)
	(output = row tab "\\" tab hline) (row = )	:(read)	
	terminal = row :(read)
endenv
	output = "\end{tabular}"
	(leq(complete,'y')) (output = nl "\end{document}")

end