Skip to content

Commit

Permalink
Added bold, italic, underline and code formatting whilst printing text.
Browse files Browse the repository at this point in the history
Added example02 if you have the code for it
  • Loading branch information
jasoncabot committed Feb 25, 2024
1 parent cb7fb67 commit f4938d6
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ bin
*.wasm
.wrangler
node_modules

web/example02
11 changes: 10 additions & 1 deletion internal/jabl/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"strconv"
"strings"
"text/scanner"
"unicode"
)
Expand Down Expand Up @@ -169,7 +170,15 @@ func (l *lexer) Lex(lval *yySymType) int {
default:
if text[0] == '"' && text[len(text)-1] == '"' {
// trim the start and end quotes and add a newline
lval.String = text[1 : len(text)-1]
inner := text[1 : len(text)-1]

// replace escaped characters
inner = strings.ReplaceAll(inner, "\\n", "\n")
inner = strings.ReplaceAll(inner, "\\t", "\t")
inner = strings.ReplaceAll(inner, "\\\"", "\"")

lval.String = inner

return STRING
} else if text == "true" {
lval.Boolean = true
Expand Down
7 changes: 7 additions & 0 deletions internal/jabl/testdata/examples/dynamic_goto/code.jabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
goto(1d6 + ".jabl")
set("a", 1d6)
choice("a", {
goto(get("a") + 2d6 + ".jabl")
})
}
10 changes: 10 additions & 0 deletions internal/jabl/testdata/examples/dynamic_goto/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"output": "",
"choices": [
{
"text": "a",
"code": "{\n\tgoto(get(\"a\") + 2d6 + \".jabl\")\n}"
}
],
"transition": "6.jabl"
}
3 changes: 3 additions & 0 deletions internal/jabl/testdata/examples/dynamic_goto/state_after.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"a": 6
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 2 additions & 0 deletions internal/jabl/testdata/examples/print_string/code.jabl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
print("hello world")
print(("hello world"))
print("hello" + " " + "world")
print("hello \"world\"")
print("hello\nworld")
}
2 changes: 1 addition & 1 deletion internal/jabl/testdata/examples/print_string/result.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"output": "hello world\nhello world\nhello world\n",
"output": "hello world\nhello world\nhello world\nhello \"world\"\nhello\nworld\n",
"choices": null,
"transition": ""
}
4 changes: 4 additions & 0 deletions web/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,8 @@ body {

.option:hover {
background-color: #023021;
}

code {
background-color: #033827;
}
104 changes: 92 additions & 12 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,52 @@ const renderText = (text) => {

intervalId = setInterval(() => {
if (text && textIndex < text.length) {
if (text.charAt(textIndex) === "\n") {
consoleText.innerHTML += "<br/><br/>";
} else {
consoleText.innerHTML += text.charAt(textIndex);
switch (text.charAt(textIndex)) {
case "\n":
consoleText.innerHTML += "<br/><br/>";
break;
case "*":
let boldText = "";
textIndex++;
while (text.charAt(textIndex) !== "*") {
boldText += text.charAt(textIndex);
textIndex++;
}
consoleText.innerHTML += `<b>${boldText}</b>`;
break;
case "_":
let underlineText = "";
textIndex++;
while (text.charAt(textIndex) !== "_") {
underlineText += text.charAt(textIndex);
textIndex++;
}
consoleText.innerHTML += `<u>${underlineText}</u>`;
break;
case "`":
let codeText = "";
textIndex++;
while (text.charAt(textIndex) !== "`") {
codeText += text.charAt(textIndex);
textIndex++;
}
consoleText.innerHTML += `<code>${codeText}</code>`;
break;
case "/":
let italicText = "";
textIndex++;
while (text.charAt(textIndex) !== "/") {
italicText += text.charAt(textIndex);
textIndex++;
}
consoleText.innerHTML += `<i>${italicText}</i>`;
break;
default:
console.log(text.charAt(textIndex));
consoleText.innerHTML += text.charAt(textIndex);
break;
}

// scroll consoleText to the bottom of what it is displaying
consoleText.parentElement.scrollTop =
consoleText.parentElement.scrollHeight;
Expand All @@ -24,6 +65,23 @@ const renderText = (text) => {
clearInterval(intervalId);
}
}, 10);

// can tap to skip the text animation
consoleText.onclick = () => {
if (text && textIndex < text.length) {
let html = text.replace(/\/([^/]+?)\//g, "<i>$1</i>");

html = html.replace(/\n/g, "<br/><br/>");
html = html.replace(/\*([^\*]*?)\*/g, "<b>$1</b>");
html = html.replace(/_([^_]+?)_/g, "<u>$1</u>");
html = html.replace(/`([^`]+)`/g, "<code>$1</code>");

consoleText.innerHTML = html;

clearInterval(intervalId);
textIndex = text.length;
}
};
};

const renderChoices = (choices) => {
Expand Down Expand Up @@ -94,6 +152,25 @@ const run = async () => {
}
};

const sources = [
{
id: 1,
name: "Example 1",
url: "https://raw.githubusercontent.com/jasoncabot/fabled-story-book/main/assets/example01/",
entrypoint: "entrypoint.jabl",
},
{
id: 2,
name: "Example 2",
url: "http://localhost:8788/example02/",
entrypoint: "0-choose-character.jabl",
},
];
const availableSources = sources.reduce((acc, source) => {
acc[source.id] = source.url;
return acc;
}, {});

const registerGlobals = () => {
window.bookStorage = {
getItem: (key) => {
Expand All @@ -120,7 +197,10 @@ const registerGlobals = () => {
localStorage.removeItem(key);
}
}
localStorage.setItem("system:section", "entrypoint.jabl");
const entrypoint = sources.find(
(source) => source.id == sourceId
)?.entrypoint;
localStorage.setItem("system:section", entrypoint);

// And restart the game
startSelection();
Expand All @@ -136,9 +216,7 @@ const registerGlobals = () => {
};
window.loadSection = (identifier, callback) => {
const sourceId = localStorage.getItem("system:source");
const sourceURL = {
1: "https://raw.githubusercontent.com/jasoncabot/fabled-story-book/main/assets/example01/",
}[sourceId];
const sourceURL = availableSources[sourceId];
if (!sourceURL) {
throw new Error("Invalid source id");
}
Expand All @@ -165,13 +243,15 @@ const startSelection = () => {
};

const showSelectionChoices = () => {
const choices = sources.map(
(source) =>
`choice("${source.name}", { set("system:source", ${source.id}) goto("${source.entrypoint}")})`
);

runJABL(`{
print("Welcome to the game!")
print("Which book would you like to play?")
choice("Example 1", {
set("system:source", 1)
goto("entrypoint.jabl")
})
${choices.join("\n")}
}`);
};

Expand Down

0 comments on commit f4938d6

Please sign in to comment.