Class: Murdoc::Annotator

Inherits:
Object
  • Object
show all
Includes:
Languages::Javascript::Annotator, Languages::Javascript::CommentSymbols, Languages::Ruby::Annotator, Languages::Ruby::CommentSymbols
Defined in:
lib/murdoc/annotator.rb,
lib/murdoc/languages/ruby.rb,
lib/murdoc/languages/javascript.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Languages::Javascript::Annotator

included

Methods included from Languages::Ruby::Annotator

included

Constructor Details

#initialize(source, source_type, options = {}) ⇒ Annotator

‘source` string contains annotated source code `source_type` is one of supported source types (currently `[:ruby, :javascript]`)



25
26
27
28
29
# File 'lib/murdoc/annotator.rb', line 25

def initialize(source, source_type, options = {})
  self.source_type = source_type
  self.source      = source
  self.options     = self.class.default_options.merge(options)
end

Instance Attribute Details

#optionsObject

Options Available options:

`:highlight_source` -- highlights source syntax using pygments (default: true)


14
15
16
# File 'lib/murdoc/annotator.rb', line 14

def options
  @options
end

#paragraphsObject

Attribute accessor containing the resulting paragraphs



9
10
11
# File 'lib/murdoc/annotator.rb', line 9

def paragraphs
  @paragraphs
end

Class Method Details

.default_optionsObject



16
17
18
19
20
# File 'lib/murdoc/annotator.rb', line 16

def self.default_options
  {
      :highlight_source => true
  }
end

.from_file(filename, source_type = nil, options = {}) ⇒ Object

You may also initialize annotator from file, it will even try to detect the source type from extension.



34
35
36
# File 'lib/murdoc/annotator.rb', line 34

def self.from_file(filename, source_type = nil, options = {})
  self.new(File.read(filename), source_type || detect_source_type_from_filename(filename), options)
end

Instance Method Details

#sourceObject

Rest of the file quite less self-explanatory



116
117
118
# File 'lib/murdoc/annotator.rb', line 116

def source
  @source
end

#source=(src) ⇒ Object

Big and hairy code parser



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
# File 'lib/murdoc/annotator.rb', line 47

def source=(src)
  @source = src
  @paragraphs = []

  # Lambda for checking source for comments. Used for getting consequent non-comments
  # into resulting stream
  is_comment = lambda do |line|
    result = false
    # If source supports single line comments
    if comment_symbols[:single_line]
      result ||= line =~ /^\s*#{Regexp.escape(comment_symbols[:single_line])}/
    end

    # If source supports multi-line comments
    if comment_symbols[:multiline]
      result ||= line =~ /^\s*#{Regexp.escape(comment_symbols[:multiline][:begin])}/
    end
    result
  end

  # splitting stuff into lines and setting cursor into initial position
  lines = src.split("\n")
  i = 0
  while i < lines.size
    comment_lines = []
    # get single line comments (removing optional first space after comment symbol)
    if comment_symbols[:single_line]
      while i < lines.size && lines[i] =~ /^\s*#{Regexp.escape(comment_symbols[:single_line])}\s?(.*)/
        comment_lines << $1
        i += 1
      end
    end

    # getting multiline comments
    if comment_symbols[:multiline]
      begin_symbol = Regexp.escape(comment_symbols[:multiline][:begin])
      end_symbol = Regexp.escape(comment_symbols[:multiline][:end])
      if i < lines.size && lines[i] =~ /\s*#{begin_symbol}/
        begin
          match = lines[i].match /\s*(#{begin_symbol})?\s?(.*?)(#{end_symbol}|$)/
          comment_lines << match[2]
          i += 1
        end while i < lines.size && !(lines[i-1] =~ /\s*#{end_symbol}/)
      end
    end

    # getting source lines
    starting_line = i
    source_lines = []
    while i < lines.size && !is_comment.call(lines[i])
      source_lines << lines[i]
      i += 1
    end
    # post-processing: stripping comments and removing empty strings from beginnings and ends
    while source_lines.size > 0 && source_lines[0] =~ /^\s*$/
      starting_line += 1
      source_lines.delete_at(0)
    end
    source_lines.delete_at(-1) while source_lines.size > 0 && source_lines[-1] =~ /^\s*$/
    comment_lines.map! {|l| l.strip }
    comment_lines.delete_at(0) while comment_lines.size > 0 && comment_lines[0].empty?
    comment_lines.delete_at(-1) while comment_lines.size > 0 && comment_lines[-1].empty?

    # writing a new paragraph
    @paragraphs << Paragraph.new(source_lines.join("\n"), comment_lines.join("\n"), starting_line, source_type)
  end
end

#source_typeObject



38
39
40
# File 'lib/murdoc/annotator.rb', line 38

def source_type
  @source_type
end

#source_type=(source_type) ⇒ Object



42
43
44
# File 'lib/murdoc/annotator.rb', line 42

def source_type=(source_type)
  @source_type = source_type.to_s
end