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

Doing the comment-to-markdown conversion only once.

Created ClassFormatter for doing this.

This speeds things up about 2x.
parent dbc10fe3
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ require 'jsduck/aggregator'
require 'jsduck/source_file'
require 'jsduck/source_writer'
require 'jsduck/doc_formatter'
require 'jsduck/class_formatter'
require 'jsduck/class'
require 'jsduck/icons'
require 'jsduck/search_data'
@@ -45,6 +46,7 @@ module JsDuck
      parsed_files = @timer.time(:parsing) { parallel_parse(@opts.input_files) }
      result = @timer.time(:aggregating) { aggregate(parsed_files) }
      @relations = @timer.time(:aggregating) { filter_classes(result) }
      @timer.time(:generating) { format_classes }
      Aliases.new(@relations).resolve_all
      Lint.new(@relations).run

@@ -145,6 +147,19 @@ module JsDuck
      Relations.new(classes, @opts.external_classes)
    end

    # Formats each class
    def format_classes
      formatter = ClassFormatter.new(@relations, get_doc_formatter)
      # Format all doc-objects in parallel
      formatted_docs = @parallel.map(@relations.classes) do |cls|
        formatter.format(cls.internal_doc)
      end
      # Then merge the data back to classes sequentially
      formatted_docs.each do |doc|
        @relations[doc[:name]].internal_doc = doc
      end
    end

    # Writes classes, guides, videos, and search data to one big .js file
    def write_app_data
      js = "Docs.data = " + JsonDuck.generate({
@@ -159,7 +174,7 @@ module JsDuck

    # Writes JSON export or JsonP file for each class
    def write_classes
      exporter = Exporter.new(@relations, get_doc_formatter)
      exporter = Exporter.new(@relations)
      renderer = Renderer.new
      @parallel.each(@relations.classes) do |cls|
        filename = @opts.output_dir+"/output/" + cls[:name] + (@opts.export ? ".json" : ".js")
+10 −0
Original line number Diff line number Diff line
@@ -15,6 +15,16 @@ module JsDuck
      @relations = nil
    end

    # Accessors for internal doc object.  These are used to run
    # ClassFormatter on the internal doc object and then assign it
    # back.
    def internal_doc
      @doc
    end
    def internal_doc=(doc)
      @doc = doc
    end

    def [](key)
      @doc[key]
    end
+63 −0
Original line number Diff line number Diff line
module JsDuck

  # Converts :doc properties of class from markdown to HTML and resolves @links.
  # Also removes private members.
  class ClassFormatter
    def initialize(relations, formatter)
      @relations = relations
      @formatter = formatter
    end

    # Runs the formatter on doc object of a class.
    # Accessed using Class#internal_doc
    def format(cls)
      @formatter.class_context = cls[:name]
      @formatter.doc_context = cls
      cls[:doc] = @formatter.format(cls[:doc]) if cls[:doc]
      cls[:members].each_pair do |type, members|
        cls[:members][type] = members.reject {|m| m[:private] }.map {|m| format_member(m) }
      end
      cls[:statics].each_pair do |type, members|
        cls[:statics][type] = members.reject {|m| m[:private] }.map {|m| format_member(m) }
      end
      cls
    end

    def format_member(m)
      @formatter.doc_context = m
      m[:doc] = @formatter.format(m[:doc]) if m[:doc]
      m[:deprecated][:text] = @formatter.format(m[:deprecated][:text]) if m[:deprecated]
      if m[:params] || (m[:properties] && m[:properties].length > 0) || m[:default] || @formatter.too_long?(m[:doc])
        m[:shortDoc] = @formatter.shorten(m[:doc])
      end
      m[:params] = format_params(m[:params]) if m[:params]
      m[:return] = format_return(m[:return]) if m[:return]
      m[:properties] = format_subproperties(m[:properties]) if m[:properties]
      m
    end

    def format_params(params)
      params.map do |p|
        p[:doc] = @formatter.format(p[:doc]) if p[:doc]
        p[:properties] = format_subproperties(p[:properties]) if p[:properties]
        p
      end
    end

    def format_return(r)
      r[:doc] = @formatter.format(r[:doc]) if r[:doc]
      r[:properties] = format_subproperties(r[:properties]) if r[:properties]
      r
    end

    def format_subproperties(items)
      items.map do |it|
        it[:doc] = @formatter.format(it[:doc]) if it[:doc]
        it[:properties] = format_subproperties(it[:properties]) if it[:properties]
        it
      end
    end

  end

end
+2 −61
Original line number Diff line number Diff line
require 'jsduck/doc_formatter'

module JsDuck

  # Export class data as hash with :cfg being replace with :cfgs and
  # including all the inherited members too.  Same for :properties,
  # :methods, and :events.
  #
  # Also all the :doc elements will be formatted - converted from
  # markdown to HTML and @links resolved.
  class Exporter
    def initialize(relations, formatter)
    def initialize(relations)
      @relations = relations
      @formatter = formatter
    end

    # Returns all data in Class object as hash.
@@ -27,60 +21,7 @@ module JsDuck
      h[:subclasses] = @relations.subclasses(cls).collect {|c| c.full_name }
      h[:mixedInto] = @relations.mixed_into(cls).collect {|c| c.full_name }
      h[:allMixins] = cls.all_mixins.collect {|c| c.full_name }
      format_class(h)
    end

    # converts :doc properties from markdown to html and resolve @links
    def format_class(c)
      @formatter.class_context = c[:name]
      @formatter.doc_context = c
      c[:doc] = @formatter.format(c[:doc]) if c[:doc]
      c[:members].each_pair do |type, members|
        c[:members][type] = members.map {|m| format_member(m) }
      end
      c[:statics].each_pair do |type, members|
        c[:statics][type] = members.map {|m| format_member(m) }
      end
      c
    end

    def format_member(m)
      m = m.clone
      @formatter.doc_context = m
      m[:doc] = @formatter.format(m[:doc]) if m[:doc]
      m[:deprecated][:text] = @formatter.format(m[:deprecated][:text]) if m[:deprecated]
      if m[:params] || (m[:properties] && m[:properties].length > 0) || m[:default] || @formatter.too_long?(m[:doc])
        m[:shortDoc] = @formatter.shorten(m[:doc])
      end
      m[:params] = format_params(m[:params]) if m[:params]
      m[:return] = format_return(m[:return]) if m[:return]
      m[:properties] = format_subproperties(m[:properties]) if m[:properties]
      m
    end

    def format_params(params)
      params.map do |p|
        p = p.clone
        p[:doc] = @formatter.format(p[:doc]) if p[:doc]
        p[:properties] = format_subproperties(p[:properties]) if p[:properties]
        p
      end
    end

    def format_return(r)
      r = r.clone
      r[:doc] = @formatter.format(r[:doc]) if r[:doc]
      r[:properties] = format_subproperties(r[:properties]) if r[:properties]
      r
    end

    def format_subproperties(items)
      items.map do |it|
        it = it.clone
        it[:doc] = @formatter.format(it[:doc]) if it[:doc]
        it[:properties] = format_subproperties(it[:properties]) if it[:properties]
        it
      end
      h
    end

  end