Skip to content

Commit

Permalink
Referencing implemented basically
Browse files Browse the repository at this point in the history
  • Loading branch information
inossidabile committed Nov 6, 2013
1 parent 6132513 commit ba2a45e
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 40 deletions.
34 changes: 14 additions & 20 deletions lib/codo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,24 @@ Path = require 'path'
walkdir = require 'walkdir'
Winston = require 'winston'

Environment = require './environment'
Markdown = require './markdown'
File = require './entities/file'
Class = require './entities/class'
Method = require './entities/method'
Variable = require './entities/variable'
Property = require './entities/property'
Mixin = require './entities/mixin'
Extra = require './entities/extra'


module.exports = Codo =
Environment: Environment

Markdown: Markdown
Environment: require './environment'

Markdown: require './markdown'

Entities:
File: File
Class: Class
Method: Method
Variable: Variable
Property: Property
Mixin: Mixin
Extra: Extra
File: require './entities/file'
Class: require './entities/class'
Method: require './entities/method'
Variable: require './entities/variable'
Property: require './entities/property'
Mixin: require './entities/mixin'
Extra: require './entities/extra'

Meta:
Method: require './meta/method'
Parameter: require './meta/parameter'

version: ->
JSON.parse(
Expand Down
28 changes: 24 additions & 4 deletions lib/environment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ module.exports = class Environment
@options.output ?= 'doc'
@options.basedir ?= process.cwd()

@needles = []
@entities = []
@parsed = {}
@needles = []
@entities = []
@references = {}
@parsed = {}

@needles.push Class
@needles.push Method
Expand All @@ -63,13 +64,16 @@ module.exports = class Environment
Winston.info("Parsing Extra file #{file}") if @options.verbose

try
@entities.push(new Extra @, file)
@registerEntity(new Extra @, file)
catch error
throw error if @options.debug
Winston.error("Cannot parse Extra file #{file}: #{error.message}") unless @options.quiet
finally
@parsed[file] = true

registerEntity: (entity) ->
@entities.push entity

all: (Entity, haystack = []) ->
for entity in @entities
haystack.push(entity) if entity instanceof Entity
Expand Down Expand Up @@ -105,5 +109,21 @@ module.exports = class Environment
linkify: ->
entity.linkify() for entity in @entities

for basics in [@allFiles(), @allClasses(), @allMixins()]
for basic in basics
@references[basic.name] = basic

for method in @allMethods()
keyword = method.owner.name + method.entity.shortSignature()
@references[keyword] = method

reference: (needle) ->
needle = needle.split(' ')[0]

if @references[needle]
@references[needle]
else
needle

inspect: ->
@entities.map (entity) -> entity.inspect()
2 changes: 1 addition & 1 deletion lib/traverser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ module.exports = class Traverser
if Entity.is(node)
entity = new Entity @environment, file, node
node.entities.push(entity)
@environment.entities.push(entity)
@environment.registerEntity(entity)

entity

Expand Down
1 change: 1 addition & 0 deletions spec/lib/environment_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,4 @@ describe 'Environment', ->

actual = JSON.stringify(environment.inspect(), null, 2)
expect(FS.readFileSync('spec/_templates/environment/result.json', 'utf8')).toEqual actual
console.log environment.references
26 changes: 22 additions & 4 deletions themes/default/lib/theme.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,35 @@ module.exports = class Theme.Theme
#
# HELPERS
#
reference: (needle, prefix) ->
@pathFor(@environment.reference(needle), undefined, prefix)

anchorFor: (entity) ->
if entity instanceof Codo.Meta.Method
"#{entity.name}-#{entity.kind}"
else if entity instanceof Codo.Entities.Property
"#{entity.name}-property"
else if entity instanceof Codo.Entities.Variable
"#{entity.name}-variable"

pathFor: (kind, entity, prefix='') ->
unless entity?
entity = kind
kind = 'class' if entity instanceof Codo.Entities.Class
kind = 'mixin' if entity instanceof Codo.Entities.Mixin
kind = 'file' if entity instanceof Codo.Entities.File
kind = 'extra' if entity instanceof Codo.Entities.Extra
kind = 'class' if entity instanceof Codo.Entities.Class
kind = 'mixin' if entity instanceof Codo.Entities.Mixin
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

switch kind
when 'file', 'extra'
prefix + kind + '/' + entity.name + '.html'
when 'class', 'mixin'
prefix + kind + '/' + entity.name.replace(/\./, '/') + '.html'
when 'method'
@pathFor(entity.owner, undefined, prefix) + '#' + @anchorFor(entity.entity)
else
entity

activate: (text, limit=false) ->
Codo.Markdown.convert(text, limit)
Expand Down Expand Up @@ -80,7 +96,9 @@ module.exports = class Theme.Theme
globalContext =
environment: @environment
path: @calculatePath(destination)
anchorFor: @anchorFor
pathFor: @pathFor
reference: @reference
activate: @activate
render: (template, context={}) =>
context[key] = value for key, value of globalContext
Expand Down
14 changes: 7 additions & 7 deletions themes/default/templates/class.hamlc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
- if @entity.properties.length > 0
%dl.properties
- for property in @entity.properties
%dt{ id: "#{property.name}-property" }
%dt{id: @anchorFor(property)}
%span.signature
- if property.documentation?.property
= "(#{property.documentation.property})"
Expand All @@ -102,7 +102,7 @@
%a{href: @pathFor('class', @entity.parent, @path)}= @entity.parent.name
%p.inherited
- for property in @entity.inheritedProperties()
%a{href: "#{@pathFor('class', @entity.parent, @path)}##{property.name}-property"}= property.name
%a{href: "#{@pathFor('class', @entity.parent, @path)}##{@anchorFor(property)}"}= property.name


- if @entity.variables.length > 0 || @entity.inheritedVariables().length > 0
Expand All @@ -115,7 +115,7 @@
%a{href: @pathFor('class', @entity.parent, @path)}= @entity.parent.name
%p.inherited
- for variable in @entity.inheritedVariables()
%a{href: "#{@pathFor('class', @entity.parent, @path)}##{variable.name}-variable"}= variable.name
%a{href: "#{@pathFor('class', @entity.parent, @path)}##{@anchorFor(variable)}"}= variable.name


- staticMethods = @entity.effectiveMethods().filter (m) -> m.kind == 'static'
Expand All @@ -139,7 +139,7 @@
%a{href: @pathFor('class', @entity.parent, @path)}= @entity.parent.name
%p.inherited
- for method in @entity.inheritedMethods()
%a{href: "#{@pathFor 'class', @entity.parent, @path}##{method.name}-#{method.kind}"}= method.shortSignature()
%a{href: "#{@pathFor 'class', @entity.parent, @path}##{@anchorFor(method)}"}= method.shortSignature()

- if @entity.extends.length > 0
%h2
Expand All @@ -152,7 +152,7 @@
%a{href: @pathFor('mixin', mixin, @path)}= mixin.name
%p.inherited
- for method in mixin.effectiveExtensionMethods()
%a{href: "#{@pathFor 'mixin', mixin, @path}##{method.name}-#{method.kind}"}= method.shortSignature()
%a{href: "#{@pathFor 'mixin', mixin, @path}##{@anchorFor(method)}"}= method.shortSignature()
- else
%h3.inherited
Methods extended from
Expand All @@ -169,7 +169,7 @@
%a{href: @pathFor('mixin', mixin, @path)}= mixin.name
%p.inherited
- for method in mixin.effectiveInclusionMethods()
%a{href: "#{@pathFor 'mixin', mixin, @path}##{method.name}-#{method.kind}"}= method.shortSignature()
%a{href: "#{@pathFor 'mixin', mixin, @path}##{@anchorFor(method)}"}= method.shortSignature()
- else
%h3.inherited
Methods included from
Expand All @@ -187,7 +187,7 @@
%a{href: @pathFor('mixin', mixin, @path)}= mixin.name
%p.inherited
- for method in mixin.effectiveConcernMethods()
%a{href: "#{@pathFor 'mixin', mixin, @path}##{method.name}-#{method.kind}"}= method.shortSignature()
%a{href: "#{@pathFor 'mixin', mixin, @path}##{@anchorFor(method)}"}= method.shortSignature()
- else
%h3.inherited
Methods concerned from
Expand Down
2 changes: 1 addition & 1 deletion themes/default/templates/method_list.hamlc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
%ul
- for method in @environment.allMethods()
%li{deprecated: if method.documentation?.deprecated then true else false}
%a{href: "#{@pathFor(method.owner, undefined, @path)}##{method.entity.name}-#{method.entity.kind}", target: 'main', title: method.entity.name}
%a{href: @pathFor(method), target: 'main', title: method.entity.name}
= method.entity.shortSignature()
%small
= surround '(', ')', -> method.owner.name
4 changes: 2 additions & 2 deletions themes/default/templates/partials/documentation.hamlc
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@
- for see in @documentation.see
%li
- if see.reference && see.label
%a{ href: see.reference }= see.label
%a{href: @reference(see.reference, @path)}= see.label
- else if see.reference
%a{ href: see.reference }= see.reference
%a{href: @reference(see.reference, @path)}= see.reference
- else
= see.label

Expand Down
2 changes: 1 addition & 1 deletion themes/default/templates/partials/method_summary.hamlc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
%li{deprecated: if method.documentation?.deprecated then true else false}

%span.signature
%a{href: "##{method.name}-#{method.kind}"}
%a{href: '#'+@anchorFor(method)}
!= @render 'partials/method_signature', method: method

- if method.name == 'constructor'
Expand Down

0 comments on commit ba2a45e

Please sign in to comment.