Skip to content

Commit

Permalink
added lookahead regex and simplified slicing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jdan committed Feb 23, 2014
1 parent 8b31066 commit 13530d5
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions lib/cleaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ var yaml = require('js-yaml');
var mustache = require('mustache');
var helper;

function Slice(header, content) {
this.classList = (header ? header.replace('--', '').replace(/\s/g, ' ') : '').trim();
this.content = content;
}

function Cleaver(file) {
if (!file) {
throw '!! Please specify a file to parse';
Expand Down Expand Up @@ -241,33 +236,47 @@ Cleaver.prototype.renderAuthorSlide = function (content) {
* @return {Array.<string>} A list of all slides
*/
Cleaver.prototype.slice = function(document) {
var cuts = document.split(/(\r?\n--.*\r?\n)/);
var cuts = document.split(/\n(?=\-\-)/);
var slices = [];
var nlIndex;

for (var i = 0; i < cuts.length; i += 2) {
for (var i = 0; i < cuts.length; i++) {
/**
* EDGE CASE
* If the slideshow begins with `--`, that is, the metadata is empty and
* the user did *not* include a blank line at the top, we will insert an
* empty slide at the beginning (to represent empty metadata), followed
* by the first slice (with the -- chopped off).
* The first slide does not get the following treatment, so we just
* add it as content.
*
* Otherwise, we slice off the `--` at the beginning.
*/
if (i === 0 && cuts[i].match(/^--/)) {
slices.push(new Slice('', ''));
if (!cuts[i].match(/^--/)) {
slices.push({
content: cuts[i].trim()
});

continue;
} else {
/* If we leave out metadata, add an empty slide at the beginning */
if (i === 0) {
slices.push({ content: '' });
}

cuts[i] = cuts[i].slice(2);
}

/**
* CASE ONE: we have an ideally formatted cleaver markdown file;
* the author has one slide with metadata, followed by a series of delimiters
* and sections of content that follow; we just have to account for the empty
* delimiter before the meta data
*/
if (i === 0 && !cuts[i].match(/^--/)) {
cuts.unshift('');
* Slices at this point will contain class names, followed by several
* newlines
*/
nlIndex = cuts[i].indexOf('\n');
if (nlIndex === -1) {
// Just to be safe...
nlIndex = 0;
}

slices.push(new Slice(cuts[i], cuts[i+1].trim()));
/* Push the classList and markdown content */
slices.push({
classList: cuts[i].slice(0, nlIndex),
content: cuts[i].slice(nlIndex).trim()
});
}

return slices;
Expand Down

0 comments on commit 13530d5

Please sign in to comment.