Class: Restwoods::FileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/restwoods/file_parser.rb

Constant Summary collapse

LANGUAGES =
{
  java:   [['/**', '*/'], /\A\.(js|jsx|c|cs|java|php?|ts|cpp|go|scala|dart)\Z/],
  ruby:   [['=begin', '=end'], /\A\.(rb)\Z/],
  perl:   [['#**', '#*'], /\A\.(perl|pl|pm)\Z/],
  python: [['"""', '"""'], /\A\.(py)\Z/],
  elixir: [['@restwoods """', '"""'], /\A\.(ex|exs?)\Z/],
  erlang: [['%{', '%}'], /\A\.(erl)\Z/],
  coffee: [['###', '###'], /\A\.(coffee)\Z/],
  lua:    [['--[[', ']]'], /\A\.(lua)\Z/]
}
MARKDOWNS =
/\A\s*(#+|>|\-\s*|\d+\.|`{3})/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ FileParser

Returns a new instance of FileParser.



18
19
20
21
22
23
# File 'lib/restwoods/file_parser.rb', line 18

def initialize(filename)
  @name = filename
  @type = File.ftype(filename)
  @ext = File.extname(@name)
  @document_name = File.basename(filename, @ext)
end

Instance Attribute Details

#document_nameObject (readonly)

Returns the value of attribute document_name.



16
17
18
# File 'lib/restwoods/file_parser.rb', line 16

def document_name
  @document_name
end

Instance Method Details

#langObject



25
26
27
# File 'lib/restwoods/file_parser.rb', line 25

def lang
  @flang ||= check_lang
end

#parseObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/restwoods/file_parser.rb', line 33

def parse
  @results = []
  File.open(@name, "r") do |f|
    started = false
    f.each_line do |s|
      if s.strip == LANGUAGES[lang][0][0]
        started = true
        @source = false
        next
      end

      started = false if started && s.strip == LANGUAGES[lang][0][1]
      process_line(s) if started
    end
  end if @type == "file" && !lang.nil?
  @results
end

#resultsObject



29
30
31
# File 'lib/restwoods/file_parser.rb', line 29

def results
  @results ||= parse
end