Skip to content

Commit 802057b

Browse files
Merge pull request AllenDowney#13 from trinketapp/trinket-poc
Trinket proof of concept
2 parents 1922137 + 2118d62 commit 802057b

File tree

4 files changed

+50
-41
lines changed

4 files changed

+50
-41
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ thinkjava.toc
1313
# Java examples
1414
*.class
1515
checkstyle*.jar
16+
17+
# HTML output
18+
trinkethtml/*
19+
html/*

Makefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
F=thinkjava
22

3+
.PHONY: trinket
4+
35
all:
46
pdflatex $(F)
57
makeindex $(F).idx # shouldn't need .idx here, but we do
@@ -57,13 +59,15 @@ distrib:
5759
# a bug (in ocaml?) causes "make trinket" to fail; use "make -i trinket" instead
5860
trinket: thinkjava.tex header.html footer.html
5961
cp $(F).tex $(F)6.tex
60-
rm -rf html
61-
mkdir html
62+
rm -rf trinkethtml
63+
mkdir trinkethtml
6264
hevea -O -exec xxdate.exe -e latexonly trinket $(F)6
6365
hevea -O -exec xxdate.exe -e latexonly trinket $(F)6
6466
imagen -png -pdf $(F)6
6567
imagen -png -pdf $(F)6
6668
hacha $(F)6.html
67-
cp up.png next.png back.png html
68-
mv index.html $(F)6.css $(F)6?*.html $(F)6*.png html
69+
cp up.png next.png back.png trinkethtml
70+
mv index.html $(F)6.css $(F)6?*.html $(F)6*.png trinkethtml
6971
rm *motif.gif $(F)6.*
72+
# perl postprocessing (woot) seems easier than escaping through Latex and Hevea
73+
perl -i -pe 's/\[\[\[\[\s?(\S*?)\s?\]\]\]\]/----{\1}----/g' trinkethtml/*.html

thinkjava.tex

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ \section{The hello world program}
561561
% more precise, but also to reduce the number of times ``print'' appears,
562562
% which is a lot!
563563

564-
\begin{trinket}
564+
\begin{trinket}{Hello.java}
565565
public class Hello {
566566

567567
public static void main(String[] args) {
@@ -651,7 +651,7 @@ \section{Displaying strings}
651651
You can put as many statements as you like in \java{main}.
652652
For example, to display more than one line of output:
653653

654-
\begin{code}
654+
\begin{trinket}{Hello.java}
655655
public class Hello {
656656

657657
public static void main(String[] args) {
@@ -660,7 +660,7 @@ \section{Displaying strings}
660660
System.out.println("How are you?"); // another line
661661
}
662662
}
663-
\end{code}
663+
\end{trinket}
664664

665665
As this example shows, you can put comments at the end of a line as well as on lines all by themselves.
666666

@@ -762,36 +762,36 @@ \section{Formatting code}
762762
In Java programs, some spaces are required.
763763
For example, you need at least one space between words, so this program is not legal:
764764

765-
\begin{code}
765+
\begin{trinket}{Goodbye.java}
766766
publicclassGoodbye{
767767

768768
publicstaticvoidmain(String[] args) {
769769
System.out.print("Goodbye, ");
770770
System.out.println("cruel world");
771771
}
772772
}
773-
\end{code}
773+
\end{trinket}
774774

775775
But most other spaces are optional.
776776
For example, this program is legal:
777777

778-
\begin{code}
778+
\begin{trinket}{Goodbye.java}
779779
public class Goodbye {
780780
public static void main(String[] args) {
781781
System.out.print("Goodbye, ");
782782
System.out.println("cruel world");
783783
}
784784
}
785-
\end{code}
785+
\end{trinket}
786786

787787
The newlines are optional, too.
788788
So we could just write:
789789

790-
\begin{code}
790+
\begin{trinket}{Goodbye.java}
791791
public class Goodbye { public static void main(String[] args)
792792
{ System.out.print("Goodbye, "); System.out.println
793793
("cruel world");}}
794-
\end{code}
794+
\end{trinket}
795795

796796
It still works, but the program is getting harder and harder to read.
797797
Newlines and spaces are important for organizing your program visually, making it easier to understand the program and find errors when they occur.
@@ -1661,15 +1661,15 @@ \section{Types of errors}
16611661
Error messages from the compiler usually indicate where in the program the error occurred, and sometimes they can tell you exactly what the error is.
16621662
As an example, let's get back to the hello world program from Section~\ref{hello}.
16631663

1664-
\begin{code}
1664+
\begin{trinket}{Hello.java}
16651665
public class Hello {
16661666

16671667
public static void main(String[] args) {
16681668
// generate some simple output
16691669
System.out.println("Hello, World!");
16701670
}
16711671
}
1672-
\end{code}
1672+
\end{trinket}
16731673

16741674
\index{semicolon}
16751675

@@ -1753,15 +1753,15 @@ \section{Types of errors}
17531753
Instead, it will do exactly what you told it to do.
17541754
For example, here is a version of the hello world program with a logic error:
17551755

1756-
\begin{code}
1756+
\begin{trinket}{Hello.java}
17571757
public class Hello {
17581758

17591759
public static void main(String[] args) {
17601760
System.out.println("Hello, ");
17611761
System.out.println("World!");
17621762
}
17631763
}
1764-
\end{code}
1764+
\end{trinket}
17651765

17661766
This program compiles and runs just fine, but the output is:
17671767

@@ -2068,7 +2068,7 @@ \section{The Scanner class}
20682068
\java{Scanner} provides a method called \java{nextLine} that reads a line of input from the keyboard and returns a \java{String}.
20692069
The following example reads two lines and repeats them back to the user:
20702070

2071-
\begin{code}
2071+
\begin{trinket}{Echo.java}
20722072
import java.util.Scanner;
20732073

20742074
public class Echo {
@@ -2086,7 +2086,7 @@ \section{The Scanner class}
20862086
System.out.println("You also said: " + line);
20872087
}
20882088
}
2089-
\end{code}
2089+
\end{trinket}
20902090

20912091
If you omit the import statement and later refer to \java{Scanner}, you will get a compiler error like ``cannot find symbol''.
20922092
That means the compiler doesn't know what you mean by \java{Scanner}.
@@ -2397,7 +2397,7 @@ \section{Putting it all together}
23972397
%Since we've looked at each of these topics in isolation, it's important to see how they fit together in a complete program.
23982398
%If you've been working through the examples on your computer as you've been reading (like we recommended in Section~\ref{sec:examples}), then good job!
23992399

2400-
\begin{code}
2400+
\begin{trinket}{Convert.java}
24012401
import java.util.Scanner;
24022402

24032403
/**
@@ -2424,7 +2424,7 @@ \section{Putting it all together}
24242424
cm, feet, remainder);
24252425
}
24262426
}
2427-
\end{code}
2427+
\end{trinket}
24282428

24292429
Although not required, all variables and constants are declared at the top of \java{main}.
24302430
This practice makes it easier to find their types later on, and it helps the reader know what data is involved in the algorithm.
@@ -2645,7 +2645,7 @@ \section{Exercises}
26452645
To choose a random number, you can use the \java{Random} class in \java{java.util}.
26462646
Here's how it works:
26472647

2648-
\begin{code}
2648+
\begin{trinket}{GuessStarter.java}
26492649
import java.util.Random;
26502650

26512651
public class GuessStarter {
@@ -2657,7 +2657,7 @@ \section{Exercises}
26572657
System.out.println(number);
26582658
}
26592659
}
2660-
\end{code}
2660+
\end{trinket}
26612661

26622662
\index{new}
26632663
\index{operator!new}
@@ -2833,7 +2833,7 @@ \section{Adding new methods}
28332833
You have probably guessed by now that you can define more than one method in a class.
28342834
Here's an example:
28352835

2836-
\begin{code}
2836+
\begin{trinket}{NweLine.java}
28372837
public class NewLine {
28382838

28392839
public static void newLine() {
@@ -2846,7 +2846,7 @@ \section{Adding new methods}
28462846
System.out.println("Second line.");
28472847
}
28482848
}
2849-
\end{code}
2849+
\end{trinket}
28502850

28512851
\index{main}
28522852
\index{case-sensitive}
@@ -2948,7 +2948,7 @@ \section{Flow of execution}
29482948

29492949
Pulling together the code from the previous section, the complete program looks like this:
29502950

2951-
\begin{code}
2951+
\begin{trinket}{NewLine.java}
29522952
public class NewLine {
29532953

29542954
public static void newLine() {
@@ -2967,7 +2967,7 @@ \section{Flow of execution}
29672967
System.out.println("Second line.");
29682968
}
29692969
}
2970-
\end{code}
2970+
\end{trinket}
29712971

29722972
\index{flow of execution}
29732973

@@ -3008,7 +3008,7 @@ \section{Parameters and arguments}
30083008
The parameter list indicates what arguments are required.
30093009
The following class shows an example:
30103010

3011-
\begin{code}
3011+
\begin{trinket}{PrintTwice.java}
30123012
public class PrintTwice {
30133013

30143014
public static void printTwice(String s) {
@@ -3020,7 +3020,7 @@ \section{Parameters and arguments}
30203020
printTwice("Don't make me say this twice!");
30213021
}
30223022
}
3023-
\end{code}
3023+
\end{trinket}
30243024

30253025
\java{printTwice} has a parameter named \java{s} with type \java{String}.
30263026
When we invoke \java{printTwice}, we have to provide an argument with type \java{String}.
@@ -3128,7 +3128,7 @@ \section{Stack diagrams}
31283128

31293129
Pulling together the code fragments from the previous section, here is a complete class definition:
31303130

3131-
\begin{code}
3131+
\begin{trinket}{PrintTime.java}
31323132
public class PrintTime {
31333133

31343134
public static void printTime(int hour, int minute) {
@@ -3143,7 +3143,7 @@ \section{Stack diagrams}
31433143
printTime(hour, minute);
31443144
}
31453145
}
3146-
\end{code}
3146+
\end{trinket}
31473147

31483148
\java{printTime} has two parameters, named \java{hour} and \java{minute}.
31493149
And \java{main} has two variables, also named \java{hour} and \java{minute}.
@@ -3311,7 +3311,7 @@ \section{Writing documentation}
33113311

33123312
Here's a class definition with two Javadoc comments, one for the class and one for the \java{main} method:
33133313

3314-
\begin{code}
3314+
\begin{trinket}{Goodbye.java}
33153315
/**
33163316
* Example program that demonstrates print vs println.
33173317
*/
@@ -3325,7 +3325,7 @@ \section{Writing documentation}
33253325
System.out.println("cruel world");
33263326
}
33273327
}
3328-
\end{code}
3328+
\end{trinket}
33293329

