Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from lemieux/master
Added coffeescript source and some improvements
- Loading branch information
Showing
4 changed files
with
185 additions
and
178 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |