diff --git a/lib/_tools.coffee b/lib/_tools.coffee new file mode 100644 index 0000000..b3793fe --- /dev/null +++ b/lib/_tools.coffee @@ -0,0 +1 @@ +module.exports = Tools = {} \ No newline at end of file diff --git a/lib/codo.coffee b/lib/codo.coffee index a11e350..01437fa 100644 --- a/lib/codo.coffee +++ b/lib/codo.coffee @@ -7,7 +7,9 @@ module.exports = Codo = Environment: require './environment' - Markdown: require './markdown' + Tools: + Markdown: require './tools/markdown' + Referencer: require './tools/referencer' Entities: File: require './entities/file' diff --git a/lib/entities/extra.coffee b/lib/entities/extra.coffee index db2e840..e3fba9c 100644 --- a/lib/entities/extra.coffee +++ b/lib/entities/extra.coffee @@ -1,7 +1,7 @@ FS = require 'fs' Path = require 'path' Entities = require '../_entities' -Markdown = require '../markdown' +Markdown = require '../tools/markdown' module.exports = class Entities.Extra diff --git a/lib/environment.coffee b/lib/environment.coffee index fa0b29f..930d9d9 100644 --- a/lib/environment.coffee +++ b/lib/environment.coffee @@ -98,6 +98,19 @@ module.exports = class Environment return 1 if a.entity.name > b.entity.name return 0 + allVariables: -> + return @_allVariables if @_allVariables? + + @_allVariables = [] + + for source in [@allFiles(), @allClasses(), @allMixins()] + for entry in source + for variable in entry.variables + @_allVariables.push {entity: variable, owner: entry} + + @_allVariables + + find: (Entity, name) -> for entity in @entities if entity instanceof Entity && entity.name == name @@ -113,6 +126,10 @@ module.exports = class Environment for basic in basics @references[basic.name] = basic + for variable in @allVariables() + keyword = variable.owner.name + '.' + variable.entity.name + @references[keyword] = variable + for method in @allMethods() keyword = method.owner.name + method.entity.shortSignature() @references[keyword] = method diff --git a/lib/markdown.coffee b/lib/tools/markdown.coffee similarity index 95% rename from lib/markdown.coffee rename to lib/tools/markdown.coffee index 3ddca2e..2b5c32e 100644 --- a/lib/markdown.coffee +++ b/lib/tools/markdown.coffee @@ -1,11 +1,12 @@ marked = require 'marked' +Tools = require '../_tools' # It looks like all the markdown libraries for node doesn't get # GitHub flavored markdown right. This helper class post-processes # the best available output from the marked library to conform to # GHM. In addition the allowed tags can be limited. # -module.exports = class Markdown +module.exports = class Tools.Markdown # Tags to keep when parsing is limited @limitedTags: 'a,abbr,acronym,b,big,cite,code,del,em,i,ins,sub,sup,span,small,strike,strong,q,tt,u' diff --git a/lib/tools/referencer.coffee b/lib/tools/referencer.coffee new file mode 100644 index 0000000..90215f0 --- /dev/null +++ b/lib/tools/referencer.coffee @@ -0,0 +1,25 @@ +Tools = require '../_tools' + +module.exports = class Tools.Referencer + + constructor: (@environment) -> + + resolve: (text, replacer) -> + # Make curly braces within code blocks undetectable + text = text.replace /\`[^\`]*\`/mg, (match) -> match.replace(/\{/mg, "\u0091").replace(/\}/mg, "\u0092") + + # Search for references and replace them + text = text.replace /\{([^\}]*)\}/gm, (match, link) => + link = link.split(' ') + href = link.shift() + label = link.join(' ') + + replacement = @environment.reference(href) + + if replacement != href || /\:\/\/\w+((\:\d+)?\/\S*)?/.test(href) + replacer replacement, label || href + else + match + + # Restore curly braces within code blocks + text = text.replace /\`[^\`]*\`/mg, (match) -> match.replace(/\u0091/mg, '{').replace(/\u0092/mg, '}') diff --git a/spec/lib/environment_spec.coffee b/spec/lib/environment_spec.coffee index 9f3adf3..7939cd8 100644 --- a/spec/lib/environment_spec.coffee +++ b/spec/lib/environment_spec.coffee @@ -93,4 +93,8 @@ describe 'Environment', -> actual = JSON.stringify(environment.inspect(), null, 2) expect(FS.readFileSync('spec/_templates/environment/result.json', 'utf8')).toEqual actual - console.log environment.references \ No newline at end of file + expect(Object.keys environment.references).toEqual [ + 'spec/_templates/environment/class.coffee', + 'spec/_templates/environment/mixin.coffee', + 'Fluffy', 'LookAndFeel', 'LookAndFeel~feel', 'LookAndFeel~look' + ] \ No newline at end of file diff --git a/themes/default/lib/theme.coffee b/themes/default/lib/theme.coffee index 8aea1d5..33d2908 100644 --- a/themes/default/lib/theme.coffee +++ b/themes/default/lib/theme.coffee @@ -18,7 +18,8 @@ module.exports = class Theme.Theme theme.compile() constructor: (@environment) -> - @templater = new Templater(@environment.options.output) + @templater = new Templater(@environment.options.output) + @referencer = new Codo.Tools.Referencer(@environment) compile: -> @templater.compileAsset('javascript/application.js') @@ -55,19 +56,23 @@ module.exports = class Theme.Theme kind = 'file' if entity instanceof Codo.Entities.File kind = 'extra' if entity instanceof Codo.Entities.Extra kind = 'method' if entity.entity instanceof Codo.Meta.Method + kind = 'variable' if entity.entity instanceof Codo.Entities.Variable switch kind when 'file', 'extra' prefix + kind + '/' + entity.name + '.html' when 'class', 'mixin' prefix + kind + '/' + entity.name.replace(/\./, '/') + '.html' - when 'method' + when 'method', 'variable' @pathFor(entity.owner, undefined, prefix) + '#' + @anchorFor(entity.entity) else entity - activate: (text, limit=false) -> - Codo.Markdown.convert(text, limit) + activate: (text, prefix, limit=false) -> + text = @referencer.resolve text, (link, label) => + "#{label}" + + Codo.Tools.Markdown.convert(text, limit) generateBreadcrumbs: (entries = []) -> entries = [entries] unless Array.isArray(entries) @@ -99,7 +104,7 @@ module.exports = class Theme.Theme anchorFor: @anchorFor pathFor: @pathFor reference: @reference - activate: @activate + activate: => @activate(arguments...) render: (template, context={}) => context[key] = value for key, value of globalContext @templater.render template, context diff --git a/themes/default/templates/partials/documentation.hamlc b/themes/default/templates/partials/documentation.hamlc index 4598736..30249c8 100644 --- a/themes/default/templates/partials/documentation.hamlc +++ b/themes/default/templates/partials/documentation.hamlc @@ -9,7 +9,7 @@ - if @documentation.deprecated? .note.deprecated %strong Deprecated. - != @activate @documentation.deprecated, true + != @activate @documentation.deprecated, @path, true - if @documentation.abstract? .note.abstract @@ -17,21 +17,21 @@ This = @kind is abstract. - != @activate @documentation.abstract, true + != @activate @documentation.abstract, @path, true - if @documentation.todos - for todo in @documentation.todos .note.todo %strong TODO: - != @activate todo, true + != @activate todo, @path, true - if @documentation.notes - for note in @documentation.notes .note %strong Note: - != @activate note, true + != @activate note, @path, true - != @activate @documentation.comment + != @activate @documentation.comment, @path - if @documentation.examples .examples @@ -56,7 +56,7 @@ ) - if param.desc — - %span.desc!= @activate param.desc, true + %span.desc!= @activate param.desc, @path, true - if @documentation.options - for hash, options of @documentation.options @@ -73,7 +73,7 @@ ) - if option.desc — - %span.desc!= @activate option.desc, true + %span.desc!= @activate option.desc, @path, true - if @documentation.throws %h3 Throws: @@ -86,7 +86,7 @@ %tt>= throws.type ) — - %span.desc!= @activate throws.desc, true + %span.desc!= @activate throws.desc, @path, true - else %tt>= throws.type @@ -100,7 +100,7 @@ %tt>= @documentation.returns.type ) — - %span.desc!= @activate @documentation.returns.desc, true + %span.desc!= @activate @documentation.returns.desc, @path, true - else %tt>= @documentation.returns.type @@ -109,25 +109,25 @@ %ul.author - for author in @documentation.authors %li - != @activate author, true + != @activate author, @path, true - if @documentation.copyright %h3 Copyright: %ul.copyright %li - != @activate @documentation.copyright, true + != @activate @documentation.copyright, @path, true - if @documentation.since %h3 Since: %ul.since %li - != @activate @documentation.since, true + != @activate @documentation.since, @path, true - if @documentation.version %h3 Version: %ul.version %li - != @activate @documentation.version, true + != @activate @documentation.version, @path, true - if @documentation.see %h3 See also: