Skip to content

Commit

Permalink
Add tests for content and resultant fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
idmillington committed Sep 25, 2014
1 parent 9fa439b commit e445b9e
Show file tree
Hide file tree
Showing 3 changed files with 318 additions and 49 deletions.
98 changes: 51 additions & 47 deletions lib/parsers/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/

var featuresRe =
/\*+|\/\/\n|^[ \t]*\n|^\s*---\s*$|^\s*>>?|^\s*=|\[\??|\??\]|\{\?|\?\}/mg;
/\*+|\/\/\n|^[ \t]*\n|^\s*---\s*$|^\s*>>?|^\s*=|\[\??|\??\]|\{\!|\!\}/mg;

var _findFeatures = function(text) {
var results = [];
Expand Down Expand Up @@ -65,6 +65,24 @@
}
};

var simplePara = function(type, interstitial) {
assert (currentParagraphFeature !== null);
add(currentParagraphFeature, 'end');
if (interstitial !== undefined) {
add(interstitial, 'single');
}
currentParagraphFeature = type;
add(currentParagraphFeature, 'start');
};

var continuablePara = function(type) {
if (currentParagraphFeature === type) {
add('skip', 'single');
} else {
simplePara(type);
}
};

add('paragraph', 'start', 0, 0);
var currentParagraphFeature = 'paragraph';
var inConditional = false;
Expand All @@ -77,7 +95,7 @@
// Magic can only be ended by magic, anything else inside is
// considered part of the magic.
if (magicStarted !== null) {
if (feature.feature === '?}') {
if (feature.feature === '!}') {
// End of magic.
add('magic', 'end');
magicStarted = null;
Expand Down Expand Up @@ -111,54 +129,27 @@
if (inConditional) add('conditional', 'end');
else add('hidden', 'end', feature.start+1);
break;
case '{?':
case '{!':
// Start of magic (end is handled in a separate clause above).
add('magic', 'start');
magicStarted = feature.end;
break;

// New paragraph creation...
case '': // Blank line, new paragraph.
if (currentParagraphFeature !== null) {
add(currentParagraphFeature, 'end');
}
currentParagraphFeature = 'paragraph';
add(currentParagraphFeature, 'start');
simplePara('paragraph');
break;
case '---': // Horizontal rule, also forces a new paragraph.
if (currentParagraphFeature !== null) {
add(currentParagraphFeature, 'end');
}
add('hrule', 'single');
currentParagraphFeature = 'paragraph';
add(currentParagraphFeature, 'start');
simplePara('paragraph', 'hrule');
break;
case '>': // This paragraph is a quote.
if (currentParagraphFeature !== 'quotation') {
if (currentParagraphFeature !== null) {
add(currentParagraphFeature, 'end');
}
currentParagraphFeature = 'quotation';
add(currentParagraphFeature, 'start');
}
continuablePara('quotation');
break;
case '>>': // This paragraph is a quote attribution.
if (currentParagraphFeature !== 'attribution') {
if (currentParagraphFeature !== null) {
add(currentParagraphFeature, 'end');
}
currentParagraphFeature = 'attribution';
add(currentParagraphFeature, 'start');
}
continuablePara('attribution');
break;
case '=': // This paragraph is a heading (level one or two).
if (currentParagraphFeature !== 'heading') {
if (currentParagraphFeature !== null) {
add(currentParagraphFeature, 'end');
}
currentParagraphFeature = 'heading';
add(currentParagraphFeature, 'start');
}
continuablePara('heading');
break;
}
}
Expand All @@ -167,11 +158,8 @@
};

var _buildRanges = function(text, features) {
var top = function(array) {
return array[array.length - 1];
};
var add = function(range) {
top(stack).content.push(range);
_.last(stack).content.push(range);
};
var addText = function() {
if (lastPosition < feature.start) {
Expand All @@ -196,10 +184,12 @@
});
break;
case 'single':
add({type:feature.feature});
if (feature.feature !== 'skip') {
add({type:feature.feature});
}
break;
case 'end':
assert(top(stack).type === feature.feature);
assert(_.last(stack).type === feature.feature);
add(stack.pop());
break;
}
Expand All @@ -211,16 +201,25 @@

var tidy = function(ranges) {
var result = [];
_.each(ranges, function(range) {
for (var i = 0; i < ranges.length; ++i) {
var range = ranges[i];
if (range.content === undefined) {
result.push(range);
// Try to merge raw text.
var lastPos = result.length - 1;
if (lastPos >= 0 &&
range.type === undefined &&
result[lastPos].type === undefined) {
result[lastPos] = result[lastPos] + ' ' + range;
} else {
result.push(range);
}
} else {
range.content = tidy(range.content);
if (range.content.length > 0) {
result.push(range);
}
}
});
}
return result;
};

Expand Down Expand Up @@ -273,7 +272,12 @@
// Compile the predicates
var fns = async.map(predicates, function(predicate, done) {
if (predicate.type === 'magic') {
var fn = engine.makeFunctionFromSource(predicate.source);
var fn;
try {
fn = engine.makeFunctionFromSource(predicate.source);
} catch(err) {
return done(err);
}
return done(null, fn);
} else {
assert(predicate.type === 'logic');
Expand All @@ -297,7 +301,7 @@
};

// ------------------------------------------------------------------------

/*
var toHTML = function(ranges) {
var result = [];
_.each(ranges, function(range) {
Expand Down Expand Up @@ -363,7 +367,7 @@
});
return result.join('');
};

*/

// ------------------------------------------------------------------------
// DEBUG
Expand Down
3 changes: 1 addition & 2 deletions lib/parsers/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,7 @@

var validateContent = function(property, callback) {
var val = propval(property);
if (val === undefined) callback(null, undefined);
else content.compile(val, callback);
content.compile(val, callback);
};

// ------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit e445b9e

Please sign in to comment.