Skip to content

Commit

Permalink
Referencing implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
inossidabile committed Nov 6, 2013
1 parent ba2a45e commit bb6202f
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/_tools.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = Tools = {}
4 changes: 3 additions & 1 deletion lib/codo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion lib/entities/extra.coffee
Original file line number Diff line number Diff line change
@@ -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

Expand Down
17 changes: 17 additions & 0 deletions lib/environment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/markdown.coffee → lib/tools/markdown.coffee
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
25 changes: 25 additions & 0 deletions lib/tools/referencer.coffee
Original file line number Diff line number Diff line change
@@ -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, '}')
6 changes: 5 additions & 1 deletion spec/lib/environment_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
expect(Object.keys environment.references).toEqual [
'spec/_templates/environment/class.coffee',
'spec/_templates/environment/mixin.coffee',
'Fluffy', 'LookAndFeel', 'LookAndFeel~feel', 'LookAndFeel~look'
]
15 changes: 10 additions & 5 deletions themes/default/lib/theme.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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) =>
"<a href='#{@pathFor link, undefined, prefix}'>#{label}</a>"

Codo.Tools.Markdown.convert(text, limit)

generateBreadcrumbs: (entries = []) ->
entries = [entries] unless Array.isArray(entries)
Expand Down Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions themes/default/templates/partials/documentation.hamlc
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@
- if @documentation.deprecated?
.note.deprecated
%strong Deprecated.
!= @activate @documentation.deprecated, true
!= @activate @documentation.deprecated, @path, true

- if @documentation.abstract?
.note.abstract
%strong
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
Expand All @@ -56,7 +56,7 @@
)
- if param.desc
&mdash;
%span.desc!= @activate param.desc, true
%span.desc!= @activate param.desc, @path, true

- if @documentation.options
- for hash, options of @documentation.options
Expand All @@ -73,7 +73,7 @@
)
- if option.desc
&mdash;
%span.desc!= @activate option.desc, true
%span.desc!= @activate option.desc, @path, true

- if @documentation.throws
%h3 Throws:
Expand All @@ -86,7 +86,7 @@
%tt>= throws.type
)
&mdash;
%span.desc!= @activate throws.desc, true
%span.desc!= @activate throws.desc, @path, true
- else
%tt>= throws.type

Expand All @@ -100,7 +100,7 @@
%tt>= @documentation.returns.type
)
&mdash;
%span.desc!= @activate @documentation.returns.desc, true
%span.desc!= @activate @documentation.returns.desc, @path, true
- else
%tt>= @documentation.returns.type

Expand All @@ -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:
Expand Down

0 comments on commit bb6202f

Please sign in to comment.