Permalink
Browse files

Add support for using plaintext code output when pygments is not pres…

…ent, rather than failing entirely.
  • Loading branch information...
1 parent 02c4824 commit 7c1e3b8a4b0c4fd206ab33e1aebdfea3b17b4ed6 @justindujardin justindujardin committed Oct 26, 2012
Showing with 15 additions and 3 deletions.
  1. +15 −3 src/docco.coffee
View
@@ -118,15 +118,17 @@ highlight = (source, sections, callback) ->
console.error error.toString() if error
pygments.stdin.on 'error', (error) ->
- console.error 'Could not use Pygments to highlight the source.'
- process.exit 1
+ console.error 'Could not highlight code, using plaintext'
pygments.stdout.on 'data', (result) ->
output += result if result
pygments.on 'exit', ->
output = output.replace(highlightStart, '').replace(highlightEnd, '')
- codeFragments = output.split language.codeSplitHtml
+ if output is ''
+ codeFragments = (htmlEscape section.codeText for section in sections)
+ else
+ codeFragments = output.split language.codeSplitHtml
docsFragments = showdown.makeHtml(docs).split language.docsSplitHtml
for section, i in sections
@@ -138,6 +140,16 @@ highlight = (source, sections, callback) ->
pygments.stdin.write code
pygments.stdin.end()
+# Escape an html string, to produce valid non-highlighted output when pygments
+# is not present on the system.
+htmlEscape = (string) ->
+ string.replace(/&/g, '&')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;')
+ .replace(/"/g, '&quot;')
+ .replace(/'/g, '&#x27;')
+ .replace(/\//g,'&#x2F;')
+
# Once all of the code is finished highlighting, we can generate the HTML file by
# passing the completed sections into the template, and then writing the file to
# the specified output path.

0 comments on commit 7c1e3b8

Please sign in to comment.