33303330
The class comment explains the purpose of the class.
33313331
The method comment explains what the method does.
@@ -4246,7 +4246,7 @@ \section{Exercises}
42464246
This exercise reviews the flow of execution through a program with multiple methods.
42474247
Read the following code and answer the questions.
42484248

4249-
\begin{code}
4249+
\begin{trinket}{Buzz.java}
42504250
public class Buzz {
42514251

42524252
public static void baffle(String blimp) {
@@ -4269,7 +4269,7 @@ \section{Exercises}
42694269
}
42704270

42714271
}
4272-
\end{code}
4272+
\end{trinket}
42734273

42744274
\begin{enumerate}
42754275

@@ -7245,13 +7245,13 @@ \section{Command-line arguments}
72457245
Rather than read the numbers from \java{System.in}, we'll pass them as command-line arguments.
72467246
Here is a starting point:
72477247

7248-
\begin{code}
7248+
\begin{trinket}{Max.java}
72497249
public class Max {
72507250
public static void main(String[] args) {
72517251
System.out.println(Arrays.toString(args));
72527252
}
72537253
}
7254-
\end{code}
7254+
\end{trinket}
72557255

72567256
You can run this program from the command line by typing:
72577257

@@ -12560,4 +12560,4 @@ \subsection*{I found the bug!}
1256012560

