Skip to content

Commit

Permalink
Also check missing transclusion destinations in all activity files. #2
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatsushita committed Sep 18, 2017
1 parent c4f3144 commit b1c1723
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
45 changes: 31 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function plugin(options) {
// Get index content
var index = minimatch(key, 'exercises/*/index.md') ? file.contents.toString() : null;

// Sanity checks
// Sanity check: Verify that activity index structure is as expected
var fields = ['summary', 'approach', 'materials_needed', 'operational_security', 'instructions', 'recommendations'];

var includes_regexp = /^!INCLUDE\s"(.*)\.md"/gm;
Expand All @@ -63,22 +63,39 @@ function plugin(options) {
while (match = includes_regexp.exec(index)) {
matches.push(match[1]);
}matches.forEach(function (match) {
if (!fields.includes(match)) console.warn('Unknown field ' + match + ' in file ' + key);
if (!fields.includes(match)) console.warn('Unexpected transclusion ' + match + ' in activity index file ' + key);
});

// Match title

var title = index && index.match(/^####\s(.*)$/m) ? { title: index.match(/^####\s(.*)$/m)[1] } : null;

var processTransclusions = function processTransclusions(str) {
return trimNewlines(str.toString().replace(includes_regexp, ':[]($1.md)'));
};

// Sanity check: Check if transclusion destinations exist.

var transclusionRE = /\:\[\]\((.*)\)/gm;
var destLinks = void 0;

while ((destLinks = transclusionRE.exec(processTransclusions(file.contents))) !== null) {
try {
fs.openSync(path.join(metalsmith.source(), 'exercises', key.split('exercises/')[1].split('/')[0], destLinks[1]), fs.constants.O_RDONLY);
} catch (e) {
console.log(`Missing transclusion destination in ${key}:`, path.join(metalsmith.source(), 'exercises', key.split('exercises/')[1].split('/')[0], destLinks[1]));
}
}

// Add included files as metadata on activity object.

var summary = minimatch(key, 'exercises/*/summary.md') ? { summary: trimNewlines(file.contents.toString()) } : null;
var summary = minimatch(key, 'exercises/*/summary.md') ? { summary: processTransclusions(file.contents) } : null;

var approach = minimatch(key, 'exercises/*/approach.md') ? { approach: trimNewlines(file.contents.toString()) } : null;
var materials = minimatch(key, 'exercises/*/materials_needed.md') ? { materials: trimNewlines(file.contents.toString()) } : null;
var opsec = minimatch(key, 'exercises/*/operational_security.md') ? { opsec: trimNewlines(file.contents.toString()) } : null;
var instructions = minimatch(key, 'exercises/*/instructions.md') ? { instructions: trimNewlines(file.contents.toString()) } : null;
var recommendations = minimatch(key, 'exercises/*/recommendations.md') ? { recommendations: trimNewlines(file.contents.toString()) } : null;
var approach = minimatch(key, 'exercises/*/approach.md') ? { approach: processTransclusions(file.contents) } : null;
var materials = minimatch(key, 'exercises/*/materials_needed.md') ? { materials: processTransclusions(file.contents) } : null;
var opsec = minimatch(key, 'exercises/*/operational_security.md') ? { opsec: processTransclusions(file.contents) } : null;
var instructions = minimatch(key, 'exercises/*/instructions.md') ? { instructions: processTransclusions(file.contents) } : null;
var recommendations = minimatch(key, 'exercises/*/recommendations.md') ? { recommendations: processTransclusions(file.contents) } : null;

var id = {
id: key.split('exercises/')[1].split('/')[0].replace(/_/g, '-')
Expand All @@ -100,16 +117,16 @@ function plugin(options) {

// Sanity check: Check if transclusion destinations exist.

var transclusionRE = /\:\[\]\((.*)\)/gm;
var destLinks = void 0;
var _transclusionRE = /\:\[\]\((.*)\)/gm;
var _destLinks = void 0;

while ((destLinks = transclusionRE.exec(_contents)) !== null) {
while ((_destLinks = _transclusionRE.exec(_contents)) !== null) {
// We skip activities as they are linked to methods via the taxonomy
if (!destLinks[1].includes('/activities/')) {
if (!_destLinks[1].includes('/activities/')) {
try {
fs.openSync(path.join(metalsmith.source(), 'methods', destLinks[1]), fs.constants.O_RDONLY);
fs.openSync(path.join(metalsmith.source(), 'methods', _destLinks[1]), fs.constants.O_RDONLY);
} catch (e) {
console.log(`Missing transclusion destination in ${key}:`, path.join(metalsmith.source(), 'methods', destLinks[1]));
console.log(`Missing transclusion destination in ${key}:`, path.join(metalsmith.source(), 'methods', _destLinks[1]));
}
}
}
Expand Down
38 changes: 30 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function plugin(options) {
// Get index content
const index = minimatch(key, 'exercises/*/index.md') ? file.contents.toString() : null;

// Sanity checks
// Sanity check: Verify that activity index structure is as expected
const fields = [
'summary',
'approach',
Expand All @@ -62,33 +62,55 @@ function plugin(options) {
while ((match = includes_regexp.exec(index))) matches.push(match[1]);

matches.forEach(match => {
if (!fields.includes(match)) console.warn('Unknown field ' + match + ' in file ' + key);
if (!fields.includes(match))
console.warn('Unexpected transclusion ' + match + ' in activity index file ' + key);
});

// Match title

const title = index && index.match(/^####\s(.*)$/m) ? { title: index.match(/^####\s(.*)$/m)[1] } : null;

const processTransclusions = str => trimNewlines(str.toString().replace(includes_regexp, ':[]($1.md)'));

// Sanity check: Check if transclusion destinations exist.

const transclusionRE = /\:\[\]\((.*)\)/gm;
let destLinks;

while ((destLinks = transclusionRE.exec(processTransclusions(file.contents))) !== null) {
try {
fs.openSync(
path.join(metalsmith.source(), 'exercises', key.split('exercises/')[1].split('/')[0], destLinks[1]),
fs.constants.O_RDONLY
);
} catch (e) {
console.log(
`Missing transclusion destination in ${key}:`,
path.join(metalsmith.source(), 'exercises', key.split('exercises/')[1].split('/')[0], destLinks[1])
);
}
}

// Add included files as metadata on activity object.

const summary = minimatch(key, 'exercises/*/summary.md')
? { summary: trimNewlines(file.contents.toString()) }
? { summary: processTransclusions(file.contents) }
: null;

const approach = minimatch(key, 'exercises/*/approach.md')
? { approach: trimNewlines(file.contents.toString()) }
? { approach: processTransclusions(file.contents) }
: null;
const materials = minimatch(key, 'exercises/*/materials_needed.md')
? { materials: trimNewlines(file.contents.toString()) }
? { materials: processTransclusions(file.contents) }
: null;
const opsec = minimatch(key, 'exercises/*/operational_security.md')
? { opsec: trimNewlines(file.contents.toString()) }
? { opsec: processTransclusions(file.contents) }
: null;
const instructions = minimatch(key, 'exercises/*/instructions.md')
? { instructions: trimNewlines(file.contents.toString()) }
? { instructions: processTransclusions(file.contents) }
: null;
const recommendations = minimatch(key, 'exercises/*/recommendations.md')
? { recommendations: trimNewlines(file.contents.toString()) }
? { recommendations: processTransclusions(file.contents) }
: null;

const id = {
Expand Down

0 comments on commit b1c1723

Please sign in to comment.