@@ -11,6 +11,8 @@ lines it enables developers to transform sources in pretty much every way possib
1111 <img src="https://raw.github.com/dcodeIO/MetaScript/master/example.jpg" />
1212</p >
1313
14+ [ ![ Donate] ( https://raw.githubusercontent.com/dcodeIO/MetaScript/master/donate.png )] ( https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=info%40code-emitter.com&item_name=Open%20Source%3A%20MetaScript )
15+
1416How does it work?
1517-----------------
1618If you already know JavaScript, adding some meta is as simple as remembering that:
@@ -32,11 +34,6 @@ Let's assume that you have a library and that you want its version number to be
3234MyLibrary .VERSION = /* ?== VERSION */ ;
3335// or, alternatively, if VERSION is always string-safe:
3436MyLibrary .VERSION = " /*?= VERSION */" ;
35- // or, alternatively, if you don't mind a missing trailing semicolon:
36- MyLibrary .VERSION = // ?== VERSION
37- // or, alternatively, if you like it procedural:
38- MyLibrary .VERSION = /* ? write(JSON.stringify(VERSION)) */ ;
39- // etc.
4037```
4138
4239This is what the meta program, when compiled, will look like:
@@ -61,7 +58,17 @@ Advanced examples
6158Of course it's possible to do much more with it, like declaring macros and defining an entire set of useful utility
6259functions, just like with any sort of preprocessor:
6360
64- #### That's a globally available utility function:
61+ #### That's a globally available utility function as a snippet:
62+
63+ ``` js
64+ // ?...
65+ includeFile = function (file ) {
66+ write (indent (require (" fs" ).readFileSync (file)), __);
67+ }
68+ // ?.
69+ ```
70+
71+ or, as a block:
6572
6673``` js
6774/* ? includeFile = function(file) {
@@ -75,12 +82,12 @@ Using it:
7582// ? includeFile("some/other/file.js")
7683```
7784
78- #### That's a globally available macro:
85+ #### That's a globally available macro using inline blocks :
7986
8087``` js
8188// ? ASSERT_OFFSET = function(varname) {
8289 if (/* ?= varname */ < 0 || /* ?= varname */ > this .capacity ()) {
83- throw ( new RangeError (" Illegal /*?= varname */" ) );
90+ throw RangeError (" Illegal /*?= varname */" );
8491 }
8592// ? }
8693```
@@ -99,28 +106,13 @@ Results in:
99106``` js
100107function writeInt8 (value , offset ) {
101108 if (offset < 0 || offset > this .capacity ()) {
102- throw ( new RangeError (" Illegal offset" ) );
109+ throw RangeError (" Illegal offset" );
103110 }
104111 ...
105112}
106113```
107114
108- #### And that's a snippet with both the utility function and macro from above:
109-
110- ``` js
111- // ?...
112- includeFile = function (file ) {
113- write (indent (require (" fs" ).readFileSync (file)), __);
114- };
115-
116- ASSERT_OFFSET = function (varname ) {
117- writeln (__+ ' if (' + varname+ ' < 0 || ' + varname+ ' > this.capacity()) {' );
118- writeln (__+ ' throw(new RangeError("Illegal ' + varname+ ' "));' );
119- };
120- // ?.
121- ```
122-
123- Some early examples are available in the [ tests folder] ( https://github.com/dcodeIO/MetaScript/tree/master/tests ) . While
115+ Some examples are available in the [ tests folder] ( https://github.com/dcodeIO/MetaScript/tree/master/tests ) . While
124116these are JavaScript examples, MetaScript should fit nicely with any other programming language that uses ` // ... ` and
125117` /* ... */ ` style comments.
126118
@@ -166,18 +158,25 @@ There are a few quite useful utility functions available to every meta program:
166158
167159* ** write(contents: string )**
168160 Writes some raw data to the resulting document, which is equal to using ` /*?= contents */ ` .
161+
169162* ** writeln(contents: string )**
170163 Writes some raw data, followed by a line break, to the resulting document, which is equal to using ` //?= __+contents ` .
164+
171165* ** dirname(filename: string )**
172166 Gets the directory name from a file name.
167+
173168* ** include(filename: string , absolute: boolean =)**
174169 Includes another source file or multiple ones when using a glob expression. ` absolute ` defaults to ` false ` (relative)
170+
175171* ** indent(str: string , indent: string |number): string ** indents a block of text using the specified indentation given
176172 either as a whitespace string or number of whitespaces to use.
173+
177174* ** escapestr(str: string ): string **
178175 Escapes a string to be used inside of a single or double quote enclosed JavaScript string.
176+
179177* ** snip()**
180178 Begins a snipping operation at the current offset of the output.
179+
181180* ** snap(): string **
182181 Ends a snipping operation, returning the (suppressed) output between the two calls to ` snip() ` and ` snap() ` .
183182
@@ -186,10 +185,14 @@ Most notably there is the variable [__](https://github.com/dcodeIO/MetaScript/wi
186185remembers the current indentation level. This is used for example to indent included sources exactly like the meta block
187186that contains the include call.
188187
188+ Using utility dependencies
189+ --------------------------
190+ In case this isn't obvious: Add the dependency to your package.json and use ` //? myutility = require('metascript-myutility') ` .
191+
189192Documentation
190193-------------
191194* [ Get additional insights at the wiki] ( https://github.com/dcodeIO/MetaScript/wiki )
192- * [ View the API documentation] ( http://htmlpreview.github.com/?http ://github. com/dcodeIO/MetaScript/master/docs/MetaScript .html )
195+ * [ View the API documentation] ( http://htmlpreview.github.io/?https ://raw.githubusercontent. com/dcodeIO/MetaScript/master/docs/index .html )
193196* [ View the tiny but fully commented source] ( https://github.com/dcodeIO/MetaScript/blob/master/MetaScript.js )
194197
195198** License:** Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html
0 commit comments