Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add runnable code #28

Merged
merged 10 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ multilingual = false
git-repository-url = "https://github.com/Together-Java/ModernJava"
edit-url-template = "https://github.com/Together-Java/ModernJava/edit/develop/{path}"
mathjax-support = true
additional-css = ["ferris.css"]
additional-js = ["ferris.js"]

[output.html.fold]
enable = true # whether or not to enable section folding
Expand All @@ -25,4 +27,5 @@ simple_io = false
java_21 = false

[output.html.playground]
editable = true
editable = true

45 changes: 45 additions & 0 deletions ferris.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
body.light .does_not_compile,
body.light .panics,
body.light .not_desired_behavior,
body.rust .does_not_compile,
body.rust .panics,
body.rust .not_desired_behavior {
background: #fff1f1;
}

body.coal .does_not_compile,
body.coal .panics,
body.coal .not_desired_behavior,
body.navy .does_not_compile,
body.navy .panics,
body.navy .not_desired_behavior,
body.ayu .does_not_compile,
body.ayu .panics,
body.ayu .not_desired_behavior {
background: #501f21;
}

.ferris-container {
position: absolute;
z-index: 99;
right: 5px;
top: 30px;
}

.ferris {
vertical-align: top;
margin-left: 0.2em;
height: auto;
}

.ferris-large {
width: 4.5em;
}

.ferris-small {
width: 2.3em;
}

.ferris-explain {
width: 100px;
}
66 changes: 66 additions & 0 deletions ferris.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var ferrisTypes = [
{
attr: 'does_not_compile',
title: 'This code does not compile!'
},
{
attr: 'panics',
title: 'This code panics!'
},
{
attr: 'not_desired_behavior',
title: 'This code does not produce the desired behavior.'
}
]

document.addEventListener('DOMContentLoaded', () => {
for (var ferrisType of ferrisTypes) {
attachFerrises(ferrisType)
}
})

function attachFerrises(type) {
var elements = document.getElementsByClassName(type.attr)

for (var codeBlock of elements) {
var lines = codeBlock.innerText.replace(/\n$/, '').split(/\n/).length
var size = 'large'
if (lines < 4) {
size = 'small'
}

var container = prepareFerrisContainer(codeBlock, size == 'small')
container.appendChild(createFerris(type, size))
}
}

function prepareFerrisContainer(element, useButtons) {
var foundButtons = element.parentElement.querySelector('.buttons')
if (useButtons && foundButtons) {
return foundButtons
}

var div = document.createElement('div')
div.classList.add('ferris-container')

element.parentElement.insertBefore(div, element)

return div
}

function createFerris(type, size) {
var a = document.createElement('a')
a.setAttribute('href', 'ch00-00-introduction.html#ferris')
a.setAttribute('target', '_blank')

var img = document.createElement('img')
img.setAttribute('src', '../img/' + type.attr + '.svg')
img.setAttribute('title', type.title)
img.classList.add('ferris')
img.classList.add('ferris-' + size)

a.appendChild(img)

return a
}

2 changes: 1 addition & 1 deletion out.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@
# User Input

- [User Input](./user_input.md)
- [readLine](./user_input/readLine.md)

# Data Types III

Expand Down
4 changes: 2 additions & 2 deletions src/first_steps/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ words.

~IF toplevel_anonymous_class

```java
```java, no_run
void main() {
// This prints hello world!
System.out.println("Hello, World!");
Expand All @@ -17,7 +17,7 @@ void main() {

~ELSE

```java
```java, no_run
public class Main {
public static void main(String[] args) {
// This prints hello world!
Expand Down
6 changes: 4 additions & 2 deletions src/first_steps/formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ You may have noticed that after each `{` all the code that comes after it is "in
~IF toplevel_anonymous_class

```java
void main() {
System.out.println("Hello, World!");
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
```

Expand Down
13 changes: 12 additions & 1 deletion src/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@ edit the following code.

~IF toplevel_anonymous_class

```java
<span id="ferris"></span>

| Ferris | Meaning |
|------------------------------------------------------------------------------------------------------------------|--------------------------------------------------|
| <img src="img/does_not_compile.svg" class="ferris-explain" alt="Ferris with a question mark"/> | This code does not compile! |
| <img src="img/panics.svg" class="ferris-explain" alt="Ferris throwing up their hands"/> | This code panics! |
| <img src="img/not_desired_behavior.svg" class="ferris-explain" alt="Ferris with one claw up, shrugging"/> | This code does not produce the desired behavior. |

In most situations, we’ll lead you to the correct version of any code that
doesn’t compile.

```java,panics
void main() {
System.out.println("Hello, World");
}
Expand Down
72 changes: 72 additions & 0 deletions src/img/does_not_compile.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions src/img/not_desired_behavior.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading