class JsDuck::Source::File

Represents one JavaScript or SCSS source file.

The filename parameter determines whether it's parsed as JavaScript (the default) or SCSS.

Attributes

contents[R]
docs[R]
filename[R]
html_filename[R]

Public Class Methods

new(contents, docs, filename="") click to toggle source
# File lib/jsduck/source/file.rb, line 16
def initialize(contents, docs, filename="")
  @contents = contents
  @docs = docs
  @filename = filename
  @html_filename = ""
  @links = {}

  @docs.map do |docset|
    link(docset)
  end
end

Public Instance Methods

each(&block) click to toggle source

loops through each doc-object in file

# File lib/jsduck/source/file.rb, line 29
def each(&block)
  @docs.each(&block)
end
html_filename=(html_filename) click to toggle source

Sets the html filename of this file, updating also all doc-objects linking this file

# File lib/jsduck/source/file.rb, line 35
def html_filename=(html_filename)
  @html_filename = html_filename
  @links.each_value do |line|
    line.each do |link|
      link[:file][:html_filename] = @html_filename
      link[:file][:href] = @html_filename + "#" + id(link[:doc])
    end
  end
end
id(doc) click to toggle source
# File lib/jsduck/source/file.rb, line 64
def id(doc)
  if doc[:tagname] == :class
    doc[:name].gsub(/\./, '-')
  else
    # when creation of global class is skipped,
    # this owner property can be nil.
    (doc[:owner] || "global").gsub(/\./, '-') + "-" + doc[:id]
  end
end
to_html() click to toggle source

Returns source code as HTML with lines starting doc-comments specially marked.

# File lib/jsduck/source/file.rb, line 46
def to_html
  linenr = 0
  lines = []
  # Use #each_line instead of #lines to support Ruby 1.6
  @contents.each_line do |line|
    linenr += 1;
    line = Util::HTML.escape(line)
    # wrap the line in as many spans as there are links to this line number.
    if @links[linenr]
      @links[linenr].each do |link|
        line = "<span id='#{id(link[:doc])}'>#{line}</span>"
      end
    end
    lines << line
  end
  lines.join()
end

Private Instance Methods