Class: JsDuck::SourceFile

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, filename = "", options = {}) ⇒ SourceFile

Returns a new instance of SourceFile.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jsduck/source_file.rb', line 18

def initialize(contents, filename="", options={})
  @contents = contents
  @filename = filename
  @options = options
  @html_filename = ""
  @links = {}

  merger = Merger.new
  merger.filename = @filename
  @docs = parse.map do |docset|
    merger.linenr = docset[:linenr]
    link(docset[:linenr], merger.merge(docset[:comment], docset[:code]))
  end
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



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

def contents
  @contents
end

#docsObject (readonly)

Returns the value of attribute docs.



15
16
17
# File 'lib/jsduck/source_file.rb', line 15

def docs
  @docs
end

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#html_filenameObject

Returns the value of attribute html_filename.



16
17
18
# File 'lib/jsduck/source_file.rb', line 16

def html_filename
  @html_filename
end

Instance Method Details

#each(&block) ⇒ Object

loops through each doc-object in file



34
35
36
# File 'lib/jsduck/source_file.rb', line 34

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

#id(doc) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/jsduck/source_file.rb', line 69

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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jsduck/source_file.rb', line 51

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 = CGI.escapeHTML(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