class Murdoc::Annotator

Attributes

language[RW]

Source language

metadata[RW]
paragraphs[RW]

Attribute accessor containing the resulting paragraphs

source[RW]

Public Class Methods

from_file(filename, source_type = nil, do_not_count_comment_lines = false) click to toggle source

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

# File lib/murdoc/annotator.rb, line 30
def self.from_file(filename, source_type = nil, do_not_count_comment_lines = false)
  self.new(File.read(filename),
           source_type || Languages.detect(filename),
           do_not_count_comment_lines)
end
new(source, source_type, do_not_count_comment_lines = false) click to toggle source

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

# File lib/murdoc/annotator.rb, line 15
def initialize(source, source_type, do_not_count_comment_lines = false)
  self.source = source
  self.source_type = source_type
  self.language = Languages.get(source_type)
  self.paragraphs = if !language.annotation_only?
    Scanner.new(language).call(self.source, do_not_count_comment_lines)
  else
    [Paragraph.new('', self.source, 0, nil)]
  end
  extract_metadata!
end

Public Instance Methods

extract_metadata!() click to toggle source
# File lib/murdoc/annotator.rb, line 36
def extract_metadata!
  if paragraphs.count > 0 && paragraphs[0].annotation =~ /\A\s*---\n(.*?)\n\s*---\n?(.*)\z/m
    paragraphs[0].annotation = $2
    self.metadata = Murdoc.try_load_yaml($1)
  else
    self.metadata = {}
  end
end
source_type() click to toggle source
# File lib/murdoc/annotator.rb, line 45
def source_type
  @source_type
end
source_type=(source_type) click to toggle source
# File lib/murdoc/annotator.rb, line 49
def source_type=(source_type)
  @source_type = (source_type || :base).to_sym
end