Class: JsDuck::ClassFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/class_formatter.rb

Overview

Converts :doc properties of class from markdown to HTML, resolves @links, and converts type definitions to HTML.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relations, formatter) ⇒ ClassFormatter

Returns a new instance of ClassFormatter.



13
14
15
16
17
18
19
# File 'lib/jsduck/class_formatter.rb', line 13

def initialize(relations, formatter)
  @relations = relations
  @formatter = formatter
  # inject formatter to all meta-tags
  MetaTagRegistry.instance.formatter = formatter
  @include_types = true
end

Instance Attribute Details

#include_typesObject

Set to false to disable HTML-formatting of type definitions.



11
12
13
# File 'lib/jsduck/class_formatter.rb', line 11

def include_types
  @include_types
end

Instance Method Details

#expandable?(m) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/jsduck/class_formatter.rb', line 56

def expandable?(m)
  m[:params] || (m[:properties] && m[:properties].length > 0) || m[:default] || m[:meta][:deprecated] || m[:meta][:template]
end

#format(cls) ⇒ Object

Runs the formatter on doc object of a class. Accessed using Class#internal_doc



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jsduck/class_formatter.rb', line 23

def format(cls)
  @cls = cls
  @formatter.class_context = cls[:name]
  @formatter.doc_context = cls[:files][0]
  cls[:doc] = @formatter.format(cls[:doc]) if cls[:doc]
  [:members, :statics].each do |group|
    cls[group].each_pair do |type, members|
      # format all members (except hidden ones)
      cls[group][type] = members.map {|m| m[:meta][:hide] ? m : format_member(m)  }
    end
  end
  cls[:html_meta] = (cls)
  cls
end

#format_item(it, is_css_tag) ⇒ Object



60
61
62
63
64
65
# File 'lib/jsduck/class_formatter.rb', line 60

def format_item(it, is_css_tag)
  it[:doc] = @formatter.format(it[:doc]) if it[:doc]
  it[:html_type] = (@include_types && !is_css_tag) ? format_type(it[:type]) : it[:type] if it[:type]
  it[:properties] = it[:properties].map {|s| format_item(s, is_css_tag) } if it[:properties]
  it
end

#format_member(m) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jsduck/class_formatter.rb', line 38

def format_member(m)
  @formatter.doc_context = m[:files][0]
  m[:doc] = @formatter.format(m[:doc]) if m[:doc]
  if expandable?(m) || @formatter.too_long?(m[:doc])
    m[:shortDoc] = @formatter.shorten(m[:doc])
  end

  # We don't validate and format CSS var and mixin type definitions
  is_css_tag = m[:tagname] == :css_var || m[:tagname] == :css_mixin

  m[:html_type] = (@include_types && !is_css_tag) ? format_type(m[:type]) : m[:type] if m[:type]
  m[:params] = m[:params].map {|p| format_item(p, is_css_tag) } if m[:params]
  m[:return] = format_item(m[:return], is_css_tag) if m[:return]
  m[:properties] = m[:properties].map {|b| format_item(b, is_css_tag) } if m[:properties]
  m[:html_meta] = (m)
  m
end

#format_meta_data(context) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jsduck/class_formatter.rb', line 82

def (context)
  result = {}
  context[:meta].each_pair do |key, value|
    if value
      tag = MetaTagRegistry.instance[key]
      tag.context = context
      result[key] = tag.to_html(value)
    end
  end
  result
end

#format_type(type) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jsduck/class_formatter.rb', line 67

def format_type(type)
  tp = TypeParser.new(@relations, @formatter)
  if tp.parse(type)
    tp.out
  else
    context = @formatter.doc_context
    if tp.error == :syntax
      Logger.instance.warn(:type_syntax, "Incorrect type syntax #{type}", context[:filename], context[:linenr])
    else
      Logger.instance.warn(:type_name, "Unknown type #{type}", context[:filename], context[:linenr])
    end
    type
  end
end