-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Functionality examples for SugarCube (using Macro.Add) and Snowman (Underscore templates).
- Loading branch information
Dan Cox
authored and
Dan Cox
committed
Jun 20, 2018
1 parent
37db317
commit 74dbdba
Showing
9 changed files
with
321 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
addingfunctionality/snowman/snowman_adding_functionality.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# "Adding Functionality": Snowman (v1.3.0) | ||
|
||
## Summary | ||
|
||
Snowman does not provide [macros](../../terms/terms_macros.md). However, additional functionality can be added through the use of the [Underscore.js](https://underscorejs.org/) JavaScript library provided with Snowman. | ||
|
||
In this example, a global function, *showCurrentTime()*, is added to the *window.setup* object. It is called in a passage through using the interpolation functionality of [Underscore's template system](https://underscorejs.org/#template) to show a value. | ||
|
||
## Live Example | ||
|
||
<section> | ||
<iframe src="snowman_adding_functionality_example.html" height=400 width=90%></iframe> | ||
|
||
|
||
Download: <a href="snowman_adding_functionality_example.html" target="_blank">Live Example</a> | ||
</section> | ||
|
||
## Twee Code | ||
|
||
``` | ||
:: StoryTitle | ||
Adding Functionality in Snowman | ||
:: UserScript[script] | ||
// Use or create window.setup | ||
window.setup = window.setup || {}; | ||
// Create global function | ||
window.setup.showCurrentTime = function() { | ||
return new Date(); | ||
} | ||
:: Start | ||
The current time is <%= setup.showCurrentTime() %> | ||
``` | ||
|
||
Download: <a href="snowman_adding_functionality_twee.txt" target="_blank">Twee Code</a> |
46 changes: 46 additions & 0 deletions
46
addingfunctionality/snowman/snowman_adding_functionality_example.html
Large diffs are not rendered by default.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
addingfunctionality/snowman/snowman_adding_functionality_twee.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
:: StoryTitle | ||
Adding Functionality in Snowman | ||
|
||
:: UserScript[script] | ||
// Use or create window.setup | ||
window.setup = window.setup || {}; | ||
|
||
// Create global function | ||
window.setup.showCurrentTime = function() { | ||
return new Date(); | ||
} | ||
|
||
:: Start | ||
The current time is <%= setup.showCurrentTime() %> |
50 changes: 50 additions & 0 deletions
50
addingfunctionality/sugarcube/sugarcube_adding_functionality.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# "Adding Functionality": SugarCube (v2.18) | ||
|
||
## Summary | ||
|
||
In SugarCube, additional functionality can be added through the [*Macro.add()*](http://www.motoslave.net/sugarcube/2/docs/api-macro.html#macro-api) function. | ||
|
||
In this example, the [*Date()*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) JavaScript function is used to get the current time. This is saved to *payload.contents*, and the [*jQuery.wiki()*](http://www.motoslave.net/sugarcube/2/docs/object-methods.html#jquery-jqueryprotowiki) function is used to convert and append it to the current passage. | ||
|
||
## Live Example | ||
|
||
<section> | ||
<iframe src="sugarcube_adding_functionality_example.html" height=400 width=90%></iframe> | ||
|
||
|
||
Download: <a href="sugarcube_adding_functionality_example.html" target="_blank">Live Example</a> | ||
</section> | ||
|
||
## Twee Code | ||
|
||
``` | ||
:: StoryTitle | ||
Adding Functionality in SugarCube | ||
:: UserScript[script] | ||
Macro.add("currenttime", { | ||
tags: null, | ||
handler: function() { | ||
// Try the following code and catch any errors | ||
try { | ||
// Get the current time and save it to the payload | ||
this.payload.contents = new Date(); | ||
// Wikify (and append) the current payload contents | ||
jQuery(this.output).wiki(this.payload.contents); | ||
} | ||
catch (ex) { | ||
// Return any errors | ||
return this.error("Error: " + ex.message); | ||
} | ||
} | ||
}); | ||
:: Start | ||
<<currenttime>><</currenttime>> | ||
``` | ||
|
||
Download: <a href="sugarcube_adding_functionality_twee.txt" target="_blank">Twee Code</a> |
135 changes: 135 additions & 0 deletions
135
addingfunctionality/sugarcube/sugarcube_adding_functionality_example.html
Large diffs are not rendered by default.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
addingfunctionality/sugarcube/sugarcube_adding_functionality_twee.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
:: StoryTitle | ||
Adding Functionality in SugarCube | ||
|
||
:: UserScript[script] | ||
Macro.add("currenttime", { | ||
tags: null, | ||
handler: function() { | ||
// Try the following code and catch any errors | ||
try { | ||
|
||
// Get the current time and save it to the payload | ||
this.payload.contents = new Date(); | ||
|
||
// Wikify (and append) the current payload contents | ||
jQuery(this.output).wiki(this.payload.contents); | ||
|
||
} | ||
catch (ex) { | ||
// Return any errors | ||
return this.error("Error: " + ex.message); | ||
} | ||
} | ||
}); | ||
|
||
:: Start | ||
<<currenttime>><</currenttime>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters