Permalink
Browse files

Replaced external Pygmentize with bundled one.

  • Loading branch information...
alexindigo committed Jan 20, 2014
1 parent 1bf0db9 commit 4a2a925024ce14474fe1279054234fd32d5f519d
Showing with 50 additions and 104 deletions.
  1. +2 −4 package.json
  2. +0 −14 scripts/install.js
  3. +0 −18 scripts/install.sh
  4. +48 −68 src/docker.js
View
@@ -5,7 +5,8 @@
"dependencies": {
"mkdirp": "0.3.2",
"commander": "0.5.2",
- "watchr": "~2.3.3"
+ "watchr": "~2.3.3",
+ "pygmentize-bundled": "~2.1.0"
},
"bin": {
"docker": "./docker"
@@ -19,8 +20,5 @@
"type": "git",
"url": "git://github.com/jbt/docker.git"
},
- "scripts": {
- "install": "node scripts/install.js"
- },
"main": "./src/docker.js"
}
View
@@ -1,14 +0,0 @@
-var os = require('os');
-
-if(os.type().toLowerCase().substring(0,3) == "win"){
- console.log("Remember to install Pygments for docker to work.");
- console.log("Check out http://pygments.org/ for instructions how to do this.");
-}else{
- var i = require('child_process').spawn('sh',['scripts/install.sh']);
- i.stdout.pipe(process.stdout, {end: false});
- process.stdin.resume();
- process.stdin.pipe(i.stdin, {end: false});
- i.on('exit', function(a){
- process.exit(a);
- });
-}
View
@@ -1,18 +0,0 @@
-#!/usr/bin/env bash
-
-if [ -z "$(which pygmentize)" ]
-then
- echo "Looks like you don't have Pygments installed."
-
- if [ -z "$(which easy_install)" ]
- then
- echo "Looks like you don't have easy_install either."
- echo "Check out http://pypi.python.org/pypi/setuptools to install easy_install, then run:"
- echo " sudo easy_install Pygments"
- echo "to install pygments"
- else
- echo "You need to run:"
- echo " sudo easy_install Pygments"
- echo "to install Pygments in order for docker to work"
- fi
-fi
View
@@ -49,8 +49,10 @@ var mkdirp = require('mkdirp'),
exec = require('child_process').exec,
spawn = require('child_process').spawn,
watchr = require('watchr'),
+ pygmentize = require('pygmentize-bundled'),
showdown = require('../lib/showdown').Showdown;
+
// Polyfill `fs.exists` for node <= 0.6
if(typeof fs.exists != 'function') fs.exists = path.exists;
@@ -65,9 +67,6 @@ if(typeof fs.exists != 'function') fs.exists = path.exists;
* * Or `indir`, `outDir`, `onlyUpdated`, `colourScheme` and `ignoreHidden` in order
*/
var Docker = module.exports = function( /* inDir, outDir, onlyUpdated, colourScheme, ignoreHidden */ ){
- if(!this.checkForPygments()){
- throw new Error("Pygments doesn't appear to be installed. Docker can't work.");
- }
if(typeof arguments[0] === 'object'){
this.parseOpts(arguments[0]);
@@ -149,41 +148,6 @@ Docker.prototype.parseOpts = function(opts){
}
};
-/**
- * ## Docker.prototype.checkForPygments
- *
- * Checks to see whether Pygments is installed
- *
- * @return {boolean} True or false, whether Pygments is present
- */
-Docker.prototype.checkForPygments = function(){
-
- // Grab the platform type; it'll tell us how $PATH is delimited
- var platformType = os.type();
-
- var Path = process.env.path || process.env.Path || process.env.PATH;
-
- Path = Path.split(/^Win/.test(platformType) ? ';' : ':');
-
- // We care about whether the pygmentize command exists on at least
- // one of the given paths. We can use Array.some for this
- return Path.some(function(dir){
-
- // Resolve where the command would be for this path
- var guess = path.resolve(dir, 'pygmentize');
-
- return fs.existsSync(guess) || (
-
- // for Windows, there are a few more options
- /^Win/.test(platformType) && (
- fs.existsSync(guess + '.exe') ||
- fs.existsSync(guess + '.cmd') ||
- fs.existsSync(guess + '.bat')
- ));
-
- });
-};
-
/**
* ## Docker.prototype.doc
*
@@ -980,33 +944,26 @@ Docker.prototype.languages = {
Docker.prototype.pygments = function(data, language, cb){
// By default tell Pygments to guess the language, and if
// we have a language specified then tell pygments to use that lexer
- var pygArgs = ['-g'];
- if(language) pygArgs = ['-l', language];
-
- // Spawn a new **pygments** process
- var pyg = spawn('pygmentize', pygArgs.concat(['-f', 'html', '-O', 'encoding=utf-8,tabsize=2']));
-
- // Hook up errors, for either when pygments itself throws an error,
- // or for when we're unable to send the code to pygments for some reason
- pyg.stderr.on('data', function(err){ console.error(err.toString()); });
- pyg.stdin.on('error', function(err){
- console.error('Unable to write to Pygments stdin: ' , err);
- process.exit(1);
- });
+ var pygOpts = {
+ format: 'html',
+ options: {
+ encoding: 'utf-8',
+ tabsize: 2,
+ style: this.colourScheme
+ }
+ };
- var out = '';
- pyg.stdout.on('data', function(data){ out += data.toString(); });
+ if(language) pygOpts['lang'] = language;
- // When pygments is done, fire the callback with our output
- pyg.on('exit', function(){
- cb(out);
- });
+ // run pygmentize
+ pygmentize(pygOpts, data, function(err, result) {
+ if (err) {
+ console.error(err.toString());
+ return;
+ }
- // Feed pygments with the code
- if(pyg.stdin.writable){
- pyg.stdin.write(data);
- pyg.stdin.end();
- }
+ cb(result.toString());
+ });
};
/**
@@ -1159,14 +1116,14 @@ Docker.prototype.addAnchors = function(docHtml, idx, headings){
docHtml = docHtml.replace(/(<h([0-9])>)(.*)(<\/h\2>)/g, function(a, start, level, middle, end){
var id = encodeURIComponent(middle.replace(/<[^>]*>/g,'').toLowerCase());
var headingId = id;
-
+
if(typeof ids[id] === 'undefined'){
ids[id] = 0;
}else{
ids[id]++;
headingId = id + '_' + ids[id];
}
-
+
headings.push({ id: id, headingId: headingId, text: middle.replace(/<[^>]*>/g,''), level: level });
return '\n<div class="pilwrap" id="' + headingId + '">\n '+
start +
@@ -1347,20 +1304,43 @@ Docker.prototype.copySharedResources = function(){
);
});
+ // {{{ create css file with pygmentize
+ var pygOpts = {
+ format: 'html',
+ options: {
+ full: true,
+ style: this.colourScheme
+ }
+ };
+
fs.readFile(path.join(path.dirname(__filename),'..','res','css', self.colourScheme + '.css'), function(err, file){
- exec('pygmentize -S ' + self.colourScheme + ' -f html -a "body .highlight"', function(code, stdout, stderr){
- if(code || stderr !== ''){
- console.error('Error generating CSS: \n' + stderr);
+ if (err) {
+ console.error('Error reading base CSS:\n', err);
+ process.exit();
+ }
+ pygmentize(pygOpts, '', function(err, result) {
+ if (err) {
+ console.error('Error generating CSS:\n', err);
process.exit();
}
+
+ // extract css
+ result = result.toString().match(/<style type="text\/css">([\S\s]*)<\/style>/)[1];
+ // add selector prefix
+ result = result.replace(/\nbody /g, '\nbody .highlight ');
+
+ // save combined file
self.writeFileIfDifferent(
path.join(self.outDir, 'doc-style.css'),
- file.toString() + stdout,
+ file.toString() + result,
'Copied ' + self.colourScheme + '.css to doc-style.css',
done
);
});
+
});
+ // }}}
+
self.writeFileIfDifferent(
path.join(self.outDir, 'doc-filelist.js'),

0 comments on commit 4a2a925

Please sign in to comment.