Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Create sub-modules for the text modification functions
  • Loading branch information
mcwhittemore committed Mar 5, 2013
1 parent b1924a0 commit dde9b36
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 66 deletions.
2 changes: 0 additions & 2 deletions README.md
Expand Up @@ -81,9 +81,7 @@ Attempts to generate the html, css, javascript and data-uris needed to render th
* Update config
* add destination path and destination name
* remove unique_id
* --Update loadFiles to return a value, not just simply set something in the config.--
* Create sub-modules for the markdown to jade conversion functions
* Create sub-modules for the text modification functions
* Add in attribution code

### 0.2.0
Expand Down
153 changes: 89 additions & 64 deletions lib/deckmd.js
Expand Up @@ -4,40 +4,12 @@ var data_uri = require("data-uri");

var config = undefined;


var addTab = function(txt){
txt = txt.replace(/\n/g, "\n"+config.tab);
return config.tab+txt;
}

var addBack = function(txt, add, remove){
remove = remove || "";
add = add.replace(remove, "");
return add+txt;
}

var getFirstLine = function(md){
var index = md.indexOf("\n");
var out = md.substring(0, index);
return out.replace(/\n/gm, "");
}

var loadFiles = function(files){
var result = {};

for(var i=0; i<files.length; i++){
var file = files[i];
var content = fs.readFileSync(file, 'ascii');
result[file] = content;
}

return result;
}
var utils = require("./utils");

