forked from hpi-swa-lab/SqueakByExample-english
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.rb
executable file
·90 lines (83 loc) · 2.67 KB
/
examples.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
#! /usr/bin/ruby -s
#
# examples --- extract code examples from Squeak by Example LaTeX source
#
# $Id$
# ============================================================
Header = <<eof
===== SQUEAK BY EXAMPLE ==========
Below follow all the (displayed) code examples from the book "Squeak by
Example".
For details about this book, see: http://SqueakByExample.org
The examples are provided, as is, for your convenience, in case you want
to copy and paste fragments to Squeak to try out.
Note that in almost all cases the annotation "--> ..." suggests that you
can select and apply <print it> to the previous expression and you should
obtain as a result the value following the arrow.
Many of these actually serve as test cases for the book. For more details
about testing, see the Wiki link under:
http://www.squeaksource.com/SBEtesting.html
eof
# ============================================================
def main
puts Header
ARGV.each do |arg|
ch = Chapter.new arg
puts ch
end
end
# ============================================================
class Chapter
attr_reader :name, :title, :code
# ----------------------------------------------------------
def initialize name
@name = name
@title = "<unknown>"
@code = ""
file_name = name + "/" + name + ".tex"
file = File.open file_name
while !(file.eof?)
line = file.readline
case
# grab chapter title
when line =~ /\\chapter\{([^}]*)\}/
@title = $1
@title.gsub!(/\\sq/, "Squeak") # Expand macros
@title.gsub!(/\\st/, "Smalltalk")
# look for the code listing environments
when line =~ /^\\begin\{(code|example|script|classdef|methods?|numMethod)\}/
@code << get_code(file)
end
end
end
# ----------------------------------------------------------
private
def get_code file
code = String.new
line = file.readline
while !(line =~ /^\\end\{/)
# comment out --> incantation
line.gsub!(/\s*(-->[^"\r\n]*)/, ' "\1" ')
# translate listings macros
line.gsub!(/>>>/, '>>')
line.gsub!(/BANG/, '!')
line.gsub!(/UNDERSCORE/, '_') # not needed ?
# compact extra space around comments
line.gsub!(/" +/, '"')
line.gsub!(/""/, '')
line.gsub!(/ +"/, ' "')
code << line
line = file.readline
end
return code + "-----\n"
end
# ----------------------------------------------------------
public
def to_s
"\n===== CHAPTER: " + @title + " ==========\n\n-----\n" + code
end
# ----------------------------------------------------------
end
# ============================================================
main
# ============================================================