WIP
This commit is contained in:
@@ -4,7 +4,6 @@ function OutputStream(options) {
|
||||
indent_level : 4,
|
||||
quote_keys : false,
|
||||
space_colon : false,
|
||||
beautify : true,
|
||||
ascii_only : false,
|
||||
inline_script : false,
|
||||
width : 80
|
||||
@@ -12,6 +11,7 @@ function OutputStream(options) {
|
||||
|
||||
var indentation = 0;
|
||||
var current_col = 0;
|
||||
var current_line = 0;
|
||||
var OUTPUT = "";
|
||||
|
||||
function to_ascii(str) {
|
||||
@@ -45,12 +45,9 @@ function OutputStream(options) {
|
||||
};
|
||||
|
||||
function print(str) {
|
||||
var nl = str.lastIndexOf("\n");
|
||||
if (nl >= 0) {
|
||||
current_col = nl;
|
||||
} else {
|
||||
current_col += str.length;
|
||||
}
|
||||
var a = str.split(/\r?\n/), n = a.length;
|
||||
current_line += n;
|
||||
current_col += a[n - 1].length;
|
||||
OUTPUT += str;
|
||||
};
|
||||
|
||||
@@ -71,12 +68,12 @@ function OutputStream(options) {
|
||||
function make_indent(line) {
|
||||
if (line == null)
|
||||
line = "";
|
||||
if (beautify)
|
||||
line = repeat_string(" ", options.indent_start + indentation) + line;
|
||||
line = repeat_string(" ", options.indent_start + indentation) + line;
|
||||
return line;
|
||||
};
|
||||
|
||||
function with_indent(col, cont) {
|
||||
if (col === true) col = next_indent();
|
||||
var save_indentation = indentation;
|
||||
indentation = col;
|
||||
var ret = cont();
|
||||
@@ -85,34 +82,31 @@ function OutputStream(options) {
|
||||
};
|
||||
|
||||
function indent() {
|
||||
if (options.beautify) print(make_indent());
|
||||
print(make_indent());
|
||||
};
|
||||
|
||||
function newline() {
|
||||
if (options.beautify) {
|
||||
print("\n");
|
||||
print(make_indent());
|
||||
}
|
||||
print("\n");
|
||||
};
|
||||
|
||||
function next_indent() {
|
||||
return indentation + options.indent_level;
|
||||
};
|
||||
|
||||
function with_block(cont) {
|
||||
function with_block(cont, beautify) {
|
||||
var ret;
|
||||
print("{");
|
||||
with_indent(next_indent(), function(){
|
||||
newline();
|
||||
if (beautify) newline();
|
||||
ret = cont();
|
||||
newline();
|
||||
if (beautify) newline();
|
||||
});
|
||||
indent();
|
||||
if (beautify) indent();
|
||||
print("}");
|
||||
return ret;
|
||||
};
|
||||
|
||||
function with_parens(cont) {
|
||||
function with_parens(cont, beautify) {
|
||||
print("(");
|
||||
var ret = with_indent(current_col, cont);
|
||||
print(")");
|
||||
@@ -128,7 +122,10 @@ function OutputStream(options) {
|
||||
with_indent : with_indent,
|
||||
with_block : with_block,
|
||||
with_parens : with_parens,
|
||||
options : function() { return options }
|
||||
options : function() { return options },
|
||||
line : function() { return current_line },
|
||||
col : function() { return current_col },
|
||||
pos : function() { return OUTPUT.length }
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user