Class: JsDuck::DocFormatter
- Inherits:
-
Object
- Object
- JsDuck::DocFormatter
- Defined in:
- lib/jsduck/doc_formatter.rb
Overview
Formats doc-comments
Instance Attribute Summary collapse
-
#class_context ⇒ Object
Sets up instance to work in context of particular class, so that when #blah is encountered it knows that Context#blah is meant.
-
#doc_context ⇒ Object
Sets up instance to work in context of particular doc object.
-
#link_tpl ⇒ Object
Template HTML that replaces Class#member anchor text.
-
#max_length ⇒ Object
Maximum length for text that doesn’t get shortened, defaults to 120.
-
#relations ⇒ Object
JsDuck::Relations for looking up class names.
Instance Method Summary collapse
-
#create_magic_links(input) ⇒ Object
Looks input text for patterns like:.
- #first_sentence(str) ⇒ Object
-
#format(input) ⇒ Object
Formats doc-comment for placement into HTML.
- #get_matching_member(cls, member, type = nil, static = false) ⇒ Object
- #get_members(cls, member, type = nil, static = false) ⇒ Object
-
#images ⇒ Object
Returns list of all image paths gathered from @img tags.
-
#img_path=(path) ⇒ Object
Sets base path to prefix images from @img tags.
-
#initialize(relations = {}, opts = {}) ⇒ DocFormatter
constructor
A new instance of DocFormatter.
-
#link(cls, member, anchor_text, type = nil, static = false) ⇒ Object
applies the link template.
-
#replace(input) ⇒ Object
Replaces @link and @img tags, auto-generates links for recognized classnames.
- #replace_link_tag(input) ⇒ Object
- #replace_magic_link(cls, member) ⇒ Object
-
#shorten(input) ⇒ Object
Shortens text.
- #split_to_cls_and_member(str) ⇒ Object
-
#too_long?(input) ⇒ Boolean
Returns true when input should get shortened.
- #warn_magic_link(msg) ⇒ Object
Constructor Details
#initialize(relations = {}, opts = {}) ⇒ DocFormatter
Returns a new instance of DocFormatter.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jsduck/doc_formatter.rb', line 45 def initialize(relations={}, opts={}) @class_context = "" @doc_context = {} @max_length = 120 @relations = relations @images = [] @inline_img = InlineImg.new(opts) @inline_video = InlineVideo.new(opts) @link_tpl = opts[:link_tpl] || '<a href="%c%#%m">%a</a>' @link_re = /\{@link\s+(\S*?)(?:\s+(.+?))?\}/m @example_annotation_re = /<pre><code>\s*@example( +[^\n]*)?\s+/m end |
Instance Attribute Details
#class_context ⇒ Object
Sets up instance to work in context of particular class, so that when #blah is encountered it knows that Context#blah is meant.
29 30 31 |
# File 'lib/jsduck/doc_formatter.rb', line 29 def class_context @class_context end |
#doc_context ⇒ Object
Sets up instance to work in context of particular doc object. Used for error reporting.
33 34 35 |
# File 'lib/jsduck/doc_formatter.rb', line 33 def doc_context @doc_context end |
#link_tpl ⇒ Object
Template HTML that replaces Class#member anchor text. Can contain placeholders:
%c - full class name (e.g. “Ext.Panel”) %m - class member name prefixed with member type (e.g. “method-urlEncode”) %# - inserts “#” if member name present %- - inserts “-” if member name present %a - anchor text for link
Default value: ‘<a href=“%c%M”>%a</a>’
24 25 26 |
# File 'lib/jsduck/doc_formatter.rb', line 24 def link_tpl @link_tpl end |
#max_length ⇒ Object
Maximum length for text that doesn’t get shortened, defaults to 120
36 37 38 |
# File 'lib/jsduck/doc_formatter.rb', line 36 def max_length @max_length end |
#relations ⇒ Object
JsDuck::Relations for looking up class names.
When auto-creating class links from CamelCased names found from text, we check the relations object to see if a class with that name actually exists.
43 44 45 |
# File 'lib/jsduck/doc_formatter.rb', line 43 def relations @relations end |
Instance Method Details
#create_magic_links(input) ⇒ Object
Looks input text for patterns like:
My.ClassName
MyClass#method
#someProperty
and converts them to links, as if they were surrounded with @link tag. One notable exception is that Foo is not created to link, even when Foo class exists, but Foo.Bar is. This is to avoid turning normal words into links. For example:
Math involves a lot of numbers. Ext JS is a JavaScript framework.
In these sentences we don’t want to link “Math” and “Ext” to the corresponding JS classes. And that’s why we auto-link only class names containing a dot “.”
207 208 209 210 211 212 213 214 |
# File 'lib/jsduck/doc_formatter.rb', line 207 def create_magic_links(input) cls_re = "([A-Z][A-Za-z0-9.]*[A-Za-z0-9])" member_re = "(?:#([A-Za-z0-9]+))" input.gsub(/\b#{cls_re}#{member_re}?\b|#{member_re}\b/m) do replace_magic_link($1, $2 || $3) end end |
#first_sentence(str) ⇒ Object
340 341 342 |
# File 'lib/jsduck/doc_formatter.rb', line 340 def first_sentence(str) str.sub(/\A(.+?(\.|。))\s.*\Z/mu, "\\1") end |
#format(input) ⇒ Object
Formats doc-comment for placement into HTML. Renders it with Markdown-formatter and replaces @link-s.
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/jsduck/doc_formatter.rb', line 300 def format(input) # In ExtJS source "<pre>" is often at the end of paragraph, not # on its own line. But in that case RDiscount doesn't recognize # it as the beginning of <pre>-block and goes on parsing it as # normal Markdown, which often causes nested <pre>-blocks. # # To prevent this, we always add extra newline before <pre>. input.gsub!(/([^\n])<pre>/, "\\1\n<pre>") # But we remove trailing newline after <pre> to prevent # code-blocks beginning with empty line. input.gsub!(/<pre>(<code>)?\n?/, "<pre>\\1") replace(RDiscount.new(input).to_html) end |
#get_matching_member(cls, member, type = nil, static = false) ⇒ Object
284 285 286 287 288 289 290 291 292 |
# File 'lib/jsduck/doc_formatter.rb', line 284 def get_matching_member(cls, member, type=nil, static=false) ms = get_members(cls, member, type, static).find_all {|m| !m[:private] } if ms.length > 1 instance_ms = ms.find_all {|m| !m[:meta][:static] } instance_ms.length > 0 ? instance_ms[0] : ms.find_all {|m| m[:meta][:static] }[0] else ms[0] end end |
#get_members(cls, member, type = nil, static = false) ⇒ Object
294 295 296 |
# File 'lib/jsduck/doc_formatter.rb', line 294 def get_members(cls, member, type=nil, static=false) @relations[cls] ? @relations[cls].get_members(member, type, static) : [] end |
#images ⇒ Object
Returns list of all image paths gathered from @img tags.
67 68 69 |
# File 'lib/jsduck/doc_formatter.rb', line 67 def images @inline_img.images end |
#img_path=(path) ⇒ Object
Sets base path to prefix images from @img tags.
62 63 64 |
# File 'lib/jsduck/doc_formatter.rb', line 62 def img_path=(path) @inline_img.base_path = path end |
#link(cls, member, anchor_text, type = nil, static = false) ⇒ Object
applies the link template
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/jsduck/doc_formatter.rb', line 260 def link(cls, member, anchor_text, type=nil, static=false) # Use the canonical class name for link (not some alternateClassName) cls = @relations[cls].full_name # prepend type name to member name member = member && get_matching_member(cls, member, type, static) @link_tpl.gsub(/(%[\w#-])/) do case $1 when '%c' cls when '%m' member ? member[:id] : "" when '%#' member ? "#" : "" when '%-' member ? "-" : "" when '%a' HTML.escape(anchor_text||"") else $1 end end end |
#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.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 |
# File 'lib/jsduck/doc_formatter.rb', line 84 def replace(input) s = StringScanner.new(input) out = "" # Keep track of the nesting level of <a> tags. We're not # auto-detecting class names when inside <a>. Normally links # shouldn't be nested, but just to be extra safe. = 0 while !s.eos? do if s.check(@link_re) out += replace_link_tag(s.scan(@link_re)) 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 s.check(@example_annotation_re) # Match possible classnames following @example and add them # as CSS classes inside <pre> element. s.scan(@example_annotation_re) =~ @example_annotation_re css_classes = ($1 || "").strip out += "<pre class='inline-example #{css_classes}'><code>" elsif s.check(/<a\b/) # Increment number of open <a> tags. += 1 out += s.scan_until(/>|\Z/) elsif s.check(/<\/a>/) # <a> closed, auto-detection may continue when no more <a> tags open. -= 1 out += s.scan(/<\/a>/) elsif s.check(/</) # Ignore all other HTML tags out += s.scan_until(/>|\Z/) 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 += > 0 ? text : create_magic_links(text) end end out end |
#replace_link_tag(input) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/jsduck/doc_formatter.rb', line 130 def replace_link_tag(input) input.sub(@link_re) do target = $1 text = $2 if target =~ /^(.*)#(static-)?(?:(cfg|property|method|event|css_var|css_mixin)-)?(.*)$/ cls = $1.empty? ? @class_context : $1 static = !!$2 type = $3 ? $3.intern : nil member = $4 else cls = target static = false type = false member = false end # Construct link text if text text = text elsif member text = (cls == @class_context) ? member : (cls + "." + member) else text = cls end file = @doc_context[:filename] line = @doc_context[:linenr] if !@relations[cls] Logger.instance.warn(:link, "#{input} links to non-existing class", file, line) return text elsif member ms = get_members(cls, member, type, static) if ms.length == 0 Logger.instance.warn(:link, "#{input} links to non-existing member", file, line) return text end if ms.length > 1 # When multiple public members, see if there remains just # one when we ignore the static members. If there's more, # report ambiguity. If there's only static members, also # report ambiguity. instance_ms = ms.find_all {|m| !m[:meta][:static] } if instance_ms.length > 1 alternatives = instance_ms.map {|m| m[:tagname].to_s }.join(", ") Logger.instance.warn(:link_ambiguous, "#{input} is ambiguous: "+alternatives, file, line) elsif instance_ms.length == 0 static_ms = ms.find_all {|m| m[:meta][:static] } alternatives = static_ms.map {|m| "static " + m[:tagname].to_s }.join(", ") Logger.instance.warn(:link_ambiguous, "#{input} is ambiguous: "+alternatives, file, line) end end return link(cls, member, text, type, static) else return link(cls, false, text) end end end |
#replace_magic_link(cls, member) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/jsduck/doc_formatter.rb', line 216 def replace_magic_link(cls, member) if cls && member if @relations[cls] && get_matching_member(cls, member) return link(cls, member, cls+"."+member) else warn_magic_link("#{cls}##{member} links to non-existing " + (@relations[cls] ? "member" : "class")) end elsif cls && cls =~ /\./ if @relations[cls] return link(cls, nil, cls) else cls2, member2 = split_to_cls_and_member(cls) if @relations[cls2] && get_matching_member(cls2, member2) return link(cls2, member2, cls2+"."+member2) elsif cls =~ /\.(js|css|html|php)\Z/ # Ignore common filenames else warn_magic_link("#{cls} links to non-existing class") end end elsif !cls && member if get_matching_member(@class_context, member) return link(@class_context, member, member) elsif member =~ /\A([A-F0-9]{3}|[A-F0-9]{6})\Z/i || member =~ /\A[0-9]/ # Ignore HEX color codes and # member names beginning with number else warn_magic_link("##{member} links to non-existing member") end end return "#{cls}#{member ? '#' : ''}#{member}" end |
#shorten(input) ⇒ Object
Shortens text
116 chars is also where ext-doc makes its cut, but unlike ext-doc we only make the cut when there’s more than 120 chars.
This way we don’t get stupid expansions like:
Blah blah blah some text...
expanding to:
Blah blah blah some text.
329 330 331 332 333 334 335 336 337 338 |
# File 'lib/jsduck/doc_formatter.rb', line 329 def shorten(input) sent = first_sentence(HTML.(input).strip) # Use u-modifier to correctly count multi-byte characters chars = sent.scan(/./mu) if chars.length > @max_length chars[0..(@max_length-4)].join + "..." else sent + " ..." end end |
#split_to_cls_and_member(str) ⇒ Object
250 251 252 253 |
# File 'lib/jsduck/doc_formatter.rb', line 250 def split_to_cls_and_member(str) parts = str.split(/\./) return [parts.slice(0, parts.length-1).join("."), parts.last] end |
#too_long?(input) ⇒ Boolean
Returns true when input should get shortened.
345 346 347 348 349 350 |
# File 'lib/jsduck/doc_formatter.rb', line 345 def too_long?(input) stripped = HTML.(input).strip # for sentence v/s full - compare byte length # for full v/s max - compare char length first_sentence(stripped).length < stripped.length || stripped.scan(/./mu).length > @max_length end |
#warn_magic_link(msg) ⇒ Object
255 256 257 |
# File 'lib/jsduck/doc_formatter.rb', line 255 def warn_magic_link(msg) Logger.instance.warn(:link_auto, msg, @doc_context[:filename], @doc_context[:linenr]) end |