Skip to content

Commit

Permalink
Merge pull request #1 from lemieux/master
Browse files Browse the repository at this point in the history
Added coffeescript source and some improvements
  • Loading branch information
mgan59 committed Mar 7, 2012
2 parents 0900726 + e68ffb8 commit 34cd15d
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 178 deletions.
23 changes: 23 additions & 0 deletions README.markdown
Expand Up @@ -26,6 +26,29 @@ You can specify a output file name if you don't want it to inherit the original

Will generate a file called super_doc.html in the local folder

# Variables
The following variables can be used when calling the tool as the following

docdown --markdown my_doc.markdown --title "New project specs"

<table>
<thead>
<td> <strong>Variable</strong> </td>
<td> <strong>Description</strong> </td>
<td> <strong>Default value</strong> </td>
</thead>
<tr>
<td>title</td>
<td>Title in the generated HTML</td>
<td>DocDown Generated File</td>
</tr>
<tr>
<td>author</td>
<td>Value in the meta tag <strong>author</strong> in the head of the document</td>
<td> <em>None</em> </td>
</tr>
</table>

# Todo

Eventually I'll add support for custom templates for the html, currently a really basic white template is used. I have the hooks in place for the cmd-line arguments to almost work.
Expand Down
119 changes: 6 additions & 113 deletions layouts/standard.html

Large diffs are not rendered by default.

91 changes: 91 additions & 0 deletions lib/docdown.coffee
@@ -0,0 +1,91 @@
###
DocDown
A command-line utility to convert markdown files to HTML
###

fs = require 'fs'
path = require 'path';
markdownParser = require( '../node_modules/markdown-js/lib/markdown').markdown;
argv = require('../node_modules/optimist').argv;

exports.run = () ->
#Handle Markdown source determinizim
markdownInput = ''
if argv.markdown
#need to parse the file-name
# see if it has a .markdown extension
# then create the base-file-name [test.markdown -> test.html]
markdownInput = argv.markdown
else
# pretty much error out without a markdown source
throw new Error 'Failed to specify a markdown source'


#Handle layout specification
layout_template = ''
if argv.template
#in here support a file toss
#or template name...
#maybe do a template name, to prevent user/client error
throw new Error('Failed to identify a layout source... However, at this moment we have no customized layouts');
else
layout_template = '../layouts/standard.html'

#Handle output path
outputPath = ''
if argv.output
#if output param specified use it as filename output
# should do a check and make sure it is a valid syntaxfiletype
outputPath = argv.output;
else
#if no output specified use the markdown.source filename but tear off the .markdown
# and replace with html
file_name_parts = markdownInput.split '.'
outputPath = "#{file_name_parts[0]}.html"

htmlReplacement = {}

htmlReplacement['__title__'] = ''
#Handle title used in the html template
if argv.title
htmlReplacement['__title__'] = argv.title
else
#If no title param specified, use the default one
htmlReplacement['__title__'] = 'DocDown Generated File';

#Handle author used in the html template
htmlReplacement['__author__'] = ''
if argv.author
htmlReplacement['__author__'] = argv.author
else
#If no author param specified, none is used.
htmlReplacement['__author__'] = '';

#sync call to markdown file
sourceMarkdownFile = fs.readFileSync markdownInput, 'utf-8'

#get layout template
layout_dir = path.dirname fs.realpathSync __filename ;
layout_template = layout_dir+'/'+layout_template;

# Read in a layout file, only one for now
layoutFile = fs.readFile layout_template, 'utf-8', (err, buff) ->
if err
throw err

htmlReplacement['__markdown__'] = markdownParser sourceMarkdownFile
#replace text
newDoc = buff

for key,value of htmlReplacement
#replacing all variables
newDoc = newDoc.replace key,value

fs.writeFile outputPath, newDoc, (err) ->
if err
throw err
console.log "Document Generated -> #{outputPath}"
return
return


