Sha256: e9776c6d595b7a9315f1aa41c7ad3be84a32b069f60af772a6ced0b1ce553414

Contents?: true

Size: 1.78 KB

Versions: 13

Compression:

Stored size: 1.78 KB

Contents

require 'jsduck/util/html'
require 'jsduck/logger'
require 'jsduck/type_parser'

module JsDuck
  module Format

    # Helper for recursively formatting subproperties.
    class Subproperties

      def initialize(formatter)
        @formatter = formatter
        @skip_type_parsing = false
      end

      # Set to true to skip parsing and formatting of types.
      # Used to skip parsing of CSS typesdefs.
      attr_accessor :skip_type_parsing

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

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

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

      # Formats the given type definition string using TypeParser.
      #
      # - 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)
        # Skip the formatting entirely when type-parsing is turned off.
        return Util::HTML.escape(type) if @skip_type_parsing

        tp = TypeParser.new(@formatter)
        if tp.parse(type)
          tp.out
        else
          context = @formatter.doc_context
          if tp.error == :syntax
            Logger.warn(:type_syntax, "Incorrect type syntax #{type}", context)
          else
            Logger.warn(:type_name, "Unknown type #{type}", context)
          end
          Util::HTML.escape(type)
        end
      end

    end

  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
jsduck-5.3.4 lib/jsduck/format/subproperties.rb
jsduck-5.3.3 lib/jsduck/format/subproperties.rb
jsduck-5.3.2 lib/jsduck/format/subproperties.rb
jsduck-5.3.1 lib/jsduck/format/subproperties.rb
jsduck-5.3.0 lib/jsduck/format/subproperties.rb
jsduck-5.2.0 lib/jsduck/format/subproperties.rb
jsduck-5.1.0 lib/jsduck/format/subproperties.rb
jsduck-5.0.1 lib/jsduck/format/subproperties.rb
jsduck-5.0.0 lib/jsduck/format/subproperties.rb
jsduck-5.0.0.beta5 lib/jsduck/format/subproperties.rb
jsduck-5.0.0.beta4 lib/jsduck/format/subproperties.rb
jsduck-5.0.0.beta3 lib/jsduck/format/subproperties.rb
jsduck-5.0.0.beta2 lib/jsduck/format/subproperties.rb