Skip to content

Commit

Permalink
0.2.1 improved error log message
Browse files Browse the repository at this point in the history
  • Loading branch information
micnic committed Dec 22, 2015
1 parent c2b9259 commit 4eb9be8
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.2.1
- Improved error log message
- In the server-side version cache only files with the specified extension
- `print()` and `printx()` functions will print empty string for undefined arguments

## 0.2.0
- Added `recache` for dynamic caching
- Added syntax for XML escaped strings (`printx()`)
Expand Down
6 changes: 3 additions & 3 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ options: object

callback: function(engine: object)

simpleT uses a configuration object to set up the template engine. The configuration is applied on all templates rendered using this instance of simpleT. All templates are cached, to remove a template from the cache '.clear()' method should be used. In the server-side version of simpleT two additional arguments can be added, these are `directory` and `callback`, by providing these arguments simpleT will, internally, read, cache the files, and compile the templates for further use, the `callback` function will be triggered when all files are ready for usage. In `options` argument custom syntax for tags can be specified by modifying the `open` and `close` properties, which by default are `<%` and `%>`, another option is `extension`, it adds a suffix to the file names stored in the cache, this is useful, mainly in the server-side version and allow to not specify the extension while requiring templates. Each instance of simpleT has a context of values which is applied to all rendered templates, these values are defined in the `globals` options, functions have access to the internal functions like `this.include()`, `this.print()` and `this.printx()`. There is also a debug mode, in which templates more informations in case there are some errors, this mode is enabled by the option `debug`. Debug mode should be used only in the development process, because the templates compiled in debug mode are slower in compilation and execution.
simpleT uses a configuration object to set up the template engine. The configuration is applied on all templates rendered using this instance of simpleT. All templates are cached, to remove a template from the cache '.clear()' method should be used. In the server-side version of simpleT two additional arguments can be added, these are `directory` and `callback`, by providing these arguments simpleT will, internally, read, cache the files, and compile the templates for further use, the `callback` function will be triggered when all files are ready for usage. In `options` argument custom syntax for tags can be specified by modifying the `open` and `close` properties, which by default are `<%` and `%>`, another option is `extension`, it adds a suffix to the file names stored in the cache, this is useful, mainly in the server-side version (where only files with the defined extension are cached) and allow to not specify the extension while requiring templates. Each instance of simpleT has a context of values which is applied to all rendered templates, these values are defined in the `globals` options, functions have access to the internal functions like `this.include()`, `this.print()` and `this.printx()`. There is also a debug mode, in which templates more informations in case there are some errors, this mode is enabled by the option `debug`. Debug mode should be used only in the development process, because the templates compiled in debug mode are slower in compilation and execution.

```javascript
var engine = simplet({
Expand Down Expand Up @@ -44,7 +44,7 @@ To isolate the code from the rest of the content of the template the open and th

### Print Tag

To print some data it is necessary to use the open tag followed by an `-` or `=` symbol. The `print()` function is used to print the characters as they are, while the `printx()` function will print the characters and will escape XML characters like `"`, `&`, `'`, `<` and `>`
To print some data it is necessary to use the open tag followed by an `-` or `=` symbol. The `print()` function is used to print the characters as they are, while the `printx()` function will print the characters and will escape XML characters like `"`, `&`, `'`, `<` and `>`. If an undefined argument is provided then an empty string will be prited.

```
// Simple print
Expand All @@ -64,7 +64,7 @@ To print some data it is necessary to use the open tag followed by an `-` or `=`

### Include Tag

To insert another template inside the current template it is necessary to use the open tag followed by an `@` symbol. The path to the included template has to be defined relative to the path of the current template.
To insert another template inside the current template it is necessary to use the open tag followed by an `@` symbol. The path to the included template has to be defined relative to the path of the current template. The included template will have access to the imported values of the current template.

```
<%@ '/path/to/template.ejs' %> ||
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<img src="https://raw.github.com/micnic/simpleT/master/logo.png"/>

# 0.2.0
# 0.2.1

simpleT is a simple template engine for Node.JS and web browsers that has some special features:

Expand Down
83 changes: 52 additions & 31 deletions client/simplet.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,46 +85,62 @@ var simplet = function (options) {
});
};

