Permalink
Browse files

Made ehx command line. Wrote more tests.

  • Loading branch information...
Robert Sköld
Robert Sköld committed Aug 13, 2009
1 parent 4c27c3f commit 374c620f06c88f7fc25d13a6c36f0483848ba2db
View
@@ -77,8 +77,16 @@ TODO
* column position
* stacktrace
* a small excerpt surrounding the "failed area" would be ideal.
-* Test if it's possible to add `this` as a context and allow hscript to access all public(?) properties of the class calling it.
-* Allow for adding more contexts (like if we have a "Helper"-class)
-* Test if a StringBuf is really faster than a regular String +=.
+* Allow for adding more contexts (like if we have a "Helper"-class besides the passed in context)
* Write more tests, more specific and bigger fixtures.
+* Add a command line version:
+ * May pass in a file path as an argument.
+ * May pass the text stream with STDIN (pipe-friendly).
+ * Returns the converted file to STDOUT (pipe-friendly).
+ * Need to be able to pass a "context" into it, maybe using hscript or json? -context or -ctx arguments, may be file paths or a direct string?.
+ * Can be run either by using the "ehx"-executable (nekotools boot) or haxelib run ehx (is it possible to pass arguments?)
+ * Problem with making an executable (atleast when using my macports): dyld: Library not loaded: @executable_path/../lib/libneko.dylib
+* To be able to ignore the output of the line with the <%%>, like with <% -%> in erb. For a cleaner output.
+* Allow to pass a context with JSON format (mostly to the executable).
+* Fix the problems with using the -ctx argument, the arguments get stripped of " and ' and is split on spaces. STDIN way still works though.
View
@@ -2,4 +2,17 @@
-debug
-neko public/tests.n
-lib hscript
--main tests.TestEhx
+-main tests.TestEhx
+
+--next
+-cp source
+-debug
+-neko public/testcontroller.n
+-lib hscript
+-main tests.TestController
+
+--next
+-cp source
+-neko public/ehx.n
+-lib hscript
+-main ehx.Ehx
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
@@ -1,5 +1,7 @@
package ehx;
+import neko.io.File;
+
class Ehx {
public static var DEBUG : Bool = false;
@@ -47,14 +49,15 @@ class Ehx {
inBlock = false;
} catch (ex:CmdError) {
switch( ex ) {
+ // TODO Can we analyze these errors more? I'd like to know where in the source they are.
case IncompleteStatement:
if( Ehx.DEBUG )
trace("Incomplete ... "); // continue prompt
inBlock = true;
case InvalidStatement:
- trace("Syntax error. " + ex);
+ throw("Syntax error. " + ex);
case InvalidCommand(cmd):
- trace("Execution error. " + cmd);
+ throw("Execution error. " + cmd);
}
}
try { str = r.matchedRight(); } catch( e : Dynamic ) {}
@@ -73,6 +76,110 @@ class Ehx {
}
return cmd;
}
+
+ static function main() {
+ trace( "Args:" + neko.Sys.args() );
+
+ var help = "ehx [-ctx context] inputfile
+
+Possible arguments:
+ -h,-? (or nothing) Show the list of arguments (you'know, the thing your reading now).
+ -v Verbose mode.
+ -c,-ctx,-context The context (think JSON) to use while rendering the inputfile.
+ If it's not a file it assumes it's \"inlined\".
+ A context must be within curly brackets \"{}\", ex. \"{ id: 12 }\".
+
+Usage examples:
+
+ 1. A simple load/parse of a text document file.
+ > ehx index.ehx
+
+ 2. Loading an ehx text document with an inlined context.
+ > ehx -ctx name=steve steve.ehx
+
+ STDIN will be the context if there's already an input file and no context set.
+
+ 3. Passing an ehx document through STDIN.
+ > echo \"<%= 'hello' %>\" | ehx
+
+ 4. Passing an ehx document through STDIN with an inline context.
+ > echo \"<%= 'hey there ' + name %>\" | ehx -ctx { name:\"steve\" }
+
+ 5. Using an external context.
+ > ehx -ctx context.hscript info.ehx
+
+ 6. Passing a context through STDIN.
+ > echo \"str='hej';num=123\" | neko public/ehx.n public/fixtures/mixed.ehx
+
+";
+
+ // No arguments or stdin, just show the help.
+ if( !hasStdIn() && neko.Sys.args().length == 0 ) {
+ neko.Lib.print( help );
+ return;
+ }
+
+ var arg, context = "", input = "";
+ var args = neko.Sys.args();
+ while( ( arg = args.shift() ) != null ) {
+ switch( arg ) {
+ case "-c","-ctx","-context":
+ var ctx = args.shift();
+ context = if( neko.FileSystem.exists( ctx ) ) neko.io.File.getContent( ctx ) else ctx;
+ case "-h","-help","-?":
+ neko.Lib.print( help );
+ return;
+ case "-v":
+ neko.Lib.println( "No verbose mode yet. But thanks for trying!" );
+ return;
+ default:
+ if( args.length == 0 && neko.FileSystem.exists( arg ) )
+ input = neko.io.File.getContent( arg );
+ else {
+ neko.Lib.println( "Invalid argument." );
+ neko.Lib.print( help );
+ return;
+ }
+ }
+ }
+
+ // If we have a stdin, an inputfile and no context the stdin is the context, otherwise it's the input.
+ if( hasStdIn() ) {
+ var stdin = neko.io.File.stdin().readAll().toString();
+ if( context.length == 0 && input.length > 0 )
+ context = stdin;
+ else
+ input = stdin;
+ }
+
+ trace( "context: " + context );
+ trace( "input: " + input );
+
+ if( input == "" ) {
+ neko.Lib.println( "Missing input." );
+ neko.Lib.print( help );
+ return;
+ }
+
+ // All tests have passed, do the magic!
+ var ehx = new Ehx();
+ var output = "";
+ try {
+ output = ehx.render( input , context );
+ } catch( e : Dynamic ) {
+ neko.io.File.stderr().writeString( Std.string( e ) );
+ }
+ trace( output );
+ neko.io.File.stdout().writeString( output );
+ }
+
+ static function hasStdIn() : Bool {
+ try {
+ return false;
+ } catch( e : Dynamic ) {
+ return true;
+ };
+ }
}
@@ -143,6 +250,11 @@ class CmdProcessor {
}
public function addContext( context : Dynamic ) {
+ if( Std.is( context , String ) ) {
+ // Attempt to parse it with hscript first.
+ context = interp.execute(parser.parseString(context));
+ }
+
for( field in Reflect.fields( context ) )
interp.variables.set( field , Reflect.field( context , field ) );
}
@@ -0,0 +1,56 @@
+package tests;
+
+typedef ListItem = {
+ var name : String;
+ var id : Int;
+}
+
+class TestController {
+ var string : String;
+ var float : Float;
+ var integer : Int;
+ var arr : Array<ListItem>;
+
+ function new( html ) {
+ string = "I am a little string.";
+ float = Math.PI;
+ integer = 0xFFFFFF;
+ arr = [
+ { name: "bob" , id: 123 },
+ { name: "nic" , id: 321 },
+ { name: "bill" , id: 213 }
+ ];
+
+
+
+ var e = new ehx.Ehx();
+ trace( e.render( html , this ) );
+ }
+
+ static function main() {
+
+ var html = "
+<html>
+ <head>
+ <title>TestController</title>
+ <head>
+ <body>
+ <h1>Some data from ehx:</h1>
+ <ul>
+ <li>String: <%= string %></li>
+ <li>Float: <%= float %></li>
+ <li>Integer: <%= integer %></li>
+ </ul>
+ <h2>List of ListItems:</h2>
+ <ul>
+ <% for( item in arr ) { %>
+ <li><%=item.id%> - <%=item.name%></li>
+ <% } %>
+ </ul>
+ </body>
+</html>
+";
+ var controller = new TestController(html);
+ }
+
+}

0 comments on commit 374c620

Please sign in to comment.