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

Returns a new instance of File.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jsduck/source/file.rb', line 17

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

  @docs = Source::FileParser.new.parse(@contents, @filename, options)

  @docs.map do |docset|
    link(docset[:linenr], docset)
  end
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



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

def contents
  @contents
end

#docsObject (readonly)

Returns the value of attribute docs.



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

def docs
  @docs
end

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#html_filenameObject

Returns the value of attribute html_filename.



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

def html_filename
  @html_filename
end

Instance Method Details

#each(&block) ⇒ Object

loops through each doc-object in file



31
32
33
# File 'lib/jsduck/source/file.rb', line 31

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

#id(doc) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/jsduck/source/file.rb', line 66

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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jsduck/source/file.rb', line 48

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