Skip to content

Instantly share code, notes, and snippets.

@DanielVF
Created March 5, 2010 17:39
Show Gist options
  • Save DanielVF/322951 to your computer and use it in GitHub Desktop.
Save DanielVF/322951 to your computer and use it in GitHub Desktop.
diff --git a/docco.coffee b/docco.coffee
index beb6ec7..399fe80 100644
--- a/docco.coffee
+++ b/docco.coffee
@@ -25,11 +25,11 @@
# and merging them into an HTML template.
generate_documentation: (source) ->
ensure_directory ->
- set_language source
code: fs.readFile source, (error, code) ->
throw error if error
- sections: parse code
- highlight source, sections, ->
+ language: get_language(source)
+ sections: parse code, language
+ highlight source, language, sections, ->
generate_html source, sections
# Given a string of source code, parse out each comment and the code that
@@ -43,7 +43,7 @@ generate_documentation: (source) ->
# code_html: ...
# }
#
-parse: (code) ->
+parse: (code, language) ->
lines: code.split '\n'
sections: []
has_code: docs_text: code_text: ''
@@ -73,7 +73,7 @@ parse: (code) ->
# We process the entire file in a single call to Pygments by inserting little
# marker comments between each section and then splitting the result string
# wherever our markers occur.
-highlight: (source, sections, callback) ->
+highlight: (source, language, sections, callback) ->
pygments: process.createChildProcess 'pygmentize', ['-l', language.name, '-f', 'html']
output: ''
pygments.addListener 'error', (error) ->
@@ -106,6 +106,7 @@ generate_html: (source, sections) ->
# (the JavaScript implementation of Markdown).
require.paths.unshift __dirname
fs: require 'fs'
+sys: require 'sys'
path: require 'path'
showdown: require('vendor/showdown').Showdown
@@ -118,13 +119,9 @@ languages: {
'.rb': {name: 'ruby', symbol: '#'}
}
-# The language object, containing the appropriate matchers and delimiters for
-# for the current sourcefile's language.
-language: null
-
# Set the current language we're documenting, based on the extension.
-set_language: (source) ->
- l: language: languages[path.extname(source)]
+get_language: (source) ->
+ l: languages[path.extname(source)]
# Does the line begin with a comment?
l.comment_matcher: new RegExp('^\\s*' + l.symbol + '\\s?')
@@ -136,6 +133,8 @@ set_language: (source) ->
# The mirror of `divider_text` that we expect Pygments to return. We can split
# on this to recover the original sections.
l.divider_html: new RegExp('\\n*<span class="c1">' + l.symbol + 'DIVIDER<\\/span>\\n*')
+
+ l
# Compute the destination HTML path for an input source file path. If the source
# is `lib/example.coffee`, the HTML will be at `docs/example.html`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment