Class: JsDuck::Source::File

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

Overview

Represents one JavaScript or CSS source file.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents, docs, filename = "") ⇒ File

Returns a new instance of File.



16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



12
13
14
# File 'lib/jsduck/source/file.rb', line 12

def contents
  @contents
end

#docsObject (readonly)

Returns the value of attribute docs.



13
14
15
# File 'lib/jsduck/source/file.rb', line 13

def docs
  @docs
end

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#html_filenameObject

Returns the value of attribute html_filename.



14
15
16
# File 'lib/jsduck/source/file.rb', line 14

def html_filename
  @html_filename
end

Instance Method Details

#each(&block) ⇒ Object

loops through each doc-object in file



29
30
31
# File 'lib/jsduck/source/file.rb', line 29

def each(&block)
  @docs.each(&block)
end

#id(doc) ⇒ Object



64
65
66
67
68
69
70
71
72
# 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_htmlObject

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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