Skip to content

Commit ab280e7

Browse files
committed
Docco can now handle literate CoffeeScript, as well as literate anything-else, with the .codeext.md convention
1 parent 017c118 commit ab280e7

File tree

6 files changed

+685
-184
lines changed

6 files changed

+685
-184
lines changed

bin/docco

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
var path = require('path');
44
var fs = require('fs');
5-
var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');
6-
require(lib + '/docco.js').run();
5+
var dir = path.join(path.dirname(fs.realpathSync(__filename)), '../');
6+
require(dir + 'docco.js').run();

docco.js

+41-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docco.litcoffee

+24-23
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ follows it, and create an individual **section** for it.
7979
save = (docsText, codeText) ->
8080
sections.push {docsText, codeText}
8181
82+
if language.literate
83+
for line, i in lines
84+
lines[i] = if match = (/^([ ]{4}|\t)/).exec line
85+
line[match[0].length..]
86+
else
87+
'# ' + line
88+
8289
for line in lines
8390
if line.match(language.commentMatcher) and not line.match(language.commentFilter)
8491
if hasCode
@@ -89,6 +96,7 @@ follows it, and create an individual **section** for it.
8996
hasCode = yes
9097
codeText += line + '\n'
9198
save docsText, codeText
99+
92100
sections
93101
94102
Highlights parsed sections of code, using **Pygments** over stdio,
@@ -150,7 +158,7 @@ the specified output path.
150158

151159
generateHtml = (source, sections, config) ->
152160
destination = (filepath) ->
153-
path.join(config.output, path.basename(filepath, path.extname(filepath)) + '.html')
161+
path.join(config.output, path.basename(filepath, config.fullExtension) + '.html')
154162
title = path.basename source
155163
dest = destination source
156164
html = config.doccoTemplate {
@@ -170,16 +178,17 @@ Helpers & Setup
170178

171179
Require our external dependencies.
172180

181+
_ = require 'underscore'
173182
fs = require 'fs'
174183
path = require 'path'
175184
marked = require 'marked'
176-
{spawn, exec} = require 'child_process'
177185
commander = require 'commander'
186+
{spawn, exec} = require 'child_process'
178187
179188
Read resource file and return its content.
180189

181190
getResource = (name) ->
182-
fullPath = path.join __dirname, '..', 'resources', name
191+
fullPath = path.join __dirname, 'resources', name
183192
fs.readFileSync(fullPath).toString()
184193
185194
Languages are stored in JSON format in the file `resources/languages.json`
@@ -218,7 +227,15 @@ Build out the appropriate matchers and delimiters for each language.
218227
219228
Get the current language we're documenting, based on the extension.
220229

221-
getLanguage = (source, config) -> languages[config.extension or path.extname(source)]
230+
getLanguage = (source, config) ->
231+
ext = config.fullExtension = config.extension or path.extname(source)
232+
lang = languages[ext]
233+
if lang.name is 'markdown'
234+
codeExt = path.extname(path.basename(source, ext))
235+
if codeExt and codeLang = languages[codeExt]
236+
config.fullExtension = codeExt + ext
237+
lang = _.extend {}, codeLang, {literate: yes}
238+
lang
222239
223240
Ensure that the destination directory exists.
224241

@@ -231,22 +248,6 @@ Ensure that the destination directory exists.
231248
if er then cb er, made else ensureDirectory dir, cb, made
232249
cb er, made
233250
234-
Micro-templating, originally by John Resig, borrowed by way of
235-
[Underscore.js](http://documentcloud.github.com/underscore/).
236-
237-
template = (str) ->
238-
new Function 'obj',
239-
'var p=[],print=function(){p.push.apply(p,arguments);};' +
240-
'with(obj){p.push(\'' +
241-
str.replace(/[\r\t\n]/g, " ")
242-
.replace(/'(?=[^<]*%>)/g,"\t")
243-
.split("'").join("\\'")
244-
.split("\t").join("'")
245-
.replace(/<%=(.+?)%>/g, "',$1,'")
246-
.split('<%').join("');")
247-
.split('%>').join("p.push('") +
248-
"');}return p.join('');"
249-
250251
The start of each Pygments highlight block.
251252

252253
highlightStart = '<div class="highlight"><pre>'
@@ -303,7 +304,7 @@ Run Docco over a list of `sources` with the given `options`.
303304
config.sources = resolved.filter((source) -> getLanguage source, config).sort()
304305
console.log "docco: skipped unknown type (#{m})" for m in resolved when m not in config.sources
305306
306-
config.doccoTemplate = template fs.readFileSync(config.template).toString()
307+
config.doccoTemplate = _.template fs.readFileSync(config.template).toString()
307308
doccoStyles = fs.readFileSync(config.css).toString()
308309
309310
ensureDirectory config.output, ->
@@ -333,5 +334,5 @@ Resolve a wildcard `source` input to the files it matches.
333334
Public API
334335
----------
335336

336-
module.exports = {run, document, parse, resolveSource, version, defaults,
337-
languages, ensureDirectory}
337+
module.exports = {run, document, parse, resolveSource, version, defaults,
338+
languages, ensureDirectory}

0 commit comments

Comments
 (0)