docjs 0.1.5 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,16 +9,6 @@ require_relative '../parser/meta_container'
9
9
  #
10
10
  module CodeObject
11
11
 
12
- # Find all CodeObject-Types, that inherit from Base
13
- # @return [Hash] like {:object => CodeObject::Object, :function => CodeObject::Function }
14
- def self.all_types
15
- Hash[self.constants
16
- .map { |c| [c.to_s.downcase.to_sym, self.const_get(c)] }
17
- .select { |klass| klass[1].class == Class and klass[1].ancestors.include? Base }
18
- ]
19
- end
20
-
21
-
22
12
  class Base
23
13
 
24
14
  include Token::Container
@@ -11,10 +11,10 @@ module CodeObject
11
11
 
12
12
  def to_code_object
13
13
 
14
- # 1. Create a new CodeObject from Type-Token
14
+ # 1. Create a new CodeObject from Type-Token like @function → CodeObject::Function
15
15
  @code_object = find_type_for(@tokenlines) or return nil
16
16
 
17
- # join all documentation-contents
17
+ # join all documentation-contents and make them one text again
18
18
  @code_object.docs = @doclines.join ''
19
19
 
20
20
  # move meta-information from comment to code_object
@@ -45,7 +45,7 @@ module CodeObject
45
45
  def find_type_for(tokenlines)
46
46
 
47
47
  # all_types returns a hash like {:object => CodeObject::Object ...}
48
- available_types = CodeObject.all_types
48
+ available_types = Token::Handler.types
49
49
 
50
50
  types = tokenlines.select {|t| available_types.has_key? t.token }
51
51
 
data/lib/dom/dom.rb CHANGED
@@ -4,14 +4,13 @@ require_relative 'exceptions'
4
4
  # Storing {CodeObject::Base Objects}
5
5
  # ==================================
6
6
  #
7
- # The datastructure is based on a treelike concept. Every new inserted
8
- # {CodeObject::Base object} is represented by a {Dom::Node node} of the tree.
7
+ # The datastructure is based on a treelike concept. Every new inserted {CodeObject::Base object} is
8
+ # represented by a {Dom::Node node} of the tree.
9
9
  # There are three types of nodes:
10
10
  #
11
11
  # 1. {Dom::NoDoc Not documented nodes} that contain other nodes
12
12
  # 2. {Dom::Node Documented nodes}, that contain other nodes
13
- # 3. Leafs of the tree, without children. (Those leafs have to be
14
- # {Dom::Node documented nodes})
13
+ # 3. Leafs of the tree, without children. (Those leafs have to be {Dom::Node documented nodes})
15
14
  #
16
15
  # The architecure of the Dom looks pretty much like this:
17
16
  #
@@ -49,8 +48,8 @@ require_relative 'exceptions'
49
48
  # / \
50
49
  # bar baz
51
50
  #
52
- # Now all nodes are documented (i.e. a CodeObject exists for every node) and `Foo`
53
- # contains two other CodeObjects. `bar` and `baz` are leafs of the tree.
51
+ # Now all nodes are documented (i.e. a CodeObject exists for every node) and `Foo` contains two
52
+ # other CodeObjects. `bar` and `baz` are leafs of the tree.
54
53
  #
55
54
  # Adding a new node to the tree is as simple as:
56
55
  #
@@ -58,8 +57,8 @@ require_relative 'exceptions'
58
57
  #
59
58
  # Traversing the Tree
60
59
  # -------------------
61
- # There are several method, which you can use to navigate throught the dom. The
62
- # most important is the {Dom::Node#[] children selector}.
60
+ # There are several method, which you can use to navigate throught the dom. The most important is
61
+ # the {Dom::Node#[] children selector}.
63
62
  #
64
63
  # The tree above could be traversed using the following operations:
65
64
  #
@@ -72,15 +71,14 @@ require_relative 'exceptions'
72
71
  #
73
72
  # The Root Node
74
73
  # -------------
75
- # The Dom inherits functionality from it's **root-node**. So all method's
76
- # invoked on the root node, can be expressed equivalent as member of the Dom.
74
+ # The Dom inherits functionality from it's **root-node**. So all method's invoked on the root node,
75
+ # can be expressed equivalent as member of the Dom.
77
76
  #
78
77
  # Dom.root[:some_child] <=> Dom[:some_child]
79
78
  # Dom.root.children <=> Dom.children
80
79
  # Dom.root.print_tree <=> Dom.print_tree
81
80
  #
82
- # Please note, that some methods of the root-node are hidden behind direct
83
- # implementations.
81
+ # Please note, that some methods of the root-node are hidden behind direct implementations.
84
82
  #
85
83
  # Dom.add_node != Dom.root.add_node
86
84
  #
@@ -137,33 +135,38 @@ module Dom
137
135
  # Reset the Dom to it's initial state by creating an empty {.root root-node}
138
136
  def self.clear
139
137
  @@root = NoDoc.new('__ROOT__')
138
+ @@docs = NoDoc.new('__DOCUMENTS__')
140
139
  end
141
140
 
142
141
  # @group Caching Methods
143
142
 
144
- # Serializes and dumps the complete Domtree (but without last_position) to the
145
- # specified `path`. If no `path` is given, the default `@@cache_path` will be
146
- # used instead.
143
+ # Serializes and dumps the complete Domtree to the specified `path`. If no `path` is given, the
144
+ # default `@@cache_path` will be used instead.
147
145
  #
148
- # This Method can be useful, to save a specific state of the Domtree to disk
149
- # and reuse it later, without the need to reconstruct it from zero.
146
+ # This Method can be useful, to save a specific state of the Domtree to disk and reuse it later,
147
+ # without the need to reconstruct it from zero.
150
148
  #
151
149
  # @note To recreate the Dom from the dump-file, use {.load}.
152
150
  #
153
151
  # @param [String] file the filepath, where to write the serialized data
154
152
  def self.dump(file = @@cache_path)
155
153
  File.open(file, 'w') do |f|
156
- f.write Marshal.dump @@root
154
+ f.write Marshal.dump({
155
+ :root => @@root,
156
+ :docs => @@docs
157
+ })
157
158
  end
158
159
  end
159
160
 
160
- # Loads the {.dump serialized Dom} and replaces the current root node with
161
- # the one created from the file.
161
+ # Loads the {.dump serialized Dom} and replaces the current root node with the one created from
162
+ # the file.
162
163
  #
163
164
  # @see .dump
164
165
  # @param [String] file the filepath from which to load the Dom
165
166
  def self.load(file = @@cache_path)
166
- @@root = Marshal.load(File.read(file))
167
+ dom = Marshal.load(File.read(file))
168
+ @@root = dom[:root]
169
+ @@docs = dom[:docs]
167
170
  end
168
171
 
169
172
  # @group Document Objects
data/lib/dom/no_doc.rb CHANGED
@@ -3,7 +3,7 @@ require_relative 'node'
3
3
  module Dom
4
4
 
5
5
  # NoDoc is used by {Dom} and {Dom::Node} to preserve the correct hierachy of
6
- # the tree, while inserting nodes with non existing parents.
6
+ # the tree, while inserting nodes with non (or not yet) existing parents.
7
7
  #
8
8
  # For example let's add the node 'foo.bar.baz' in our empty Dom. This will
9
9
  # result in the following tree:
@@ -12,7 +12,7 @@ module Dom
12
12
  # -bar (NoDoc)
13
13
  # -baz
14
14
  #
15
- # If a documented
15
+ # If a documented node with the same path is inserted, the NoDoc-node will be replaced by it.
16
16
  class NoDoc
17
17
  include Node
18
18
 
data/lib/dom/node.rb CHANGED
@@ -3,14 +3,13 @@ require_relative 'exceptions'
3
3
 
4
4
  module Dom
5
5
 
6
- # Node can be seen as an **aspect** or feature of another Object. Therefore it can
7
- # be mixed in to add node-functionality to a class.
6
+ # Node can be seen as an **aspect** or feature of another Object. Therefore it can be mixed in to
7
+ # add node-functionality to a class.
8
8
  # Such functionality is used by {CodeObject::Base code-objects} and {Document::Document documents}.
9
9
  #
10
10
  # Instance Variables
11
11
  # ------------------
12
- # The following instance-variables will be set while including Dom::Node into
13
- # your class:
12
+ # The following instance-variables will be set while including Dom::Node into your class:
14
13
  #
15
14
  # - `@name` (should be already set in your including class)
16
15
  # - `@parent`
@@ -39,8 +38,9 @@ module Dom
39
38
  # # -foo
40
39
  # # -bar
41
40
  #
42
- # @note including Class should implement instance-variable @name
41
+ # @note including Class should set instance-variable @name
43
42
  # @see CodeObject::Base
43
+ # @see Document::Document
44
44
  # @see Dom
45
45
  module Node
46
46
 
@@ -55,8 +55,8 @@ module Dom
55
55
 
56
56
  # @group Initialization
57
57
 
58
- # The 'constructor' of {Dom::Node}. It initializes all required
59
- # instance-variables (see {Dom::Node above}).
58
+ # The 'constructor' of {Dom::Node}. It initializes all required instance-variables.
59
+ # (see {Dom::Node above}).
60
60
  def initialize
61
61
  super
62
62
  @children, @parent = {}, nil
@@ -65,9 +65,8 @@ module Dom
65
65
 
66
66
  # @group Traversing
67
67
 
68
- # `node.parent` returns the parent {Dom::Node node}, if there is one.
69
- # If no parent exists `nil` is returned. In this case `node` can be
70
- # considered either as loose leave or root of the {Dom}.
68
+ # `node.parent` returns the parent {Dom::Node node}, if there is one. If no parent exists `nil`
69
+ # is returned. In this case `node` can be considered either as loose leave or root of the {Dom}.
71
70
  #
72
71
  # @return [Dom::Node] the parent node, if one exists
73
72
  def parent
@@ -75,9 +74,8 @@ module Dom
75
74
  end
76
75
 
77
76
 
78
- # searches all parents recursivly and returns an array, starting with the
79
- # highest order parent (excluding the {Dom.root}) and ending with the
80
- # immediate parent.
77
+ # searches all parents recursivly and returns an array, starting with the highest order parent
78
+ # (excluding the {Dom.root}) and ending with the immediate parent.
81
79
  #
82
80
  # @example
83
81
  # o1 = CodeObject::Base.new :foo
@@ -105,7 +103,8 @@ module Dom
105
103
  end
106
104
 
107
105
 
108
- # Get's the child of this node, which is identified by `path`
106
+ # Get's the child of this node, which is identified by `path`. Returns `nil? , if no matching
107
+ # child was found.
109
108
  #
110
109
  # @example childname
111
110
  # Dom[:Core] #=> #<CodeObject::Object:Core @parent=__ROOT__ …>
@@ -116,11 +115,11 @@ module Dom
116
115
  #
117
116
  # @overload [](childname)
118
117
  # @param [String, Symbol] childname
119
- # @return <Dom::Node>
118
+ # @return [Dom::Node, nil]
120
119
  #
121
120
  # @overload [](path)
122
121
  # @param [String] path The path to the desired node
123
- # @return <Dom::Node>
122
+ # @return [Dom::Node, nil]
124
123
  def [](path)
125
124
  return @children[path] if path.is_a? Symbol
126
125
  return @children[path.to_sym] if path.split(NS_SEP_STRING).size == 1
@@ -148,8 +147,7 @@ module Dom
148
147
  not (@children.nil? or @children.size == 0)
149
148
  end
150
149
 
151
- # Finds all siblings of the current node. If there is no parent, it will
152
- # return `nil`.
150
+ # Finds all siblings of the current node. If there is no parent, it will return `nil`.
153
151
  #
154
152
  # @return [Array<Dom::Node>, nil]
155
153
  def siblings
@@ -239,14 +237,15 @@ module Dom
239
237
  # @group Manipulation
240
238
 
241
239
  # There are three different cases
242
- # 1. Last Childnode i.e. ".foo"
243
- # -> Append node as child
244
- # 2. absolute path i.e. "Foo"
245
- # -> delegate path resolution to Dom.root
246
- # 3. relative path i.e. ".bar.foo"
247
- # a. if there is a matching child for first element, delegate
248
- # adding to this node
249
- # b. create NoDoc node and delegate rest to this NoDoc
240
+ #
241
+ # 1. Last Childnode i.e. ".foo"
242
+ # -> Append node as child
243
+ # 2. absolute path i.e. "Foo"
244
+ # -> delegate path resolution to Dom.root
245
+ # 3. relative path i.e. ".bar.foo"
246
+ # a. if there is a matching child for first element, delegate
247
+ # adding to this node
248
+ # b. create NoDoc node and delegate rest to this NoDoc
250
249
  #
251
250
  #
252
251
  # @overload add_node(path, node)
data/lib/helper/helper.rb CHANGED
@@ -4,58 +4,106 @@ require 'rdiscount'
4
4
 
5
5
  require_relative 'linker'
6
6
 
7
- # The Helpers are 'mixed' into your {Generator::Generator generator} and therefore can be used in all
8
- # template-views.
7
+ # The Helpers are 'mixed' into your {Generator::Generator generator} and therefore can be used in
8
+ # all template-views.
9
9
  # If you are searching for a method and don't know, where it may be implemented i suggest the
10
10
  # following inheritence chain as your search-strategy:
11
11
  #
12
- # Helper::IncludedHelpers Generator::YourGenerator Generator::Generator Renderer
12
+ # Helper::IncludedHelpers -> Generator::YourGenerator -> Generator::Generator -> Renderer
13
13
  #
14
14
  # Somewhere at that chain you will find your desired function.
15
15
  module Helper
16
16
 
17
17
  # The Helper-methods in this module are globally used one and should not depend on the template
18
- # you are using. You will find many html-helpers around here.
18
+ # you are using. You will find many html-helpers around here, that are inspired by rails.
19
19
  module Helper
20
20
 
21
21
  include Linker
22
22
 
23
- def tag(sym, content = "", attrs = {})
23
+ # Creates a HTML-Tag and adds the attributes, specified with `attrs`
24
+ #
25
+ # @todo FIXME - not working with block, yet...
26
+ # Rails incorporates a `capture` method, which captures the ERB-output of a block
27
+ # maybe we can use something like that
28
+ # `with_output_buffer` {http://apidock.com/rails/ActionView/Helpers/CaptureHelper/with_output_buffer}
29
+ #
30
+ # @example
31
+ # tag :a, 'Hello', :href => 'http://foobar.com', :class => 'red_one'
32
+ # #=> <a href="http://foobar.com" class="red_one">Hello</a>
33
+ #
34
+ # @param [Symbol, String] tagname
35
+ # @param [String] content
36
+ # @param [Hash<Symbol, String>] attrs
37
+ # @return [String] html-tag
38
+ #
39
+ # @see #attributize
40
+ def tag(tagname, content = "", attrs = {})
24
41
 
25
- # @todo FIXME
42
+ # Not working with blocks!
26
43
  if block_given?
27
- _erbout << "<#{sym.to_s} #{attributize(content)}>"
44
+ _erbout << "<#{tagname.to_s} #{attributize(content)}>"
28
45
  content = yield
29
- _erbout << "</#{sym.to_s}>"
46
+ _erbout << "</#{tagname.to_s}>"
30
47
  else
31
- "<#{sym.to_s} #{attributize(attrs)}>#{content}</#{sym.to_s}>"
48
+ "<#{tagname.to_s} #{attributize(attrs)}>#{content}</#{tagname.to_s}>"
32
49
  end
33
50
  end
34
51
 
35
- def truncate(string, num = 150)
36
- if string.length > num
37
- string[0..num] + " &hellip;"
38
- else
39
- string
40
- end
52
+ # Shortens the given string to the specified length and adds '...'
53
+ def truncate(string, length = 150)
54
+ string.length <= length ? string : string[0..length] + " &hellip;"
41
55
  end
42
56
 
57
+ # Creates a css-link tag for each input string. The resource will be linked relativly.
58
+ #
59
+ # @example
60
+ # style 'foo', 'bar'
61
+ # #=> <link rel='stylesheet' href='../css/foo.css'/>
62
+ # #=> <link rel='stylesheet' href='../css/bar.css'/>
63
+ #
64
+ # @param [String] basename of the css-file (without extension)
65
+ # @return [String] html-element to include the css-file
43
66
  def style(*args)
44
- html = ""
45
- args.each do |path|
46
- html += tag :link, "", :rel => 'stylesheet', :href => to_relative('css/'+path+'.css')
47
- end
48
- return html
67
+ args.map do |path|
68
+ tag :link, "", :rel => 'stylesheet', :href => to_relative('css/'+path+'.css')
69
+ end.join ''
49
70
  end
50
71
 
72
+ # Creates a javascript-tag for each input string to import the script. The resource will be
73
+ # linked relativly.
74
+ #
75
+ # @example
76
+ # script 'foo', 'bar'
77
+ # #=> <script href='../js/foo.js'/>
78
+ # #=> <script href='../js/bar.js'/>
79
+ #
80
+ # @todo because those js-files are all relative links, they could be joined together and packed
81
+ # afterwards
82
+ #
83
+ # @param [String] basename of the javascript-file (without extension)
84
+ # @return [String] html-element to include the javascript-file
51
85
  def script(*args)
52
- html = ""
53
- args.each do |path|
54
- html += tag :script, "", :src => to_relative('js/'+path+'.js')
55
- end
56
- return html
86
+ args.map do |path|
87
+ tag :script, "", :src => to_relative('js/'+path+'.js')
88
+ end.join ''
57
89
  end
58
90
 
91
+ # Removes intendation from the sources and generates a code-tag with all the required classes
92
+ # to make the javascript-syntax-highlighter work.
93
+ #
94
+ # @example
95
+ # code " function() {}"
96
+ # #=> <code class="brush:js first-line:1">function(){}</code>
97
+ #
98
+ # @example
99
+ # code " function() {}", :firstline => 15
100
+ # #=> <code class="brush:js first-line:15">function(){}</code>
101
+ #
102
+ # @param [String] source
103
+ # @param [Hash] opts
104
+ # @option opts [Numeric] :firstline (1) The line-numeration will start with that number
105
+ # @option opts [String] :class ("block") A optional css-class which can be added
106
+ # @return [String] the html-code-element
59
107
  def code(source, opts = {})
60
108
 
61
109
  # defaults
@@ -66,21 +114,24 @@ module Helper
66
114
  intendation = source.lines.map {|line| line.match(/(^\s+)/) && line.match(/(^\s+)/).captures.first.size || 0 }.min
67
115
 
68
116
  # @todo there has to be a better way for that
69
- tag :code, h(source.lines.map { |line| line[intendation .. line.size] }.join("")), :class => "#{opts[:class]} brush:js first-line:#{opts[:firstline]}"
70
- end
71
-
72
- def to_html(markdown_text, *markdown_opts)
73
- replace_links RDiscount.new(markdown_text, *markdown_opts).to_html
74
- end
75
-
76
- def toc(markdown_text)
77
- RDiscount.new(markdown_text, :generate_toc).toc_content
117
+ tag :code, h(source.lines.map {
118
+ |line| line[intendation .. line.size]
119
+ }.join("")), :class => "#{opts[:class]} brush:js first-line:#{opts[:firstline]}"
78
120
  end
79
121
 
122
+ # Escapes any html-elements in the given string
123
+ #
124
+ # @param [String] to_escape
125
+ # @return [String] the escaped string
80
126
  def h(to_escape)
81
127
  CGI.escapeHTML(to_escape)
82
128
  end
83
129
 
130
+ # Takes an absolute path and converts it to a relative one, comparing it to the **current
131
+ # output path**.
132
+ #
133
+ # @param [String] path
134
+ # @return [String] relative path
84
135
  def to_relative(path)
85
136
 
86
137
  path = Pathname.new(path)
@@ -95,8 +146,44 @@ module Helper
95
146
 
96
147
  Logger.debug "Relative path '#{path}' from '#{base}'"
97
148
  path.relative_path_from(base).to_s
98
- end
99
-
149
+ end
150
+
151
+ # To visually group the tokens you can specify an area. All tokens for one area (`:sidebar` in this
152
+ # example) will be collected and can be rendered in the view-templates with the
153
+ # {Helper::Helper#render_tokens render_tokens} helper-method.
154
+ #
155
+ # render_tokens :of => @code_object, :in => :sidebar
156
+ #
157
+ # While {Token::Handler.register registering a new token} you can use any symbol for `area`. But your tokens may not appear in
158
+ # the rendered html-documentation, unless you explicitly call `render_tokens` for each area.
159
+ #
160
+ # The default-templates make use of the following areas:
161
+ #
162
+ # - :notification
163
+ # - :body
164
+ # - :sidebar
165
+ # - :footnote
166
+ #
167
+ # If you don't want your token to be rendered at all, you can use `:none` as value for `area`.
168
+ #
169
+ # register :your_token, :area => :none
170
+ #
171
+ # @example render tokens of notification-area
172
+ # render_tokens :of => code_object, :in => :notification
173
+ #
174
+ # @example exclude `@deprecated`-Tokens from output
175
+ # render_tokens :of => code_object, :in => :body, :without => [:deprecated]
176
+ #
177
+ # @example use special default-template
178
+ # render_tokens :of => code_object, :in => :sidebar, :template => 'sidebar'
179
+ #
180
+ # @param [Hash] opts
181
+ # @option opts [CodeObject::Base] :of The object, which contains the tokens, to be rendered
182
+ # @option opts [Symbol] :area The area to filter the tokens for
183
+ # @option opts [Array<Symbol>, nil] :without Tokennames to be excluded from the output
184
+ # @option opts [Symbol, String, nil] :template If you wan't to overwrite the default template
185
+ # you can use this option. (Note: templates, specified at token-registration have higher
186
+ # precedence, than this option)
100
187
  def render_tokens(opts = {})
101
188
 
102
189
  code_object = opts[:of] or raise Exception.new("Parameter :of (CodeObject) required")
@@ -124,9 +211,47 @@ module Helper
124
211
  rendered
125
212
  end
126
213
 
214
+ # Takes a hash as input and returns a string, which can be included in a html tag.
215
+ #
216
+ # @example
217
+ # attributize :style => 'border: none;', :class => 'foo' #=> 'style="border: none;" class="foo"'
218
+ #
219
+ # @param [Hash] hash
220
+ # @return [String]
127
221
  def attributize(hash)
128
222
  hash.map{|k,v| "#{k}=\"#{v}\""}.join ' '
129
223
  end
130
224
 
225
+ # @group Markdown
226
+
227
+ # Converts input text to html using RDiscount. Afterwards all contained links are resolved and
228
+ # replaced.
229
+ #
230
+ # More information about the markdown_opts can be found at the
231
+ # {http://rubydoc.info/github/rtomayko/rdiscount/master/RDiscount RDiscount-rdoc-page}.
232
+ #
233
+ # @param [String] markdown_text plain text with markdown-markup
234
+ # @param [Symbol, nil] markdown_opts
235
+ # @return [String] converted html
236
+ def to_html(markdown_text, *markdown_opts)
237
+ replace_links RDiscount.new(markdown_text, *markdown_opts).to_html
238
+ end
239
+
240
+ # Can be used to generate a table of contents out of a markdown-text.
241
+ # The generated toc contains links to the document-headlines.
242
+ # To make this links actually work you need to process the document with the
243
+ # :generate_toc flag, too.
244
+ #
245
+ # @example
246
+ # <%= toc(my_text) %>
247
+ # ...
248
+ # <%= to_html my_text, :generate_toc %>
249
+ #
250
+ # @param [String] markdown_text
251
+ # @return [String] html table of contents
252
+ def toc(markdown_text)
253
+ RDiscount.new(markdown_text, :generate_toc).toc_content
254
+ end
255
+
131
256
  end
132
257
  end