var getSections = function(md){
var sections = md.split(config.sections[0]);
for(var i=0; i<sections.length; i++){
sections[i] = addBack(sections[i], config.sections[0], "\n");
sections[i] = utils.addBack(sections[i], config.sections[0], "\n");
}
return sections;
}
Expand All @@ -52,7 +24,7 @@ var getSlides = function(md){
for(var j=0; j<s1.length; j++){
if(s1[j]!=""){
if(title_added){
s1[j] = addBack(s1[j], config.sections[1], "\n");
s1[j] = utils.addBack(s1[j], config.sections[1], "\n");
}
else{
title_added = true;
Expand All @@ -67,7 +39,7 @@ var getSlides = function(md){

//TURNS THE MARKDOWN FOR H1 SLIDES INTO JADE
var h1 = function(md){
var title = getFirstLine(md);
var title = utils.getFirstLine(md);
var subtitle = md.replace(title+"\n", "");
title = title.replace("# ", "");

Expand All @@ -83,7 +55,7 @@ var h1 = function(md){

out+=config.tab+config.tab+config.tab+":markdown\n";

out+=addTab(addTab(addTab(addTab(subtitle))))+"\n";
out+=utils.addTab(utils.addTab(utils.addTab(utils.addTab(subtitle))))+"\n";
}
else{
out+="\n";
Expand All @@ -95,11 +67,11 @@ var h1 = function(md){

//TURNS THE MARKDOWN FOR H2 SLIDES INTO JADE
var h2 = function(md){
var title = getFirstLine(md);
var title = utils.getFirstLine(md);

var out = "section.slide\n";
out+=config.tab+":markdown\n";
out+=addTab(addTab(md))+"\n";
out+=utils.addTab(utils.addTab(md))+"\n";

return out;
}
Expand Down Expand Up @@ -236,7 +208,34 @@ exports.renderFile = function(files, userConfig, callback){

exports.render = function(files, userConfig, callback){

/*
/************************************************************
************************ START CONFIG ***********************
************************************************************/

config = require("./baseConfig");

if(typeof userConfig == "function"){
callback = userConfig;
userConfig={};
}

var ucKeys = Object.keys(userConfig);
for(var i=0; i<ucKeys.length; i++){
var ucKey = ucKeys[i];
config[ucKey] = userConfig[ucKey];
}

utils.init(config);

/************************************************************
************************ END CONFIG *************************
*************************************************************
/************************************************************
*********************** FIX FILE OBJ ************************
*************************************************************
{
FILE_ID: {
path: "FILEPATH",
Expand All @@ -252,14 +251,8 @@ exports.render = function(files, userConfig, callback){
]
}
}
*/

config = require("./baseConfig");

if(typeof userConfig == "function"){
callback = userConfig;
userConfig={};
}
*************************************************************
*************************************************************/

if(typeof files == "string"){
files = {
Expand All @@ -286,11 +279,17 @@ exports.render = function(files, userConfig, callback){
files = tmp;
}

var md = "";
/************************************************************
************************* END FILE OBJ **********************
************************************************************/

var fileNames = Object.keys(files);
/************************************************************
************************ START MD CONCAT ********************
************************************************************/

var md = "";

var fileNames = Object.keys(files);

for(var i=0; i<fileNames.length; i++){

Expand All @@ -302,12 +301,6 @@ exports.render = function(files, userConfig, callback){

}

var ucKeys = Object.keys(userConfig);
for(var i=0; i<ucKeys.length; i++){
var ucKey = ucKeys[i];
config[ucKey] = userConfig[ucKey];
}

if(typeof callback=="undefined"){
callback = function(err, html, configsUsed){
console.log(html);
Expand All @@ -317,17 +310,26 @@ exports.render = function(files, userConfig, callback){
//add newline to start of file so getSections will grab the first line too
md = "\n"+md;

//break file into sections
var sections = getSections(md);
/************************************************************
************************* END MD CONCAT *********************
************************************************************/

/************************************************************
*********************** START MD TO JADE ********************
************************************************************/

var jd = "";

//break md into sections
var sections = getSections(md);

//break sections into slides
for(var i=0; i<sections.length; i++){
if(sections[i]!="# "){
var slides = getSlides(sections[i]);

if(config.title == undefined){
config.title = getFirstLine(slides[0]);
config.title = utils.getFirstLine(slides[0]);
config.title = config.title.replace(/^# /, "");
}

Expand All @@ -342,14 +344,41 @@ exports.render = function(files, userConfig, callback){
}
}

jd = "extends layout\n\nblock slides\n"+addTab(jd);
//add jade headers and add indentation
jd = "extends layout\n\nblock slides\n"+utils.addTab(jd);

//set all \t to use the config.tab string
jd = jd.replace(/\t/gm, config.tab);

//save jade if config asks us to
if(config.saveJade===true){
fs.writeFile(config.unique_id+".jade", jd, "utf8");
}

/************************************************************
*********************** END MD TO JADE **********************
************************************************************/

/************************************************************
*************** START CSS AND JS REFS TO TXT ****************
************************************************************/

config.cssFiles = utils.loadFiles(config.css);
config.coreCSSFiles = utils.loadFiles(config.coreCSS);

config.jsFiles = utils.loadFiles(config.js);
config.modernizrJSFiles = utils.loadFiles(config.modernizrJS);
config.jqueryJSFiles = utils.loadFiles(config.jqueryJS);
config.coreJSFiles = utils.loadFiles(config.coreJS);

/************************************************************
***************** END CSS AND JS REFS TO TXT ****************
************************************************************/

/************************************************************
********************* START JADE TO HTML ********************
************************************************************/

var jadeback = function(err, html){
if(err){
console.log(err);
Expand All @@ -360,14 +389,10 @@ exports.render = function(files, userConfig, callback){
}
}

config.cssFiles = loadFiles(config.css);
config.coreCSSFiles = loadFiles(config.coreCSS);

config.jsFiles = loadFiles(config.js);
config.modernizrJSFiles = loadFiles(config.modernizrJS);
config.jqueryJSFiles = loadFiles(config.jqueryJS);
config.coreJSFiles = loadFiles(config.coreJS);

jade.render(jd, {filename: __dirname+"/layout.jade", locals: config}, jadeback);

/************************************************************
********************** END JADE TO HTML *********************
************************************************************/

}
36 changes: 36 additions & 0 deletions lib/utils.js
@@ -0,0 +1,36 @@
var fs = require("fs");
var config = undefined;

module.exports.init = function(c){
config = c;
}


module.exports.addTab = function(txt){
txt = txt.replace(/\n/g, "\n"+config.tab);
return config.tab+txt;
}

module.exports.addBack = function(txt, add, remove){
remove = remove || "";
add = add.replace(remove, "");
return add+txt;
}

module.exports.getFirstLine = function(md){
var index = md.indexOf("\n");
var out = md.substring(0, index);
return out.replace(/\n/gm, "");
}

module.exports.loadFiles = function(files){
var result = {};

for(var i=0; i<files.length; i++){
var file = files[i];
var content = fs.readFileSync(file, 'ascii');
result[file] = content;
}

return result;
}

0 comments on commit dde9b36

Please sign in to comment.