// Catch error and prepare an expanded result if in debug mode
simplet.catchError = function (template, debug, error) {

var result;
var end = 0,
original = template.original,
result = 'Template Error\n',
start = 0;

if (debug && !(error instanceof SyntaxError)) {
var start = Math.max(0, Math.min(error.line - 2, template.length - 5)),
length = String(start + 5).length;
// Prepare final result structure
if (debug) {

result = template.slice(start, start + 5).map(function (line, index) {
result += template.location + ':' + (error.line + 1) + '\n\n';

var current = start + index + 1,
result = '';
// Check for runtime errors to prepare expanded result
if (!(error instanceof SyntaxError)) {

if (current === error.line) {
result += ' > ';
} else {
result += ' ';
}
// Prepare the start and the end position of the template fragment
start = Math.max(0, Math.min(error.line - 2, original.length - 5));
end = Math.min(start + 5, original.length);

if (String(current).length < length) {
result += ' ';
}
// Get the template fragment and mark the error line
result += original.slice(start, end).map(function (line, index) {

result += current + ' | ' + line.slice(0, 80 - length - 3);
var current = start + index,
count = String(current + 1);

return result;
}).join('\n');
}
// Check for the length of the line index to align it
if (count.length < String(end).length) {
count = ' ' + count;
}

if (result) {
result = error.name + ': ' + error.message + '\n' + result;
} else if (debug) {
result = error.name + ': ' + error.message;
// Add the indicator of the error line
if (current === error.line) {
count = '> ' + count;
} else {
count = ' ' + count;
}

return count + ' | ' + line.slice(0, 80 - count.length);
}).join('\n') + '\n\n';
}

// Check if there is a stack defined for this error to add it
if (error.stack) {
result += error.stack;
} else {
result += error.name + ': ' + error.message;
}
} else {
result = 'An error occured, enable debug mode for expanded result';
result += 'enable debug mode for expanded result';
}

return result;
};

// Normalize location path to be used in the internal cache
simplet.normalizePath = function (location, extension) {

var isAbsolute = (location[0] === '/'),
Expand Down Expand Up @@ -231,6 +247,8 @@ var simplet = function (options) {
// Stingify non-string arguments and concatenate all arguments
if (typeof arguments[index] === 'string') {
result += arguments[index];
} else if (arguments[index] === undefined) {
result += '';
} else {
result += JSON.stringify(arguments[index]);
}
Expand All @@ -252,6 +270,8 @@ var simplet = function (options) {
// Stingify non-string arguments and concatenate all arguments
if (typeof arguments[index] === 'string') {
result += simplet.escapeXML(arguments[index]);
} else if (arguments[index] === undefined) {
result += '';
} else {
result += simplet.escapeXML(JSON.stringify(arguments[index]));
}
Expand Down Expand Up @@ -302,7 +322,7 @@ var simplet = function (options) {
try {
Function(args, template.compiled).apply(context, values);
} catch (error) {
result = simplet.catchError(template.original, this.debug, error);
result = simplet.catchError(template, this.debug, error);
}
} else if (this.debug) {
result = 'Template "' + source + '" was not found';
Expand All @@ -321,7 +341,7 @@ var simplet = function (options) {
csize = close.length,
current = content[0],
index = 0,
line = 1,
line = 0,
open = this.open,
osize = open.length,
result = '',
Expand All @@ -330,7 +350,7 @@ var simplet = function (options) {

// Add the line index at the beginning for error handling in debug mode
if (this.debug) {
result += 'var _l_=1;try{';
result += 'var _l_=0;try{';
}

// Parse char by char
Expand All @@ -350,7 +370,7 @@ var simplet = function (options) {
}

// Add the line index before the code block
if (this.debug && line > 1) {
if (this.debug && line > 0) {
result += '_l_=' + line + ';';
}

Expand Down Expand Up @@ -405,7 +425,7 @@ var simplet = function (options) {
current = content[index];
}

// Trim redundant whitespace
// Remove redundant whitespace
result += buffer.trim();

// Close current statement
Expand Down Expand Up @@ -463,12 +483,12 @@ var simplet = function (options) {
buffer = '';
}

// Remove any redundant whitespace in the beginning or the end of the result
// Remove redundant whitespace
result = result.trim();

// Add the ending for the error handling in debug mode
if (this.debug) {
result += '}catch(e){throw{line:_l_,message:e.message,name:e.name}}';
result += '}catch(e){e.line=_l_;throw e}';
}

// Normalize location
Expand All @@ -477,6 +497,7 @@ var simplet = function (options) {
// Save the original and the compiled content of the template
this.container[location] = {
compiled: result,
location: location,
original: content.split(/\r\n|\n|\r|\u2028|\u2029/)
};
};
Expand Down
4 changes: 2 additions & 2 deletions client/simplet.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4eb9be8

Please sign in to comment.