class DocDigger::FileParser

Constants

LANGUAGES
MARKDOWNS

Attributes

document_name[R]

Public Class Methods

new(filename) click to toggle source
# File lib/doc-digger/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

Public Instance Methods

lang() click to toggle source
# File lib/doc-digger/file_parser.rb, line 25
def lang
  @flang ||= check_lang
end
parse() click to toggle source
# File lib/doc-digger/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
results() click to toggle source
# File lib/doc-digger/file_parser.rb, line 29
def results
  @results ||= parse
end

Protected Instance Methods

branch(command, initial=false) click to toggle source
# File lib/doc-digger/file_parser.rb, line 67
def branch(command, initial=false)
  @results << {} if initial && (@results.length == 0 || command[:type] == :doc && command[:part] == :main)
  item = @results.last

  if command[:type] == :res
    if initial
      item[:resources] = [] unless item.has_key?(:resources)
      item[:resources] << {} if command[:part] == :main
    end
    item = item[:resources].last
  end

  case command[:part]
  when :state
    item[:state] = {} if initial && !item.has_key?(:state)
    item = item[:state]
  when /\A(param|header|response|bind)\Z/
    part = "#{command[:part]}s".to_sym
    if initial
      item[part] = [] unless item.has_key?(part)
      item[part] << {}
    end
    item = item[part].last
  end
  item
end
check_lang() click to toggle source
# File lib/doc-digger/file_parser.rb, line 109
def check_lang
  LANGUAGES.each { |l, options| return l if options[1] === @ext }
  nil
end
process_descriptions(item, hash) click to toggle source
# File lib/doc-digger/file_parser.rb, line 94
def process_descriptions(item, hash)
  text = hash[:text].to_s.gsub(/\A\s{,#{@recent_command[:space] + 2}}/, '').rstrip
  if text.length == 0
    @linebreak = true
    return
  end
  markdown = MARKDOWNS === text || @source
  @source ^= /\A\s*`{3}/ === text if markdown

  item[:descriptions] = [[]] unless item.has_key?(:descriptions)
  item[:descriptions] << [] if @linebreak || markdown
  item[:descriptions].last << text
  @linebreak = markdown
end
process_line(str) click to toggle source
# File lib/doc-digger/file_parser.rb, line 53
def process_line(str)
  line_parser = LineParser.new(str, lang)
  hash = line_parser.parse
  if hash[:type] == :joint
    return if @recent_command.nil?
    process_descriptions(branch(@recent_command), hash)
  else
    branch(hash, true).merge!(hash[:data])
    @recent_command = hash.select { |k| k != :data }
    @recent_command[:space] = line_parser.indentation
    @linebreak = false
  end
end