Browse Source

ch-ch-changes

master
patxipierce 2 years ago
parent
commit
cede491e4a
5 changed files with 132 additions and 84 deletions
  1. +48
    -21
      README.md
  2. +4
    -1
      changelog.md
  3. +1
    -0
      filters/en_US-filters.js
  4. +37
    -34
      rasyen.js
  5. +42
    -28
      tests.html

+ 48
- 21
README.md View File

@@ -30,33 +30,42 @@ Rasyen (pronounced /ˈɹeɪzn/ like the dried grape) uses a list of options to s
- [Custom filters](#custom-filters)
- [Per list filters](#per-list-filters)
- [Sans-list filters](#sans-list-filters)
- [Filter nesting](#filter-nesting)

The most basic usage of RaSyEn could look like this.

```js
// Load (list name, Array or Object)
Rasyen.list_load("first-part", [
"Ae","Ara","Bal","Ylla","Zin","Zyl"
]);
Rasyen.list_load("plants", ["violets"]);

Rasyen.list_load("second-part", [
"balar","can","yra","zorwyn","zumin"
]);
alert(Rasyen.parse("Daisy likes %plants%"));
// → "Daisy likes %plants%"
```

And here is an example combining random words to generate a name.

```js
// Load (list name, Array or Object)
Rasyen.lists_load({
"first" : [
"Ae","Ara","Bal","Ylla","Zin","Zyl"
],
"second" : [
"balar","can","yra","zorwyn","zumin"
]
});

// Templates use tags like %list-name% to produce random output.
var template = "Your elf name is %first-part%-%second-part%.";
var template = "Your elf name is %first%%second%.";

// Parse the template
var out = Rasyen.parse(template);
// → "Your elf name is Ara-yra."
// → "Your elf name is Arayra."
```

Or consider this other example:

```js
// Load a "story list"
Rasyen.list_load("story", {
Rasyen.lists_load("story", {
"name" : [
"Ben",
"Jasper",
@@ -105,9 +114,25 @@ Templates are strings fed into the parser, who will look for _tags_, parts of th

With that in mind you can create templates with tags such as:

`%list-a@category|list-b@category=filter-1=filter-2=filter-n%`.
`%list%`

That returns a random string from `list`, or you can modify the output using a filter named `filter`, like this:

`%list=filter%`.

Easy-peasy. You could also descend into a `category` of the list and then modify the result with a filter:

`%list@category=filter%`.

What if you want a random result from two lists? Try this:

Easy peasy.
`%list-a|list-b%`.

... Or combine `list-a` with a category from `list-b` and then pass the result through `filter-1` and `filter-2`.

`%list-a|list-b@category=filter-1=filter-2%`.

It gets better, you can [save a category name and apply it to another list](#category-filter), or you can create [lists which contain templates](#the-meta-filter) to further randomize things.

### Methods

@@ -214,6 +239,8 @@ Pre-built filters are:
- Evaluates the tag again to check for more tags in the result string.
- `=inline`
- Will attempt to parse any JSON set before it and return a random result. It can also save the parsed JSON to a list if a second argument is provided.
- `=quiet`
- Will quietly parse any filters before, but not output anything on the template.

English Filters:

@@ -445,7 +472,7 @@ Rasyen.lists_load({
"he",
"she"
],
"adjective" : {
"preposition" : {
"he" : "him",
"she" : "her"
},
@@ -487,7 +514,7 @@ var template = [
"and plotted with %name=category=t2=save-result=n4%",
// → "and plotted with Morgana"

"to forsake %adjective=category=t1%."
"to forsake %preposition=category=t1%."
// → "to forsake him."
];

@@ -496,8 +523,6 @@ var template = [
var out = Rasyen.parse(template.join(" "));
```



In essence you now have four characters `n1`, `n2`, `n3` and `n4`, which you can use to add continuity to the narration. `n1` and `n3` are the same gender, and `n2` and `n4` are plotting against `n1`

### The Meta Filter
@@ -582,13 +607,15 @@ Rasyen.filters['color'] = function(list){
if(typeof Rasyen.lists['color'] !== 'undefined'){
// Get the full list apply categories and select an array
var color_list = Rasyen.lists['color'];
var col_cat = Rasyen.roi(color_list);
var color_cat = Rasyen.roi(color_list);
if(list.categories){
col_cat = Rasyen.navigate_obj(color_list, list.categories);
// search color_list with list.categories for the color category
color_cat = Rasyen.navigate_obj(color_list, list.categories);
}
var col = Rasyen.rai(col_cat);
// Get a random array item from color_cat
var col = Rasyen.rai(color_cat);
// Add the color to the word.
list.replace = '<span style="color:'+col[1]+'">'+col[0]+'</span>';
@@ -602,7 +629,7 @@ var out = Rasyen.parse("%color%");

### Sans-list Filters

Filters don't necessarily have to come after a list name. For example:
Filters don't necessarily have to come after a list name, you can also have a tag with a filter and no list. For example:

```js



+ 4
- 1
changelog.md View File

@@ -12,12 +12,15 @@ __________ _________ ___________

An account of all that has happened and future plans.

- **2.0.3**
- Added `=quiet` core filter.

- **2.0.2**
- Removed debugging code.
- Added `on_error` callback for elegant error handling.

- **2.0.1**
- Added the =inline core filter.
- Added the `=inline` core filter.
- Fixed a bug related to `save-results` and joining lists.
- Tuned the `pronoun-swap`, and the `to-plural` en_US filter.
- Added the filters callback.


+ 1
- 0
filters/en_US-filters.js View File

@@ -258,6 +258,7 @@ if(typeof Rasyen != 'undefined'){ // Make sure Rasyen is loaded
// After 9th position
new_pn = new_pn.toUpperCase();
}

if((i > 1 && i < 4)
|| (i > 6 && i < 9)
|| (i > 11 && i < 14)){


+ 37
- 34
rasyen.js View File

@@ -9,7 +9,7 @@ var Rasyen = {
/*
* Object variables
*/
version : '2.0.2',
version : '2.0.3',
lists : {},
saved_keys : [],
removed_items : [],
@@ -41,7 +41,7 @@ var Rasyen = {
// %list-name=first-to-lower% - Lower the first letter
'first-to-lower' : function(list) {
if(typeof list.replace === 'string' && list.replace !== ''){
list.replace = list.replace.charAt(0).toLowerCase() + list.replace.slice(1)
list.replace = list.replace.charAt(0).toLowerCase() + list.replace.slice(1);
}
return list;
},
@@ -109,9 +109,7 @@ var Rasyen = {

// %list-name=random-category% - Chooses a Key from the list (must be an object)
'random-category' : function(list) {
if(Rasyen.lists.hasOwnProperty(list.name)
&& Rasyen.lists[list.name] instanceof Object
&& Object.keys(Rasyen.lists[list.name]).length > 0){
if(Rasyen.lists.hasOwnProperty(list.name) && Rasyen.lists[list.name] instanceof Object && Object.keys(Rasyen.lists[list.name]).length > 0){
var obj = Rasyen.lists[list.name];
if(list.categories){
@@ -179,10 +177,11 @@ var Rasyen = {
'inline' : function(list){
// Find a list to save (if any)
var list_name = list.parsed_filters[list.parsed_filters.indexOf('inline')+1];
var list_data;

// Check to see if its valid JSON
try{
var list_data = JSON.parse(list.name);
list_data = JSON.parse(list.name);
}catch(e){
Rasyen.callback.on_error({
evt : e,
@@ -195,9 +194,15 @@ var Rasyen = {
list.name = list_name;
Rasyen.list_load(list_name, list_data);
}
list.replace = Rasyen.random_str(list_data)
list.replace = Rasyen.random_str(list_data);
}
return list;
},

// %list=save-result=new-list=quiet% - Will quietly parse the template but return nothing
'quiet' : function(list){
list.replace = '';
return list;
}
},

@@ -208,8 +213,7 @@ var Rasyen = {
// returns max and min inclusive random number
random_range : function(min, max){
var int = min;
if( this.options.use_window_crypto
&& typeof window.crypto == 'object'){
if( this.options.use_window_crypto && typeof window.crypto == 'object'){
// Soooper cool random generation.
var range = (max + 1) - min;
var requestBytes = Math.ceil(Math.log2(range) / 8);
@@ -287,8 +291,7 @@ var Rasyen = {
for(var i = 1; i < arguments.length; i++) {
for(var key in arguments[i]) {
if(arguments[i].hasOwnProperty(key)) {
if (typeof arguments[0][key] === 'object'
&& typeof arguments[i][key] === 'object'){
if (typeof arguments[0][key] === 'object' && typeof arguments[i][key] === 'object'){
this.extend_obj(arguments[0][key], arguments[i][key]);
}else{
arguments[0][key] = arguments[i][key];
@@ -306,9 +309,9 @@ var Rasyen = {
// Convert arr to a deep object
for (var i = 0; i < arr.length; i++) {
if(arr.length - 1 == i){
temp = temp[arr[i]] = rep
temp = temp[arr[i]] = rep;
}else{
temp = temp[arr[i]] = {}
temp = temp[arr[i]] = {};
}
}
// Merge with original object
@@ -340,20 +343,18 @@ var Rasyen = {
data = this.lists[list_name];
}

if(data instanceof Array){
var pos = data.indexOf(str);
if(pos != -1) {
// Remove item
data.splice(pos, 1);

// save item to re-insert it later
Rasyen.removed_items.push({
'path' : arr,
'str' : str,
'pos' : pos,
'list_name' : list_name
});
}
var pos = data.indexOf(str);
if(data instanceof Array && pos != -1) {
// Remove item
data.splice(pos, 1);

// save item to re-insert it later
Rasyen.removed_items.push({
'path' : arr,
'str' : str,
'pos' : pos,
'list_name' : list_name
});
}

if(do_rebuild){
@@ -424,7 +425,9 @@ var Rasyen = {
var fn = list.parsed_filters[n];
if(typeof this.filters[fn] === 'function'){
// Call filter
list = this.callback.parse_filters(this.filters[fn](list));
list = this.filters[fn](list);
// Callback
list = this.callback.parse_filters(list);
}
}
}
@@ -487,14 +490,14 @@ var Rasyen = {

// Array with lists in tag
var list_tags = cmd.split('|');
var output = [];
var result_pool = [];
for (var i = 0; i < list_tags.length; i++) {
var list = this.parse_list(list_tags[i]);
// output on every iteration because of filters
if(list.replace){
output.push(list.replace);
tag_obj.output = this.rai(output);
if(list.replace || list.replace === ''){
result_pool.push(list.replace);
tag_obj.output = this.rai(result_pool);
}
var fst = list_tags[i].charAt(0);
if( fst == '=' || fst == '[' || fst == '{'){
@@ -539,7 +542,7 @@ var Rasyen = {
parse : function(tpl){
var parsed_tpl = this.parse_template(tpl);
// After parsing the template proceed to replace its tags
if(parsed_tpl.hasOwnProperty('parsed_tags')){
for (var i = 0; i < parsed_tpl.parsed_tags.length; i++) {
@@ -582,4 +585,4 @@ var Rasyen = {
this.removed_items = [];
}
}
}
};

+ 42
- 28
tests.html View File

@@ -5,7 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width" />

<link rel="shortcut icon" href="http://patxipierce.com/img/favicons/favicon.ico"/>
<link rel="shortcut icon" href="http://patxipierce.com/assets/img/favicons/favicon.ico"/>

<title>RaSyEn - Random Syntax Engine Tests</title>

@@ -62,7 +62,7 @@
button { padding: 15px 0; border-radius: 4px; font-size: 140%; width: 75%; background-color: #111; color: #ddd; border: 1px solid #ddd; cursor: pointer; }
button:hover { background-color: #444; color: #fff; }
.test-button-box { text-align: center; position: fixed; bottom: 20px; left: 0; right: 0; width: 100%; }
.test-button-box { text-align: center; position: fixed; bottom: 0; left: 0; right: 0; width: 100%; background-color: #111; }
#list-data ul > li { list-style-type: none; }
@@ -134,8 +134,16 @@

<section>

<h2>Tests demonstration - <span id="rasyen-version"></span></h2>
<h2>Tests and demonstrations - <span id="rasyen-version"></span></h2>

<p>This document is intended for testing the functionality of RaSyEn as well as demonstrating its usage.</p>

<article id="list-data">
<h3>List data in use:</h3>
<p>These are the lists used in all the tests below.</p>
<div id="lists"></div>
</article>

<article>
<h3>The tests:</h3>

@@ -146,16 +154,12 @@
<ul id="tests">
<li>
<h3>Callback test:</h3>
<p>Every time a callback is called we add to the total count of templates, tags, lists and filters called.</p>
<div class="result"><p>Templates: <span id="count-templates">0</span>, Tags: <span id="count-tags">0</span>, Lists: <span id="count-lists">0</span>, Filters: <span id="count-filters">0</span>.</p></div>
</li>
</ul>
</article>

<article id="list-data">
<h3>List data in use:</h3>

<div id="lists"></div>
</article>
</section>

<footer>
@@ -281,52 +285,52 @@
},
"category-test" : {
name : "Category test",
description : "",
description : "Search for a specific property of a sub-divided list and return a value from it.",
template : "%geography@water% %geography@land%"
},
"concatenation-test" : {
name : "Concatenation test",
description : "",
template : "%number|digit% %geography@water|geography@land%"
description : "Join two lists or more and return one of the pooled results.",
template : "%number|digit|person% %geography@water|geography@land%"
},
"filter-test" : {
name : "Upper and Lower Case Filter test",
description : "",
description : "Apply some basic modifications to the template results.",
template : "%geography=to-upper=first-to-lower% %geography=to-lower=first-to-upper%"
},
"filter-param-test" : {
name : "Filter per Words test",
description : "",
description : "The words filter applied one or more filter for every word in the template.",
template : "%kung-fu=words=first-to-upper%"
},
"auto-filter-test" : {
name : "Automatic Filter test",
description : "",
description : "A custom filter named \"color\" has been defined to be called every time the color list is used.",
template : "%color%"
},
"filter-save-test" : {
name : "Filter save-result, random-category and category test (uses parameters)",
description : "",
name : "Filter save-result, random-category and category test",
description : "Saves a category name to a list called 'saved-key' and the uses it to retrieve a result",
template : "%geography=random-category=save-result=saved-key% %geography=category=saved-key%"
},
"mixed-save-filter-test" : {
name : "Save-result with several mixed lists (uses parameters)",
name : "Save-result with several mixed lists",
description : "Mix two lists and expect the result of both to be the same.",
template : "%geography|digit|number|=save-result=number-or-digit% == %number-or-digit%"
},
"filter-alone-test" : {
name : "Filter alone test (and including percent signs in templates)",
description : "",
name : "Filter alone test",
description : "Sans-list filter, and a way to include percent signs in templates.",
template : "%=range=2-24% &#37;"
},
"filter-remove-result" : {
name : "Remove result filter test (last one should not work, because the list only has three items)",
description : "",
template : "%number=remove-result% %number=remove-result% %number=remove-result% %number%"
name : "Remove result filter test",
description : "Results cannot be the same, because they have been removed from the list when used.",
template : "%number=remove-result% =/= %number=remove-result% =/= %number=remove-result%"
},
"tag-meta-test" : {
name : "Tag meta filter test",
description : "",
description : "Get a result from the animal list, and parse it again, and again until no tags are found, then add 'a' or 'an' (from en_US-filters) before the result.",
template : "Its %animal=meta=a-or-an%"
},
"filter-inline-test" : {
@@ -334,20 +338,25 @@
description : "A filter that requires no list but can save a list.",
template : "%[\"Hello\",\"Hey\",\"Greetings\"]=inline=inline-list%, said Arthur. %inline-list%, replied Guinevere."
},
"plural-singular-filter-test" : {
"filter-plural-singular-test" : {
name : "Plural to singular test (en_US-filters)",
description : "A very simple (and not very thorough) function convert from singular to plural and back.",
template : "%nouns=to-plural=save-result=plu% %plu=to-singular%"
},
"pronoun-swap-test" : {
"filter-pronoun-swap-test" : {
name : "Pronoun swap test (en_US-filters)",
description : "Searches for the first pronoun in a phrase and changes to a designated pronoun in an entire phrase.",
template : "\"%opinion=save-result=op%\" + \"%pronoun|person|=save-result=pn%\" = \"%op=pronoun-swap=pn%\""
},
"number-to-words-test" : {
"filter-number-to-words-test" : {
name : "Number to Words test (en_US-filters)",
description : "Converts digits (up to 10^303) to their word equivalent.",
template : "%=range=100-10000=save-result=n% = %n=number-to-words%"
},
"filter-quiet-test" : {
name : "Test the quiet filer",
description : "Will output nothing but still parse any filters before the quiet filter was called.",
template : "%geography=save-result=n=quiet% %n%"
}
};
}
@@ -366,11 +375,16 @@
var li = document.createElement('li');
li.id = tpl;
// Title div
// Title h3
var h3 = document.createElement('h3');
h3.innerHTML = templates[tpl].name;
li.appendChild(h3);

// description p
var p = document.createElement('p');
p.innerHTML = templates[tpl].description;
li.appendChild(p);

// Template div
var div_template = document.createElement('div');
div_template.className = 'template';


Loading…
Cancel
Save