Commit c31e3a10 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Refactor the DocFormatter class a bit.

- Constructor now takes just opts and relations is passed in separately
  through accessor.
- Remove old leftover: @images variable.
parent 04e04a1a
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -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
+5 −2
Original line number Diff line number Diff line
@@ -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:
+23 −22
Original line number Diff line number Diff line
@@ -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 "<pre>" 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 <pre>-block and goes on parsing it as
      # normal Markdown, which often causes nested <pre>-blocks.
      #
      # To prevent this, we always add extra newline before <pre>.
      input.gsub!(/([^\n])<pre>/, "\\1\n<pre>")

      # But we remove trailing newline after <pre> to prevent
      # code-blocks beginning with empty line.
      input.gsub!(/<pre>(<code>)?\n?/, "<pre>\\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 "<pre>" 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 <pre>-block and goes on parsing it as
      # normal Markdown, which often causes nested <pre>-blocks.
      #
      # To prevent this, we always add extra newline before <pre>.
      input.gsub!(/([^\n])<pre>/, "\\1\n<pre>")

      # But we remove trailing newline after <pre> to prevent
      # code-blocks beginning with empty line.
      input.gsub!(/<pre>(<code>)?\n?/, "<pre>\\1")

      replace(RDiscount.new(input).to_html)
    end

  end

end
+2 −2
Original line number Diff line number Diff line
@@ -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 == '<a href="String">string</a>'
@@ -355,4 +356,3 @@ describe JsDuck::TypeParser do
  end

end