Class: Murdoc::Annotator

Inherits:
Object
  • Object
show all
Includes:
Languages::Html::Annotator, Languages::Html::CommentSymbols, Languages::Javascript::Annotator, Languages::Javascript::CommentSymbols, Languages::Ruby::Annotator, Languages::Ruby::CommentSymbols
Defined in:
lib/murdoc/annotator.rb,
lib/murdoc/languages/html.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

Methods included from Languages::Html::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.options     = self.class.default_options.merge(options)
  self.source      = source
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
37
38
# 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



112
113
114
# File 'lib/murdoc/annotator.rb', line 112

def source
  @source
end

#source=(src) ⇒ Object

Big and hairy code parser



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
# File 'lib/murdoc/annotator.rb', line 49

def source=(src)
  @source = src
  @paragraphs = []
  lines = src.split("\n")

  # splitting stuff into lines and setting cursor into initial position
  i = 0
  source_lines_counter = 0
  while i < lines.size
    comment_lines = []
    # get single line comments
    if comment_symbols[:single_line]
      while i < lines.size && lines[i] =~ /^\s*#{Regexp.escape(comment_symbols[:single_line])}(.*)/
        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
    source_lines = []
    starting_line = if options[:do_not_count_comment_lines]
      source_lines_counter
    else
      i
    end

    while i < lines.size && !is_comment(lines[i])
      source_lines << lines[i]
      source_lines_counter += 1
      i += 1
    end

    # post-processing: stripping comments and removing empty strings from beginnings and ends
    starting_line += postprocess_source(source_lines)
    postprocess_comments(comment_lines)
    # clear comment lines if that's commented out code
    comment_lines.clear if comment_lines[0] =~ /^:code:$/

    # if we have comments or source
    if comment_lines.size > 0 || source_lines.size > 0
      @paragraphs << Paragraph.new(source_lines.join("\n"),
                                   comment_lines.join("\n"),
                                   starting_line,
                                   source_type,
                                   options)
    end
  end
end

#source_typeObject



40
41
42
# File 'lib/murdoc/annotator.rb', line 40

def source_type
  @source_type
end

#source_type=(source_type) ⇒ Object



44
45
46
# File 'lib/murdoc/annotator.rb', line 44

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