Commit 9e6a2f06 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Move subproperties formatting into Tag classes.

The instantce of Format::Subproperties however is now created inside the
Format::Doc class, which also has methods #format_subproperty
and #format_type that delegate to Format::Subproperties class.
parent 48becb04
Loading
Loading
Loading
Loading
+1 −5
Original line number Diff line number Diff line
@@ -49,11 +49,7 @@ module JsDuck
        doc_formatter = Format::Doc.new(relations, opts)
        doc_formatter.images = Img::DirSet.new(opts.images, "images")

        class_formatter = Format::Class.new(doc_formatter)
        # Don't format types when exporting
        class_formatter.include_types = !opts.export

        class_formatter
        return Format::Class.new(doc_formatter)
      end

    end
+0 −22
Original line number Diff line number Diff line
require 'jsduck/tag_registry'
require 'jsduck/format/subproperties'

module JsDuck
  module Format
@@ -7,12 +6,8 @@ module JsDuck
    # Converts :doc properties of class from markdown to HTML, resolves
    # @links, and converts type definitions to HTML.
    class Class
      # Set to false to disable HTML-formatting of type definitions.
      attr_accessor :include_types

      def initialize(formatter)
        @formatter = formatter
        @include_types = true
      end

      # Runs the formatter on doc object of a class.
@@ -40,23 +35,6 @@ module JsDuck
        @formatter.doc_context = m[:files][0]

        format_tags(m)

        subs = make_subproperties_formatter(m)

        m[:html_type] = subs.format_type(m[:type]) if m[:type]
        m[:params].each {|p| subs.format(p) } if m[:params]
        subs.format(m[:return]) if m[:return]
        m[:throws].each {|t| subs.format(t) } if m[:throws]
        m[:properties].each {|p| subs.format(p) } if m[:properties]
      end

      def make_subproperties_formatter(m)
        # We don't validate and format CSS var and mixin type definitions
        is_css_tag = m[:tagname] == :css_var || m[:tagname] == :css_mixin
        # Also don't check types when @include_types setting is explicitly turned off
        check_types = @include_types && !is_css_tag

        return Format::Subproperties.new(@formatter, check_types)
      end

      def format_tags(context)
+15 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ require 'rubygems'
require 'strscan'
require 'rdiscount'
require 'jsduck/format/html_stack'
require 'jsduck/format/subproperties'
require 'jsduck/inline/link'
require 'jsduck/inline/auto_link'
require 'jsduck/inline/link_renderer'
@@ -21,6 +22,7 @@ module JsDuck
      def initialize(relations={}, opts={})
        @relations = relations
        @opts = opts
        @subproperties = Format::Subproperties.new(self, opts)
        @link_renderer = Inline::LinkRenderer.new(relations, opts)
        @inline_link = Inline::Link.new(@link_renderer)
        @auto_link = Inline::AutoLink.new(@link_renderer)
@@ -142,6 +144,19 @@ module JsDuck
        @link_renderer.link(cls, member, anchor_text, type, static)
      end

      # Recursively formats a subproperty.
      # See Format::Subproperties#format for details.
      def format_subproperty(item, skip_types=false)
        @subproperties.format(item, skip_types)
      end

      # Parses and formats type definition.
      # Returns HTML-rendering of the type.
      # See Format::Subproperties#format_type for details.
      def format_type(type, skip_types=false)
        @subproperties.format_type(type, skip_types)
      end

    end

  end
+8 −7
Original line number Diff line number Diff line
@@ -8,9 +8,10 @@ module JsDuck
    # Helper for recursively formatting subproperties.
    class Subproperties

      def initialize(formatter, format_types=false)
      def initialize(formatter, opts={})
        @formatter = formatter
        @format_types = format_types
        # Don't format types when exporting
        @skip_types = !!opts[:export]
      end

      # Takes a hash of param, return value, throws value or subproperty.
@@ -19,15 +20,15 @@ module JsDuck
      # - Parses the :type field and saves HTML to :html_type.
      # - Recursively does the same with all items in :properties field.
      #
      def format(item)
      def format(item, skip_types=false)
        item[:doc] = @formatter.format(item[:doc]) if item[:doc]

        if item[:type]
          item[:html_type] = format_type(item[:type])
          item[:html_type] = format_type(item[:type], skip_types)
        end

        if item[:properties]
          item[:properties].each {|p| format(p) }
          item[:properties].each {|p| format(p, skip_types) }
        end
      end

@@ -36,9 +37,9 @@ module JsDuck
      # - On success returns HTML-version of the type definition.
      # - On failure logs error and returns the type string with only HTML escaped.
      #
      def format_type(type)
      def format_type(type, skip_types=false)
        # Skip the formatting entirely when type-parsing is turned off.
        return Util::HTML.escape(type) unless @format_types
        return Util::HTML.escape(type) if @skip_types || skip_types

        tp = TypeParser.new(@formatter)
        if tp.parse(type)
+7 −0
Original line number Diff line number Diff line
@@ -32,6 +32,13 @@ module JsDuck::Tag
      h[:params] = merge_params(docs, code)
    end

    def format(m, formatter)
      # We don't validate and format CSS var and mixin type definitions
      skip_type = m[:tagname] == :css_var || m[:tagname] == :css_mixin

      m[:params].each {|p| formatter.format_subproperty(p, skip_type) }
    end

    def to_html(m)
      JsDuck::Render::Subproperties.render_params(m[:params]) if m[:params].length > 0
    end
Loading