Method: JsDuck::Format::Doc#replace

Defined in:
lib/jsduck/format/doc.rb

#replace(input) ⇒ Object

Replaces @link and @img tags, auto-generates links for recognized classnames.

Replaces Class#member link text in given string with HTML from @link_tpl.

Replaces path/to/image.jpg Alt text with HTML from @img_tpl.

Adds ‘inline-example’ class to code examples beginning with @example.

Additionally replaces strings recognized as ClassNames or #members with links to these classes or members. So one doesn’t even need to use the @link tag to create a link.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/jsduck/format/doc.rb', line 100

def replace(input)
  s = StringScanner.new(input)
  out = ""

  # Keep track of open HTML tags. We're not auto-detecting class
  # names when inside <a>. Also we want to close down the unclosed
  # tags.
  tags = Format::HtmlStack.new(@opts[:ignore_html] || {}, @doc_context)

  while !s.eos? do
    if substitute = @inline_link.replace(s)
      out += substitute
    elsif substitute = @inline_img.replace(s)
      out += substitute
    elsif substitute = @inline_video.replace(s)
      out += substitute
    elsif s.check(/[{]/)
      # There might still be "{" that doesn't begin {@link} or {@img} - ignore it
      out += s.scan(/[{]/)
    elsif substitute = @inline_example.replace(s)
      tags.push_tag("pre")
      tags.push_tag("code")
      out += substitute
    elsif s.check(/<\w/)
      # Open HTML tag
      out += tags.open(s)
    elsif s.check(/<\/\w+>/)
      # Close HTML tag
      out += tags.close(s)
    elsif s.check(/</)
      # Ignore plain '<' char.
      out += s.scan(/</)
    else
      # Replace class names in the following text up to next "<" or "{"
      # but only when we're not inside <a>...</a>
      text = s.scan(/[^{<]+/)
      out += tags.open?("a") ? text : @auto_link.replace(text)
    end
  end

  out
end