130 changes: 65 additions & 65 deletions lib/docdown.js
@@ -1,67 +1,67 @@
(function(){

var fs = require('fs');
var path = require('path');
var markdownParser = require('../node_modules/markdown-js/lib/markdown').markdown;
var argv = require('../node_modules/optimist').argv;

exports.run = function(){
//Handle Markdown source determinizim
var markdownInput = '';
if (argv.markdown){
//need to parse the file-name
// see if it has a .markdown extension
// then create the base-file-name [test.markdown -> test.html]
markdownInput = argv.markdown;
} else {
// pretty much error out without a markdown source
throw new Error('Failed to specify a markdown source');
(function() {
/*
DocDown
A command-line utility to convert markdown files to HTML
*/
var argv, fs, markdownParser, path;
fs = require('fs');
path = require('path');
markdownParser = require('../node_modules/markdown-js/lib/markdown').markdown;
argv = require('../node_modules/optimist').argv;
exports.run = function() {
var file_name_parts, htmlReplacement, layoutFile, layout_dir, layout_template, markdownInput, outputPath, sourceMarkdownFile;
markdownInput = '';
if (argv.markdown) {
markdownInput = argv.markdown;
} else {
throw new Error('Failed to specify a markdown source');
}
layout_template = '';
if (argv.template) {
throw new Error('Failed to identify a layout source... However, at this moment we have no customized layouts');
} else {
layout_template = '../layouts/standard.html';
}
outputPath = '';
if (argv.output) {
outputPath = argv.output;
} else {
file_name_parts = markdownInput.split('.');
outputPath = "" + file_name_parts[0] + ".html";
}
htmlReplacement = {};
htmlReplacement['__title__'] = '';
if (argv.title) {
htmlReplacement['__title__'] = argv.title;
} else {
htmlReplacement['__title__'] = 'DocDown Generated File';
}
htmlReplacement['__author__'] = '';
if (argv.author) {
htmlReplacement['__author__'] = argv.author;
} else {
htmlReplacement['__author__'] = '';
}
sourceMarkdownFile = fs.readFileSync(markdownInput, 'utf-8');
layout_dir = path.dirname(fs.realpathSync(__filename));
layout_template = layout_dir + '/' + layout_template;
return layoutFile = fs.readFile(layout_template, 'utf-8', function(err, buff) {
var key, newDoc, value;
if (err) {
throw err;
}
htmlReplacement['__markdown__'] = markdownParser(sourceMarkdownFile);
newDoc = buff;
for (key in htmlReplacement) {
value = htmlReplacement[key];
newDoc = newDoc.replace(key, value);
}
fs.writeFile(outputPath, newDoc, function(err) {
if (err) {
throw err;
}

//Handle layout specification
var layout_template = '';
if(argv.template){
//in here support a file toss
//or template name...
//maybe do a template name, to prevent user/client error
throw new Error('Failed to identify a layout source... However, at this moment we have no customized layouts');
} else {
layout_template = '../layouts/standard.html';
}

//Handle output path
var outputPath = '';
if(argv.output){
//if output param specified use it as filename output
// should do a check and make sure it is a valid syntaxfiletype
outputPath = argv.output;
} else {
//if no output specified use the markdown.source filename but tear off the .markdown
// and replace with html
var file_name_parts = markdownInput.split('.');
outputPath = file_name_parts[0]+'.html';
}

//sync call to markdown file
var sourceMarkdownFile = fs.readFileSync(markdownInput, 'utf-8');

//get layout template
var layout_dir = path.dirname(fs.realpathSync(__filename));
layout_template = layout_dir+'/'+layout_template;

// Read in a layout file, only one for now
var layoutFile = fs.readFile(layout_template, 'utf-8', function(err, buff){
if(err) throw err;

var replaceText = markdownParser(sourceMarkdownFile);
//replace text
var newDoc = buff.replace('__markdown__', replaceText);

fs.writeFile(outputPath, newDoc, function(err){
if(err) throw err;
console.log('Document Generated -> '+outputPath);
});

});
};
console.log("Document Generated -> " + outputPath);
});
});
};
}).call(this);

0 comments on commit 34cd15d

Please sign in to comment.