-
Notifications
You must be signed in to change notification settings - Fork 0
/
latexinject.rb
143 lines (121 loc) · 4.28 KB
/
latexinject.rb
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
#def method_missing(m, *args, &block)
# puts "There's no method called #{m} here."
#end
def newpage input, order, output
output.write("<!-- \\newpage -->\n")
end
def sequencediagram input, order, output
output.write("<!--\n")
output.write("\\begin{sequencediagram}\n")
stack = Array.new
depth = Array.new
while line = input.gets
break if line.scan(/\[(.*?)\]/).include? ["sequencediagram"]
s = String.new
currentDepth = line[/\A */].size
puts ""
puts stack
if !stack.empty? && !line.empty? then
copy = Array.new(depth)
copy.each do |depthlv|
if depthlv >= currentDepth then
d = depth.pop
s << " "*d
s << stack.pop
puts "pop" << s
end
end
end
case
# \ newinst [1]{ c }{: C }
when !(instance = line.scan(/\A:(\w+)\[/)).empty?
s << "\\newinst"
param = line.scan(/\[(\w+)\]/)
s << "["<<param[1].to_s<<"]" if param[1]
s << "{"<<param[0].to_s<<"}"
s << "{:"<<instance.to_s<<"}"
# \ newthread [ blue ]{ b }{: Blue }
when !(instance = line.scan(/\A(\w+)\[/)).empty?
s << "\\newthread"
param = line.scan(/\[(\w+)\]/)
s << "["<<param[1].to_s<<"]" if param[1]
s << "{"<<param[0].to_s<<"}"
s << "{"<<instance.to_s<<"}"
#\ begin { messcall }{ t }{ function () }{ i}
#\ end { messcall }
when !(call = line.scan(/(\S+)\->>(\S+)\s*\:\s*([^:]*):\s*(.*)/)).empty?
stack.push("\\end{messcall}\n")
depth.push(currentDepth)
s << " "*currentDepth
s << "\\begin{messcall}"
s << "{" << call[0][0].to_s << "}"
s << "{" << call[0][2].to_s << "}"
s << "{" << call[0][1].to_s << "}"
s << "{" << call[0][3].to_s << "}"
#\ begin { call }{ t }{ function () }{ i }{ return value }
#\ end { call }
when !(call = line.scan(/(\S+)->(\S+)\s*\:\s*([^:]*):\s*(.*)/)).empty?
stack.push("\\end{call}\n")
depth.push(currentDepth)
s << " "*currentDepth
s << "\\begin{call}"
s << "{" << call[0][0].to_s << "}"
s << "{" << call[0][2].to_s << "}"
s << "{" << call[0][1].to_s << "}"
s << "{" << call[0][3].to_s << "}"
#\ begin { callself }{ t }{ function () }{ return value }
#\ end { callself }
when !(call = line.scan(/->(\S+)\s*\:\s*([^:]*):\s*(.*)/)).empty?
stack.push("\\end{callself}\n")
depth.push(currentDepth)
s << " "*currentDepth
s << "\\begin{callself}"
s << "{" << call[0][0].to_s << "}"
s << "{" << call[0][2].to_s << "}"
s << "{" << call[0][1].to_s << "}"
#\ begin { sdblock }{ Block }{ description }
#\ end { sdblock }
when !(call = line.scan(/\loop\s+(\S+)\s+(\S+)/)).empty?
stack.push("\\end{sdblock}\n")
depth.push(currentDepth)
s << " "*currentDepth
s << "\\begin{sdblock}"
s << "{" << call[0][0].to_s << "}"
s << "{" << call[0][1].to_s << "}"
#else
# puts line
end
output.write(s<<"\n")
end
while !stack.empty?
output.write(stack.pop)
end
output.write("\\end{sequencediagram}\n")
output.write("-->\n")
end
#### State
$state = {}
indicator = 91
#### Begin
raise "Wrong amount of arguments" if ARGV.size != 2
begin
input = File.new(ARGV[0], "r")
output = File.new(ARGV[1], "w")
rescue => err
puts "Exception: #{err}"
err
end
begin
while line = input.gets
if line[0] == indicator
order = line.scan(/\[(.*?)\]/)
self.send(order[0].to_s.to_sym, input, order, output)
else
output.write(line)
end
end
input.close
rescue => err
puts "Exception: #{err}"
err
end