1256112561
%\cleardoublepage
1256212562

12563-
\end{document}
12563+
\end{document}

trinket

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@
6363

6464
% custom HTML for Trinket publisher tools
6565
% see http://hevea.inria.fr/doc/manual018.html
66-
\newenvironment{trinket}
67-
{\@print{<b>TODO begin Trinket</b>}\rawhtml}
68-
{\endrawhtml\@print{<b>TODO end Trinket</b>}}
66+
\newenvironment{trinket}[1]
67+
{\@print{<script type="text/javascript">(function(d,l,s,i,c){function n(e){e=e.nextSibling;return (!e||e.nodeType!=3)?e:n(e);};function r(f){/in/.test(d.readyState) ? setTimeout(function(){r(f);},9):f()};l=d.getElementsByTagName('script');s=l[l.length-1];r(function(){i=n(s),c=n(i);i.setAttribute('src','https://trinket.io/tools/1.0/jekyll/embed/java#code='+encodeURIComponent(c.nodeValue.replace(/^\s+|\s+$/g,'')));});})(document)</script><iframe width="100%" height="400" frameborder="0" marginwidth="0" marginheight="0" class="lazyload" allowfullscreen></iframe><!--}
68+
[[[[ #1 ]]]]\rawhtml}
69+
{\endrawhtml\@print{-->}}
6970

7071
% inline syntax formatting
7172
\newcommand{\java}{\lstinline}%}

0 commit comments

Comments
 (0)