Skip to content

Commit 325ed81

Browse files
committed
Add section parsing and classification
1 parent a2a1951 commit 325ed81

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lib/is_exercise.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var _ = require('lodash');
2+
3+
function isExercise(nodes) {
4+
var codeType = { type: 'code' };
5+
6+
// Number of code nodes in section
7+
var len = _.filter(nodes, codeType).length;
8+
9+
return (
10+
// Got 3 or 4 code blocks
11+
(len === 3 || len === 4) &&
12+
// Ensure all nodes are at the end
13+
_.all(_.last(nodes, len), codeType)
14+
);
15+
}
16+
17+
module.exports = isExercise;

lib/sections.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var _ = require('lodash');
2+
3+
// Split a page up into sections (lesson, exercises, ...)
4+
function split(nodes) {
5+
var section = [];
6+
7+
return _(nodes)
8+
.reduce(, function(sections, el) {
9+
if(el.type === 'hr') {
10+
sections.push(section);
11+
section = [];
12+
section.links = nodes.links || {};
13+
} else {
14+
section.push(el);
15+
}
16+
17+
return sections;
18+
}, [])
19+
// Add remaining nodes
20+
.concat([section])
21+
// Exclude empty sections
22+
.filter(_.negate(_.isEmpty))
23+
// Spit out JS array
24+
.value();
25+
}
26+
27+
function join(sections) {
28+
var nodes = _.reduce(sections, function(nodes, section) {
29+
return nodes.concat(section);
30+
}, []);
31+
nodes.links = (sections[0] && (sections[0].links) || {};
32+
return nodes;
33+
}
34+
35+
module.exports = {
36+
split: split,
37+
join: split,
38+
};

0 commit comments

Comments
 (0)