Skip to content

Commit

Permalink
Merge pull request jsdoc#233 from matthewkastor/Fix-for-issue-#228-Pr…
Browse files Browse the repository at this point in the history
…etty-Source

Adds Pretty Printed Source Code To Docs
  • Loading branch information
matthewkastor committed Oct 19, 2012
2 parents e36cb0f + 838c4aa commit b05e3bf
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 0 deletions.
114 changes: 114 additions & 0 deletions plugins/prettyPrintSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* @file Adds pretty printed source code to
* the output files.
* @author <a href="mailto:[email protected]">Matthew Christopher Kastor-Inare III</a>
* Hial Atropa!!
* @version 20121017
*/

exports.handlers = {
beforeParse: function(e) {

function bs2fs(text) {
return text.replace(/\\/g, '/');
}

function toRelativePath(text) {
return text.replace(/^.*:/, '');
}

function slashesCollapseToDots(text) {
return text.replace(/[\/\\]+/g, '.');
}

function suffixFsCollapse(text) {
return text.replace(/\/+$/, '/');
}

function noDotPrefix(text) {
return text.replace(/^\./, '');
}

function makeOutputFileName(sourceFileName, extension) {
extension = extension || '';

var out;

out = toRelativePath(sourceFileName);
out = slashesCollapseToDots(out);
out = noDotPrefix(out)
out += extension;

return out;
}

function getOutputDirectory() {
var path = require('path');
var out;

out = path.resolve(env.opts.destination);
out = bs2fs(out);
out = suffixFsCollapse(out + '/');

return out;
}

function generateHighlightedSourceFile(outDir, outfile, sourceContent) {
var fs = require('fs');
var outsource;

outsource = '<!DOCTYPE html>\n';
outsource += ' <head>\n';
outsource += ' <meta charset="utf-8">\n';
outsource += ' <title>Source of : ' + outfile + '</title>\n';
outsource += ' <style type="text/css">\n';
outsource += ' pre, code\n';
outsource += ' {\n';
outsource += ' white-space: pre-wrap;\n';
outsource += ' }\n';
outsource += ' </style>\n';
outsource += ' <script src="scripts/prettify/prettify.js"> </script>\n';
outsource += ' <!--[if lt IE 9]>\n';
outsource += ' <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>\n';
outsource += ' <![endif]-->\n';
outsource += ' <link type="text/css" title="desert" rel="stylesheet" href="styles/desert.css">\n';
outsource += ' <link type="text/css" title="sunburst" rel="alternate stylesheet" href="styles/sunburst.css">\n';
outsource += ' <link type="text/css" title="prettify" rel="alternate stylesheet" href="styles/prettify.css">\n';
outsource += ' <link type="text/css" title="prettify-jsdoc" rel="alternate stylesheet" href="styles/prettify-jsdoc.css">\n';
outsource += ' </head>\n';
outsource += ' <body>\n';
outsource += ' <pre class="prettyprint lang-js linenums">\n';
outsource += sourceContent;
outsource += ' </pre>\n';
outsource += ' <script>prettyPrint();</script>\n';
outsource += ' </body>\n'
outsource += '</html>\n';

fs.mkPath(outDir);
fs.writeFileSync(outfile, outsource);
}

function main() {

var sourceFileName= bs2fs(e.filename);
var sourceContent = e.source;
var outFileName = makeOutputFileName(sourceFileName, '.html');
var outDir = getOutputDirectory();
var outfile = outDir + outFileName;

/*// Debug
print('source file name = ' + sourceFileName);
print('output file name = ' + outFileName);
print('output directory = ' + outDir);
print('output file = ' + outfile);
print('sourceContent = ' + sourceContent);
//*/

// write source to file with a unique name
generateHighlightedSourceFile(outDir, outfile, sourceContent);
// link to source in documentation somehow
}

main();
}
};
34 changes: 34 additions & 0 deletions templates/default/static/styles/desert.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* desert scheme ported from vim to google prettify */
pre { display: block; background-color: #333 }
pre .nocode { background-color: none; color: #000 }
pre .str { color: #ffa0a0 } /* string - pink */
pre .kwd { color: #f0e68c; font-weight: bold }
pre .com { color: #87ceeb } /* comment - skyblue */
pre .typ { color: #98fb98 } /* type - lightgreen */
pre .lit { color: #cd5c5c } /* literal - darkred */
pre .pun { color: #fff } /* punctuation */
pre .pln { color: #fff } /* plaintext */
pre .tag { color: #f0e68c; font-weight: bold } /* html/xml tag - lightyellow */
pre .atn { color: #bdb76b; font-weight: bold } /* attribute name - khaki */
pre .atv { color: #ffa0a0 } /* attribute value - pink */
pre .dec { color: #98fb98 } /* decimal - lightgreen */

/* Specify class=linenums on a pre to get line numbering */
ol.linenums { margin-top: 0; margin-bottom: 0; color: #AEAEAE } /* IE indents via margin-left */
li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8 { list-style-type: none }
/* Alternate shading for lines */
li.L1,li.L3,li.L5,li.L7,li.L9 { }

@media print {
pre { background-color: none }
pre .str, code .str { color: #060 }
pre .kwd, code .kwd { color: #006; font-weight: bold }
pre .com, code .com { color: #600; font-style: italic }
pre .typ, code .typ { color: #404; font-weight: bold }
pre .lit, code .lit { color: #044 }
pre .pun, code .pun { color: #440 }
pre .pln, code .pln { color: #000 }
pre .tag, code .tag { color: #006; font-weight: bold }
pre .atn, code .atn { color: #404 }
pre .atv, code .atv { color: #060 }
}
52 changes: 52 additions & 0 deletions templates/default/static/styles/prettify.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* Pretty printing styles. Used with prettify.js. */

/* SPAN elements with the classes below are added by prettyprint. */
.pln { color: #000 } /* plain text */

@media screen {
.str { color: #080 } /* string content */
.kwd { color: #008 } /* a keyword */
.com { color: #800 } /* a comment */
.typ { color: #606 } /* a type name */
.lit { color: #066 } /* a literal value */
/* punctuation, lisp open bracket, lisp close bracket */
.pun, .opn, .clo { color: #660 }
.tag { color: #008 } /* a markup tag name */
.atn { color: #606 } /* a markup attribute name */
.atv { color: #080 } /* a markup attribute value */
.dec, .var { color: #606 } /* a declaration; a variable name */
.fun { color: red } /* a function name */
}

/* Use higher contrast and text-weight for printable form. */
@media print, projection {
.str { color: #060 }
.kwd { color: #006; font-weight: bold }
.com { color: #600; font-style: italic }
.typ { color: #404; font-weight: bold }
.lit { color: #044 }
.pun, .opn, .clo { color: #440 }
.tag { color: #006; font-weight: bold }
.atn { color: #404 }
.atv { color: #060 }
}

/* Put a border around prettyprinted code snippets. */
pre.prettyprint { padding: 2px; border: 1px solid #888 }

/* Specify class=linenums on a pre to get line numbering */
ol.linenums { margin-top: 0; margin-bottom: 0 } /* IE indents via margin-left */
li.L0,
li.L1,
li.L2,
li.L3,
li.L5,
li.L6,
li.L7,
li.L8 { list-style-type: none }
/* Alternate shading for lines */
li.L1,
li.L3,
li.L5,
li.L7,
li.L9 { background: #eee }
51 changes: 51 additions & 0 deletions templates/default/static/styles/sunburst.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Pretty printing styles. Used with prettify.js. */
/* Vim sunburst theme by David Leibovic */

pre .str, code .str { color: #65B042; } /* string - green */
pre .kwd, code .kwd { color: #E28964; } /* keyword - dark pink */
pre .com, code .com { color: #AEAEAE; font-style: italic; } /* comment - gray */
pre .typ, code .typ { color: #89bdff; } /* type - light blue */
pre .lit, code .lit { color: #3387CC; } /* literal - blue */
pre .pun, code .pun { color: #fff; } /* punctuation - white */
pre .pln, code .pln { color: #fff; } /* plaintext - white */
pre .tag, code .tag { color: #89bdff; } /* html/xml tag - light blue */
pre .atn, code .atn { color: #bdb76b; } /* html/xml attribute name - khaki */
pre .atv, code .atv { color: #65B042; } /* html/xml attribute value - green */
pre .dec, code .dec { color: #3387CC; } /* decimal - blue */

pre.prettyprint, code.prettyprint {
background-color: #000;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
-o-border-radius: 8px;
-ms-border-radius: 8px;
-khtml-border-radius: 8px;
border-radius: 8px;
}

pre.prettyprint {
width: 95%;
margin: 1em auto;
padding: 1em;
white-space: pre-wrap;
}


/* Specify class=linenums on a pre to get line numbering */
ol.linenums { margin-top: 0; margin-bottom: 0; color: #AEAEAE; } /* IE indents via margin-left */
li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8 { list-style-type: none }
/* Alternate shading for lines */
li.L1,li.L3,li.L5,li.L7,li.L9 { }

@media print {
pre .str, code .str { color: #060; }
pre .kwd, code .kwd { color: #006; font-weight: bold; }
pre .com, code .com { color: #600; font-style: italic; }
pre .typ, code .typ { color: #404; font-weight: bold; }
pre .lit, code .lit { color: #044; }
pre .pun, code .pun { color: #440; }
pre .pln, code .pln { color: #000; }
pre .tag, code .tag { color: #006; font-weight: bold; }
pre .atn, code .atn { color: #404; }
pre .atv, code .atv { color: #060; }
}

0 comments on commit b05e3bf

Please sign in to comment.