Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

support raw text

  • Loading branch information...
commit 8189dfd642ad6bb473a148c63fec67b68e23fea4 1 parent 92da485
Grockit authored
Showing with 90 additions and 15 deletions.
  1. +13 −2 pipedent.coffee
  2. +15 −3 pipedent.js
  3. +32 −3 tests/test.coffee
  4. +30 −7 tests/test.js
15 pipedent.coffee
View
@@ -144,10 +144,21 @@ convert_widget_package = (s) ->
if key == 'HTML'
block_size = IndentationHelper.get_indented_block prefix.length, indented_lines
block = indented_lines.shift_slice(block_size)
+ IndentationHelper.eat_empty_lines(block)
+ if block.len() > 0
+ buffer = output()
+ html = HTML(buffer.append)
+ html.branch_method(block)
+ obj[key] = buffer.text()
+ else
+ block_size = IndentationHelper.get_indented_block prefix.length, indented_lines
+ block = indented_lines.shift_slice(block_size)
buffer = output()
- html = HTML(buffer.append)
- html.branch_method(block)
+ while block.len() > 0
+ [prefix, line] = block.shift()
+ buffer.append prefix+line
obj[key] = buffer.text()
+
parse(s, parser)
obj
18 pipedent.js
View
@@ -184,16 +184,28 @@
var obj, parser;
obj = {};
parser = function(indented_lines) {
- var block, block_size, buffer, html, key, line, prefix, _ref;
+ var block, block_size, buffer, html, key, line, prefix, _ref, _ref2;
IndentationHelper.eat_empty_lines(indented_lines);
_ref = indented_lines.shift(), prefix = _ref[0], line = _ref[1];
key = line;
if (key === 'HTML') {
block_size = IndentationHelper.get_indented_block(prefix.length, indented_lines);
block = indented_lines.shift_slice(block_size);
+ IndentationHelper.eat_empty_lines(block);
+ if (block.len() > 0) {
+ buffer = output();
+ html = HTML(buffer.append);
+ html.branch_method(block);
+ return obj[key] = buffer.text();
+ }
+ } else {
+ block_size = IndentationHelper.get_indented_block(prefix.length, indented_lines);
+ block = indented_lines.shift_slice(block_size);
buffer = output();
- html = HTML(buffer.append);
- html.branch_method(block);
+ while (block.len() > 0) {
+ _ref2 = block.shift(), prefix = _ref2[0], line = _ref2[1];
+ buffer.append(prefix + line);
+ }
return obj[key] = buffer.text();
}
};
35 tests/test.coffee
View
@@ -12,10 +12,19 @@ assert_equal = (expected, actual, msg) ->
console.warn 'ACTUAL'
console.warn actual
for line, i in expected.split("\n")
- if line != actual.split("\n")[i]
+ actual_line = actual.split("\n")[i]
+ if line != actual_line
console.log "****", line
+ console.log "----", actual_line
+ console.log ""
throw "fail"
+indent = (s, indent) ->
+ lines = s.split("\n")
+ lines = lines.map (line) ->
+ if line == '' then line else indent + line
+ lines.join("\n")
+
run_test = (test) ->
expected = test.output + "\n"
actual = convert(test.input)
@@ -23,23 +32,43 @@ run_test = (test) ->
assert_equal expected, actual, msg
run_package_test = (test) ->
- expected = test.output + "\n"
+ expected = indent(test.output, ' ')
actual = convert_widget_package(test.input)[test.key]
msg = "test: #{test.use_case}"
assert_equal expected, actual, msg
run_package_test
- use_case: "Basic"
+ use_case: "Basic HTML"
input: \
'''
HTML
p | hello
+ b | world
'''
key: \
'HTML'
output: \
'''
<p>hello</p>
+ <b>world</b>
+ '''
+
+run_package_test
+ use_case: "Basic CSS"
+ input: \
+ '''
+ CSS
+ #square {
+ background: red
+ }
+ '''
+ key: \
+ 'CSS'
+ output: \
+ '''
+ #square {
+ background: red
+ }
'''
run_test
37 tests/test.js
View
@@ -1,11 +1,11 @@
(function() {
- var assert_equal, convert, convert_widget_package, fs, pipedent, run_package_test, run_test;
+ var assert_equal, convert, convert_widget_package, fs, indent, pipedent, run_package_test, run_test;
pipedent = require("../pipedent");
fs = require('fs');
convert = pipedent.convert;
convert_widget_package = pipedent.convert_widget_package;
assert_equal = function(expected, actual, msg) {
- var i, line, _len, _ref;
+ var actual_line, i, line, _len, _ref;
if (expected.trim() !== actual.trim()) {
console.warn(msg);
console.warn('=====');
@@ -16,11 +16,28 @@
_ref = expected.split("\n");
for (i = 0, _len = _ref.length; i < _len; i++) {
line = _ref[i];
- if (line !== actual.split("\n")[i]) console.log("****", line);
+ actual_line = actual.split("\n")[i];
+ if (line !== actual_line) {
+ console.log("****", line);
+ console.log("----", actual_line);
+ console.log("");
+ }
}
throw "fail";
}
};
+ indent = function(s, indent) {
+ var lines;
+ lines = s.split("\n");
+ lines = lines.map(function(line) {
+ if (line === '') {
+ return line;
+ } else {
+ return indent + line;
+ }
+ });
+ return lines.join("\n");
+ };
run_test = function(test) {
var actual, expected, msg;
expected = test.output + "\n";
@@ -30,16 +47,22 @@
};
run_package_test = function(test) {
var actual, expected, msg;
- expected = test.output + "\n";
+ expected = indent(test.output, ' ');
actual = convert_widget_package(test.input)[test.key];
msg = "test: " + test.use_case;
return assert_equal(expected, actual, msg);
};
run_package_test({
- use_case: "Basic",
- input: 'HTML\n p | hello',
+ use_case: "Basic HTML",
+ input: 'HTML\n p | hello\n b | world',
key: 'HTML',
- output: '<p>hello</p>'
+ output: '<p>hello</p>\n<b>world</b>'
+ });
+ run_package_test({
+ use_case: "Basic CSS",
+ input: 'CSS\n #square {\n background: red\n }',
+ key: 'CSS',
+ output: '#square {\n background: red\n}'
});
run_test({
use_case: "Let HTML pass through",
Please sign in to comment.
Something went wrong with that request. Please try again.