Skip to content

Commit

Permalink
feat: Add semantic versioning, add better demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
vantreeseba committed Sep 3, 2020
1 parent 515441d commit c12ca8e
Show file tree
Hide file tree
Showing 12 changed files with 11,463 additions and 5,818 deletions.
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ Use Storygen by defining a Grammar and running the generator.

A Grammar is a string-to-string array map (or an object with string arrays in JavaScript).

Let's take a look at an example.
```
For more resources look at [Resources](#Resources)

Let's take a look at an example.
```js
{
origin: ["#test#"],
test: ["hi", "hello", "hola"]
}
```

When run, this will randomly output one of "hi", "hello" or "hola".

There are other ways to define parts of a Grammar.

### Functions (#funcName(arg1, arg2, ...))
- random(min:Int, max:Int) => Returns random between min and max.
- switch(symbol, ['val=>symbol']) => Returns a symbol based on input val.

```
```js
grammar = {
numberOfPeople: ['#random(50,100)#'] // Outputs 69
}
```

```
```haxe
grammar = [
"origin" => ["#[char_race:race]##[char_name:name]##char#"],
"char" => ["#char_name# the #char_race#"],
Expand All @@ -40,12 +40,11 @@ grammar = [
];
// outputs either "bob the elf" or "sally the dwarf"
```

User-defined functions can be added via static class Functions.

```
```js
// gen is always passed as first arg.
Functions.set("custom", (gen, args) => {
var firstArgument = args[0];
Expand All @@ -71,7 +70,7 @@ grammar = {

You can apply multiple transforms.

```
```js
grammar = {
origin: ['#animal.capitalize.pluralize#'],
animal: ['horse', 'lion']
Expand All @@ -80,7 +79,7 @@ grammar = {

You can define custom transforms. They are always defined as function(string):string.

```
```js
Transforms.set("custom", str => "this is a test");

grammar = {
Expand All @@ -94,7 +93,7 @@ grammar = {

Will parse the "something", return the value, and store it into `my_mem_sym` for use later in the Grammar.

```
```js
grammar = {
name = ["bob", "sally"]
origin = ["#char_name:name# went to the store. There #char_name# did some stuff. Later #char_name# saw #name# at the bowling alley."]
Expand All @@ -111,13 +110,13 @@ grammar = {
If you want to prepare a Grammar in memory for later use, you can wrap
the token with [].

```
```js
#[char_name:name]#
```

This will output nothing until referenced later. For example:

```
```js
grammar = {
name: ["bob", "sally"],
race: ["elf", "goblin"],
Expand All @@ -136,7 +135,6 @@ grammar = {
* bob the blue elf really loved eating bananas.
* bob the blue elf also was not a fan of cheese.
*/
```

### Running generator
Expand All @@ -152,7 +150,7 @@ seed. The seed MUST be parseable to an INT.
If you want to get the current seed, the generator provids a getSeed method.

When runAdvanced is called, it does the same as run but returns an object like:
```
```js
{
seed: "23498093",
output: "bob the elf",
Expand All @@ -167,8 +165,7 @@ When runAdvanced is called, it does the same as run but returns an object like:
You can merge multiple grammars to simplify definitions.


```
```haxe
grammar = [
"origin" => ["#[char_race:race]##[char_name:name]##char#"],
"char" => ["#char_name# the #char_race#"],
Expand All @@ -190,7 +187,6 @@ gen.mergeGrammar(dwarf_names);
gen.run("#origin#");
// outputs either "bob the elf" or "sally the dwarf"
```

## JS
Expand All @@ -210,7 +206,6 @@ var output = gen.run('#origin#');
console.log(output);

// Outputs something like 'Bob loves apples'.

```


Expand All @@ -234,5 +229,9 @@ class Main {
// Outputs something like 'Mary loves bananas'.
}
}
```

## Resources

[Tracery](https://www.tracery.io)

13 changes: 1 addition & 12 deletions build.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,4 @@
-D source-map-content

# OUTPUT
-js dist/js/index.js

# --next
## C#
# DEFINES / MACROS
# -D dce=no
# -D dll_import
# -D no-root
# -D dll

# OUTPUT
# -cs dist/cs/rules_engine
-js dist/js/storygen.js
44 changes: 44 additions & 0 deletions docs/example1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
addExample('example 1', () => {
var grammar = {
origin: ['#name.capitalize# loves #food.pluralize#'],
name: ['bob', 'mary'],
food: ['banana', 'apple']
};

var gen = new Generator(grammar);
var output = gen.runAdvanced('#origin#');

console.log(output);

const rootEl = document.createElement('div');
const textEl = document.createElement('pre');
const codeEl = document.createElement('pre');
const outputEl = document.createElement('div');

textEl.classList.add('output-text');
codeEl.classList.add('output-code');
outputEl.classList.add('output-json');

codeEl.innerText =`${'```js'}
var grammar = {
origin: ['#name.capitalize# loves #food.pluralize#'],
name: ['bob', 'mary'],
food: ['banana', 'apple']
};
var gen = new Generator(grammar);
var output = gen.runAdvanced('#origin#');
console.log(output);
${'```'}
`;

outputEl.innerHTML = `<pre>${JSON.stringify(output, null, 2)}</pre>`;
textEl.innerText = output.output;

rootEl.appendChild(textEl);
rootEl.appendChild(codeEl);
rootEl.appendChild(outputEl);

return rootEl;
});
42 changes: 42 additions & 0 deletions docs/example2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
addExample('random function', () => {
var grammar = {
numberOfPeople: ['#random(50,100)#'],
origin: ['There are #numberOfPeople# people in the village.']
};

var gen = new Generator(grammar);
var output = gen.runAdvanced('#origin#');

console.log(output);

const rootEl = document.createElement('div');
const textEl = document.createElement('pre');
const codeEl = document.createElement('pre');
const outputEl = document.createElement('div');

textEl.classList.add('output-text');
codeEl.classList.add('output-code');
outputEl.classList.add('output-json');

codeEl.innerText =`${'```js'}
var grammar = {
numberOfPeople: ['#random(50,100)#'],
origin: ['There are #numberOfPeople# people in the village.']
};
var gen = new Generator(grammar);
var output = gen.runAdvanced('#origin#');
console.log(output);
${'```'}
`;

outputEl.innerHTML = `<pre>${JSON.stringify(output, null, 2)}</pre>`;
textEl.innerText = output.output;

rootEl.appendChild(textEl);
rootEl.appendChild(codeEl);
rootEl.appendChild(outputEl);

return rootEl;
});
50 changes: 50 additions & 0 deletions docs/example3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
addExample('switch function', () => {
var grammar = {
'origin': ['#[char_heritage:heritage]##[char_name:name]##char#'],
'char': ['#char_name# the #char_heritage#'],
'heritage': ['elf', 'dwarf'],
'elfNames': ['legolas', 'eldrin', 'lilli'],
'dwarfNames': ['gak', 'bar', 'norn'],
'name': ['#switch(char_heritage, elf=>elfNames, dwarf=>dwarfNames)#']
};

var gen = new Generator(grammar);
var output = gen.runAdvanced('#origin#');

console.log(output);

const rootEl = document.createElement('div');
const textEl = document.createElement('pre');
const codeEl = document.createElement('pre');
const outputEl = document.createElement('div');

textEl.classList.add('output-text');
codeEl.classList.add('output-code');
outputEl.classList.add('output-json');

codeEl.innerText =`${'```js'}
var grammar = {
'origin': ['#[char_heritage:heritage]##[char_name:name]##char#'],
'char': ['#char_name# the #char_heritage#'],
'heritage': ['elf', 'dwarf'],
'elfNames': ['legolas', 'eldrin', 'lilli'],
'dwarfNames': ['gak', 'bar', 'norn'],
'name': ['#switch(char_heritage, elf=>elfNames, dwarf=>dwarfNames)#']
};
var gen = new Generator(grammar);
var output = gen.runAdvanced('#origin#');
console.log(output);
${'```'}
`;

outputEl.innerHTML = `<pre>${JSON.stringify(output, null, 2)}</pre>`;
textEl.innerText = output.output;

rootEl.appendChild(textEl);
rootEl.appendChild(codeEl);
rootEl.appendChild(outputEl);

return rootEl;
});
57 changes: 57 additions & 0 deletions docs/example4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
addExample('custom function', () => {
// gen is always passed as first arg.
Functions.set('subtract', (gen, args) => {
var firstArg = args[0];
var secondArg = args[1];

// Function implementation goes here.
return secondArg - firstArg;
});

var grammar = {
origin: ['#subtract(5, 6)#'] // "firstArg" will be 5, secondArg will be 6, will be replaced with 1 (from function return above.)
};

var gen = new Generator(grammar);
var output = gen.runAdvanced('#origin#');

console.log(output);

const rootEl = document.createElement('div');
const textEl = document.createElement('pre');
const codeEl = document.createElement('pre');
const outputEl = document.createElement('div');

textEl.classList.add('output-text');
codeEl.classList.add('output-code');
outputEl.classList.add('output-json');

codeEl.innerText =`${'```js'}
Functions.set('subtract', (gen, args) => {
var firstArg = args[0];
var secondArg = args[1];
// Function implementation goes here.
return secondArg - firstArg;
});
var grammar = {
origin: ['#subtract(5, 6)#'] // "firstArg" will be 5, secondArg will be 6, will be replaced with 1 (from function return above.)
};
var gen = new Generator(grammar);
var output = gen.runAdvanced('#origin#');
console.log(output);
${'```'}
`;

outputEl.innerHTML = `<pre>${JSON.stringify(output, null, 2)}</pre>`;
textEl.innerText = output.output;

rootEl.appendChild(textEl);
rootEl.appendChild(codeEl);
rootEl.appendChild(outputEl);

return rootEl;
});
Loading

0 comments on commit c12ca8e

Please sign in to comment.