Skip to content

Instantly share code, notes, and snippets.

@iamdustan
Created April 11, 2012 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamdustan/2356549 to your computer and use it in GitHub Desktop.
Save iamdustan/2356549 to your computer and use it in GitHub Desktop.
Concerning blacksmith static site generator losing the doctype through the generatation process.
/* ~ line 122 +
* First attempt to add the doctype was to hardcode it in place of smith.window.document.doctype.
* This added the doctype to the output argument of each renderer, but that didn’t apply to what was written to disk.
*/
smith.renderers.forEach( function (renderer) {
// Grabs the name given to the generator.
var name = Object.keys(renderer)[0];
smith.log.info('Rendering "'+name+'"');
try {
// Perform a weld on the div.
renderer[name].weld(div, smith.content[name]);
// Write the content to disk.
//
// Note: jsdom strips doctypes. If we were using full doms we could
// access the doctype for each individual page, but
// because we're using the div to contain our document this
// information never gets added to jsdom.
//
// The doctype for jsdom gets set explicitly during content loading.
// smith.window.document.doctype
renderer[name].generate(
'<!DOCTYPE html>' // smith.window.document.doctype
+ '/n'
+ div.innerHTML, smith.content[name]
);
} catch (err) {
smith.log.error( 'Problem rendering "' + name + '"' );
String(err.stack).split("\n").forEach(smith.log.error);
cb(1);
}
});
/* ~ line 393
* here is the if statement added for doctype checking.
* it’s pretty non-intrusive. Just make sure a doctype exists.
* also of note to https://github.com/nodejitsu/docs/issues/49
* not having a doctype will trigger quirksmode in IE absolutely
* destroying the web.
*/
content.generate = function(output, pages) {
// Write all the welded pages to disk.
Object.keys(pages).forEach(function (file) {
var newPath = file.replace(path.resolve(smith.src), path.resolve(smith.dst)),
content = pages[file].content;
newPath = path.normalize(newPath + '/index.html');
if (content) {
if (typeof content !== 'string' && !Buffer.isBuffer(content)) {
smith.log.warn('Content from '+file+' is of type '+(typeof content));
}
smith.log.debug("Writing " + newPath);
// test if a doctype exists in output content. if not, prepend HTML5 doctype to content.
if( !/<!DOCTYPE /gi.test(pages[file].content) ) {
pages[file].content = '<!DOCTYPE html>' + pages[file].content
}
fs2.writeFile(newPath, pages[file].content, function (err) {
if (err) {
smith.log.error('Error while writing '+newPath+' to disk:');
throw err;
}
});
} else {
// If a file is handled by a different renderer, the content may get
// written later.
smith.log.silly("No content for " + newPath);
}
});
return pages;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment