From c31e3a10538da8c9fdf8e702c29c8d01aa89fd73 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Thu, 13 Sep 2012 13:36:42 +0300 Subject: [PATCH] Refactor the DocFormatter class a bit. - Constructor now takes just opts and relations is passed in separately through accessor. - Remove old leftover: @images variable. --- lib/jsduck/app.rb | 3 ++- lib/jsduck/assets.rb | 7 ++++-- lib/jsduck/doc_formatter.rb | 45 +++++++++++++++++++------------------ spec/type_parser_spec.rb | 4 ++-- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/lib/jsduck/app.rb b/lib/jsduck/app.rb index 778e6006..1477f318 100644 --- a/lib/jsduck/app.rb +++ b/lib/jsduck/app.rb @@ -165,7 +165,8 @@ module JsDuck # Formats each class def format_classes - doc_formatter = DocFormatter.new(@relations, @opts) + doc_formatter = DocFormatter.new(@opts) + doc_formatter.relations = @relations doc_formatter.img_path = "images" class_formatter = ClassFormatter.new(@relations, doc_formatter) # Don't format types when exporting diff --git a/lib/jsduck/assets.rb b/lib/jsduck/assets.rb index f4c4bf13..51497b4d 100644 --- a/lib/jsduck/assets.rb +++ b/lib/jsduck/assets.rb @@ -26,12 +26,15 @@ module JsDuck @relations = relations @opts = opts + doc_formatter = DocFormatter.new(@opts) + doc_formatter.relations = @relations + @images = Images.new(@opts.images) @welcome = Welcome.create(@opts.welcome) - @guides = Guides.create(@opts.guides, DocFormatter.new(@relations, @opts), @opts) + @guides = Guides.create(@opts.guides, doc_formatter, @opts) @videos = Videos.create(@opts.videos) @examples = Examples.create(@opts.examples, @opts) - @categories = Categories.create(@opts.categories_path, DocFormatter.new(@relations, @opts), @relations) + @categories = Categories.create(@opts.categories_path, doc_formatter, @relations) end # Writes out the assets that can be written out separately: diff --git a/lib/jsduck/doc_formatter.rb b/lib/jsduck/doc_formatter.rb index bf8e77b7..240b986a 100644 --- a/lib/jsduck/doc_formatter.rb +++ b/lib/jsduck/doc_formatter.rb @@ -11,11 +11,11 @@ module JsDuck # Formats doc-comments class DocFormatter - def initialize(relations={}, opts={}) - @images = [] - + # Creates a formatter configured with options originating from + # command line. For the actual effect of the options see + # Inline::* classes. + def initialize(opts={}) @inline_link = Inline::Link.new(opts) - @inline_link.relations = relations @inline_img = Inline::Img.new(opts) @inline_video = Inline::Video.new(opts) @inline_example = Inline::Example.new(opts) @@ -59,6 +59,24 @@ module JsDuck @inline_link.relations = relations end + # Formats doc-comment for placement into HTML. + # Renders it with Markdown-formatter and replaces @link-s. + def format(input) + # In ExtJS source "
" is often at the end of paragraph, not
+      # on its own line.  But in that case RDiscount doesn't recognize
+      # it as the beginning of 
-block and goes on parsing it as
+      # normal Markdown, which often causes nested 
-blocks.
+      #
+      # To prevent this, we always add extra newline before 
.
+      input.gsub!(/([^\n])
/, "\\1\n
")
+
+      # But we remove trailing newline after 
 to prevent
+      # code-blocks beginning with empty line.
+      input.gsub!(/
()?\n?/, "
\\1")
+
+      replace(RDiscount.new(input).to_html)
+    end
+
     # Replaces {@link} and {@img} tags, auto-generates links for
     # recognized classnames.
     #
@@ -111,6 +129,7 @@ module JsDuck
           out += open_a_tags > 0 ? text : @inline_link.create_magic_links(text)
         end
       end
+
       out
     end
 
@@ -119,24 +138,6 @@ module JsDuck
       @inline_link.link(cls, member, anchor_text, type, static)
     end
 
-    # Formats doc-comment for placement into HTML.
-    # Renders it with Markdown-formatter and replaces @link-s.
-    def format(input)
-      # In ExtJS source "
" is often at the end of paragraph, not
-      # on its own line.  But in that case RDiscount doesn't recognize
-      # it as the beginning of 
-block and goes on parsing it as
-      # normal Markdown, which often causes nested 
-blocks.
-      #
-      # To prevent this, we always add extra newline before 
.
-      input.gsub!(/([^\n])
/, "\\1\n
")
-
-      # But we remove trailing newline after 
 to prevent
-      # code-blocks beginning with empty line.
-      input.gsub!(/
()?\n?/, "
\\1")
-
-      replace(RDiscount.new(input).to_html)
-    end
-
   end
 
 end
diff --git a/spec/type_parser_spec.rb b/spec/type_parser_spec.rb
index 782e08d8..fd814e8d 100644
--- a/spec/type_parser_spec.rb
+++ b/spec/type_parser_spec.rb
@@ -321,7 +321,8 @@ describe JsDuck::TypeParser do
 
     it "links primitive types to classes" do
       relations = JsDuck::Relations.new([JsDuck::Class.new({:name => "String"})])
-      doc_formatter = JsDuck::DocFormatter.new(relations)
+      doc_formatter = JsDuck::DocFormatter.new
+      doc_formatter.relations = relations
       p = JsDuck::TypeParser.new(relations, doc_formatter)
       p.parse("string")
       p.out.should == 'string'
@@ -355,4 +356,3 @@ describe JsDuck::TypeParser do
   end
 
 end
-
-- 
GitLab