Skip to content

Spell Creation

Samarjeet edited this page Sep 27, 2017 · 1 revision

Stupefy spells are mere handlebars templates, which reside in the $spells directory, i.e. ~/spells (default) or as configured in ~/.stupefy.json.

Stupefy defines the variable lang_{common_file_extension} to identify the programming language of the code from which the spell was enchanted.

This allows the same spell which does the same thing, but using the different standard libraries of various programming languages so that you focus on the idea and not the language.

Variables passed to the spell can be accessed using {{ var_name }}. If statements and for loops are also supported as in Handlebars.

Examples

1. Bubble sort

{{#if lang_cpp}}
for (int i = 0;i < {{ size }};i++)
     for(int j = 0;j < {{ size }};j++)
          if ({{ array }}[j] > {{ array }}[j + 1])
               swap({{ array }}[j], {{ array }}[j+1]);
{{else if lang_js}}
for (var i = 0;i < {{ array }}.length;i++)
     for (var j = 0;j < {{ array }}.length;j++)
          if ({{array}}[j] > {{array}}[j+1])
               swap({{ array }}[j], {{ array }}[j+1]);
{{/if}}

If this is saved to $spells/bubbleSort, it can be used as-

  • In Javascript
var a = [ 8, 9, 1, 2 ];
/* enchant bsort, array: 'a' */
a.forEach(console.log);
  • In C++
int main(void) {
     int a[] = { 9, 3, 4, 1 };
     int n = sizeof(a)/sizeof(int);
     /* enchant bubbleSort, array: 'a', size: 'n' */
     cout << "Sorted array ->";
     for (int i = 0;i < n;i++)
          cout << a[i] << ' ';
}     

2. Navigation Menu

<nav class="navbar navbar-default">
	<div class="container-fluid">
		<!-- Brand and toggle get grouped for better mobile display -->
		<div class="navbar-header">
			<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
				<span class="sr-only">Toggle navigation</span>
				<span class="icon-bar"></span>
				<span class="icon-bar"></span>
				<span class="icon-bar"></span>
			</button>
			<a class="navbar-brand" href="#">Brand</a>
		</div>

		<!-- Collect the nav links, forms, and other content for toggling -->
		<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
			<ul class="nav navbar-nav navbar-right">
				{{#each items}}
					<li><a href="#">{{ this }}</a></li>
				{{/each}}
			</ul>
		</div><!-- /.navbar-collapse -->
	</div><!-- /.container-fluid -->
</nav>

If this is saved to $spells/bootstrap_nav, it can be used as

<body>
<!-- enchant boostrap_nav, items: [ 'Home', 'Name' ] -->
</body>

See the demo below asciicast

Go ahead, create your own spellbook now!

Clone this wiki locally