Last active
August 29, 2015 14:13
-
-
Save dapetcu21/c812272ee42a31f9a4c2 to your computer and use it in GitHub Desktop.
LFA 0.5 -> 0.6 format conversion tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var path = require('path'); | |
var yaml = require('js-yaml'); | |
var fs = require('fs'); | |
var filewalker = require('filewalker'); | |
var nodefn = require('when/node'); | |
var _ = require('lodash'); | |
var project = path.resolve(process.argv[2] || '.'); | |
console.log(project); | |
var configYml = fs.readFileSync(path.join(project, 'config.yml'), 'utf8'); | |
var yml = yaml.safeLoad(configYml); | |
var name = yml.book_id; | |
if (!name) { | |
name = path.basename(project) | |
.toLowerCase() | |
.replace(/[^a-z0-9]+/, '-') | |
.replace(/(^-|-$)/, ''); | |
} | |
var title = yml.title; | |
if (!title) { | |
title = path.basename(project); | |
} | |
var json = { | |
name: name, | |
book: { | |
title: title, | |
}, | |
keywords: ['lfa-book'], | |
projectPath: '..', | |
}; | |
if (yml.language) { | |
json.book.language = yml.language; | |
} | |
if (yml.text_direction) { | |
json.book.textDirection = yml.text_direction; | |
} | |
try { | |
fs.mkdirSync(path.join(project, '.lfa')); | |
} catch (ex) {} | |
fs.writeFileSync(path.join(project, '.lfa', 'package.json'), JSON.stringify(json, null, 2)); | |
fs.unlinkSync(path.join(project, 'config.yml')); | |
function parseJade(data) { | |
data = data.toString().split('\n'); | |
var filter1 = []; | |
var filter2 = []; | |
var insideSection, insideArticle; | |
function article(line1, line2) { | |
var r = []; | |
if (line1) { | |
r.push('+title(' + JSON.stringify(line1) + ')'); | |
} | |
if (line2) { | |
r.push('+subtitle(' + JSON.stringify(line2) + ')'); | |
} | |
return r; | |
} | |
insideArticle = false; | |
_.each(data, function(line) { | |
if (insideArticle && !/^\( [^ ].*|\s*\)$|/.test(line)) { | |
insideArticle = false; | |
} | |
if (insideArticle) { | |
filter1.push(line.replace(/^ /, '')); | |
} else { | |
if (/^\+article/.test(line)) { | |
insideArticle = true; | |
_.each(eval(line.replace(/^\+/, '')), function (l) { | |
filter1.push(l); | |
}); | |
} else { | |
filter1.push(line); | |
} | |
} | |
}); | |
var sectionCount = 0; | |
_.each(filter1, function (line) { | |
if (/\+section/.test(line)) { | |
sectionCount++; | |
} | |
}); | |
if (sectionCount === 1) { | |
insideSection = false; | |
_.each(filter1, function(line) { | |
if (insideSection && !/^\( [^ ].*|\s*\)$|/.test(line)) { | |
insideSection = false; | |
} | |
if (insideSection) { | |
filter2.push(line.replace(/^ /, '')); | |
} else { | |
if (/\+section/.test(line)) { | |
insideSection = true; | |
} else { | |
filter2.push(line); | |
} | |
} | |
}); | |
} else { | |
_.each(filter1, function(line) { | |
filter2.push(line.replace(/\+section.*$/, 'section')); | |
}); | |
} | |
return filter2.join('\n'); | |
} | |
filewalker(path.join(project, 'text'), { matchRegExp: /\.jade$/, }) | |
.on('file', function (filePath) { | |
filePath = path.join(project, 'text', filePath); | |
nodefn.call(fs.readFile, filePath).then(function (data) { | |
return nodefn.call(fs.writeFile, filePath, parseJade(data)); | |
}); | |
}) | |
.walk(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "lfa-convert", | |
"version": "1.0.0", | |
"description": "LFA 0.5 -> 0.6 conversion tool", | |
"main": "convert.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"bin": { | |
"lfa-convert": "./convert.js" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "https://gist.github.com/c812272ee42a31f9a4c2.git" | |
}, | |
"author": "Marius Petcu <[email protected]>", | |
"license": "MPLv2", | |
"dependencies": { | |
"filewalker": "^0.1.2", | |
"js-yaml": "^3.2.5", | |
"lodash": "^2.4.1", | |
"when": "^3.6.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment