class Fyodor::EntryParser

Public Class Methods

new(entry_lines, parser_config) click to toggle source
# File lib/fyodor/entry_parser.rb, line 5
def initialize(entry_lines, parser_config)
  @lines = entry_lines
  @config = parser_config
  format_check
end

Public Instance Methods

entry() click to toggle source
# File lib/fyodor/entry_parser.rb, line 11
def entry
  Entry.new({
    book_title: book[:title],
    book_author: book[:author],
    text: text,
    desc: desc,
    type: type,
    loc: loc,
    loc_start: loc_start,
    page: page,
    page_start: page_start,
    time: time
  })
end

Private Instance Methods

book() click to toggle source
# File lib/fyodor/entry_parser.rb, line 29
def book
  regex = /^(.*) \((.*)\)\r?\n$/

  title, author = @lines[0].scan(regex).first
  # If book has no author, regex fails.
  if title.nil?
    title = @lines[0]
    author = ""
  end

  title = rm_zero_width_chars(title).strip
  author = rm_zero_width_chars(author).strip

  {title: title, author: author}
end
desc() click to toggle source
# File lib/fyodor/entry_parser.rb, line 45
def desc
  @lines[1].delete_prefix("- ").strip
end
format_check() click to toggle source
# File lib/fyodor/entry_parser.rb, line 97
def format_check
  raise "Entry is badly formatted" if @lines[0].strip.empty?
  raise "Entry is badly formatted" if @lines[1].strip.empty?
  raise "Entry is badly formatted" unless @lines[2].strip.empty?
end
loc() click to toggle source
# File lib/fyodor/entry_parser.rb, line 58
def loc
  keyword = Regexp.quote(@config["loc"])
  regex = /#{keyword} (\S+)/i

  @lines[1][regex, 1]
end
loc_start() click to toggle source
# File lib/fyodor/entry_parser.rb, line 65
def loc_start
  keyword = Regexp.quote(@config["loc"])
  regex = /#{keyword} (\d+)(-\d+)?/i

  @lines[1][regex, 1].to_i
end
page() click to toggle source
# File lib/fyodor/entry_parser.rb, line 72
def page
  keyword = Regexp.quote(@config["page"])
  regex = /#{keyword} (\S+)/i

  @lines[1][regex, 1]
end
page_start() click to toggle source
# File lib/fyodor/entry_parser.rb, line 79
def page_start
  keyword = Regexp.quote(@config["page"])
  regex = /#{keyword} (\d+)(-\d+)?/i

  @lines[1][regex, 1].to_i
end
rm_zero_width_chars(str) click to toggle source
# File lib/fyodor/entry_parser.rb, line 103
def rm_zero_width_chars(str)
  str.gsub(/[\u200B-\u200D\uFEFF]/, '')
end
text() click to toggle source
# File lib/fyodor/entry_parser.rb, line 93
def text
  @lines[3..-2].join.strip
end
time() click to toggle source
# File lib/fyodor/entry_parser.rb, line 86
def time
  keyword = Regexp.quote(@config["time"])
  regex = /#{keyword} (.*)\r?\n$/i

  @lines[1][regex, 1]
end
type() click to toggle source
# File lib/fyodor/entry_parser.rb, line 49
def type
  Entry::TYPE.values.find do |type|
    keyword = Regexp.quote(@config[type])
    regex = /^- #{keyword}/i

    @lines[1] =~ regex
  end
end