Permalink
Browse files

Added Gnu Smalltalk version of example extractor

git-svn-id: https://www.iam.unibe.ch/scg/svn_repos/SqueakByExample/Book@12123 54883e8d-cf1d-0410-83d6-f114b9419f37
  • Loading branch information...
Oscar Nierstrasz
Oscar Nierstrasz committed Sep 22, 2007
1 parent 4421150 commit 916c217d89c6dd3f4678cd8115c8c1f3d949a398
Showing with 127 additions and 4 deletions.
  1. +3 −1 Makefile
  2. +1 −3 examples.rb
  3. +123 −0 examples.st
View
@@ -36,7 +36,9 @@ complete : book index
time ${PDFLATEX} ${BOOK}
examples :
./examples.rb $C > ../$@.txt
time ./examples.rb $C > ../$@.txt
# Gnu Smalltalk version:
# time ./examples.st $C > $@.txt
# --------------------------------------------------------------------------------
# MAINTENANCE
View
@@ -2,8 +2,6 @@
#
# examples --- extract code examples from Squeak by Example LaTeX source
#
# Why Ruby and not Squeak? Why not? Anyway, it's easier to call from make.
#
# $Id$
# ============================================================
Header = <<eof
@@ -26,7 +24,6 @@
about testing, see the Wiki link under:
http://www.squeaksource.com/SBEtesting.html
eof
# ============================================================
def main
@@ -74,6 +71,7 @@ def get_code file
line.gsub!(/UNDERSCORE/, '_') # not needed ?
# compact extra space around comments
line.gsub!(/" +/, '"')
line.gsub!(/""/, '')
line.gsub!(/ +"/, ' "')
code << line
line = file.readline
View
@@ -0,0 +1,123 @@
#! /usr/local/bin/gst -f
"
examples --- extract code examples from Squeak by Example LaTeX source
Assumes you have Gnu Smalltalk with scripting syntax installed (at least v2.95c).
$Id$
"
header := '
===== 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
'.
Object subclass: Chapter [
| name title code |
setName: aName [
<category: 'initialization'>
name := aName.
self init.
]
setTitle: aString [
<category: 'initialization'>
title := aString.
title := title replacingAllRegex: '\\sq' with: 'Squeak'.
title := title replacingAllRegex: '\\st' with: 'Smalltalk'.
]
init [
| fileName file codeStream titleRegex codeRegex |
<category: 'initialization'>
title := '<unknown>'. "default value"
codeStream := WriteStream on: String new.
fileName := name, '/', name, '.tex'.
titleRegex := '\\chapter\{([^}]*)\}' asRegex.
codeRegex := '^\\begin\{(code|example|script|classdef|methods?|numMethod)\}' asRegex.
file := FileStream open: fileName mode: FileStream read.
[ file atEnd ] whileFalse: [ | line |
line := file nextLine.
line =~ titleRegex ifMatched: [ :result |
self setTitle: (result at: 1)
] ifNotMatched: [
line =~ codeRegex ifMatched: [ :result |
self getCode: file to: codeStream
] ifNotMatched: [ ]
]
].
code := codeStream contents.
file close.
]
getCode: file to: codeStream [
<category: 'private'>
| line endRegex |
endRegex := '^\\end\{' asRegex.
line := file nextLine.
[ line ~ endRegex ] whileFalse: [
"comment out --> incantation"
line := line replacingAllRegex: '\s*(-->[^"\r\n]*)' with: ' "%1" '.
"translate listings macros"
line := line replacingAllRegex: '>>>' with: '>>'.
line := line replacingAllRegex: 'BANG' with: '!'.
line := line replacingAllRegex: 'UNDERSCORE' with: '_'.
"compact extra space around comments"
line := line replacingAllRegex: '" +' with: '"'.
line := line replacingAllRegex: '""' with: ''.
line := line replacingAllRegex: ' +"' with: ' "'.
codeStream nextPutAll: line; nl.
line := file nextLine
].
codeStream nextPutAll: '-----'; nl.
]
name [
<category: 'accessing'>
^ name
]
title [
<category: 'accessing'>
^ title
]
code [
<category: 'accessing'>
^ code
]
printOn: stream [
<category: 'printing'>
stream
nextPutAll: '===== CHAPTER: '; nextPutAll: self title;
nextPutAll: ' =========='; nl; nl;
nextPutAll: '-----'; nl; nextPutAll: self code.
]
]
Chapter class extend [
named: aName [
| ch |
<category: 'instance creation'>
ch := super new.
ch setName: aName.
^ ch
]
]
Transcript show: header.
(Smalltalk arguments) do: [ :each | |ch|
ch := Chapter named: each.
ch printNl.
].

0 comments on commit 916c217

Please sign in to comment.