module Reading

Constants

Colors

Public Class Methods

add_regex_config(custom_config) click to toggle source
# File lib/reading/csv/config.rb, line 93
def self.add_regex_config(custom_config)
  return custom_config[:csv][:regex] unless custom_config[:csv][:regex].nil?
  comment_character = Regexp.escape(custom_config.fetch(:csv).fetch(:comment_character))
  formats = /#{custom_config.fetch(:item).fetch(:formats).values.join("|")}/
  dnf_string = Regexp.escape(custom_config.fetch(:csv).fetch(:dnf_string))
  date_sep = Regexp.escape(custom_config.fetch(:csv).fetch(:date_separator))
  date_regex = /(\d{4}#{date_sep}\d?\d#{date_sep}\d?\d)/ # TODO hardcode the date separator?
  time_length = /(\d+:\d\d)/
  pages_length = /p?(\d+)p?/
  custom_config[:csv][:regex] =
    {
      comment_escaped: comment_character,
      compact_planned_line_start: /\A\s*#{comment_character}(?<genre>[^a-z:,\|]+):\s*(?=#{formats})/,
      compact_planned_item: /\A(?<format_emojis>(?:#{formats})+)(?<author_title>[^@]+)(?<sources>@.+)?\z/,
      formats: formats,
      formats_split: /\s*(?=#{formats})/,
      series_volume: /,\s*#(\d+)\z/,
      isbn: isbn_regex,
      sources: sources_regex,
      date_added: /#{date_regex}.*>/,
      date_started: /#{date_regex}[^>]*\z/,
      dnf: /(?<=>|\A)\s*(#{dnf_string})/,
      progress: /(?<=#{dnf_string}|>|\A)\s*((\d?\d)%|#{time_length}|#{pages_length})\s+/,
      group_experience: /#{config.fetch(:csv).fetch(:group_emoji)}\s*(.*)\s*\z/,
      variant_index: /\s+v(\d+)/,
      date_finished: date_regex,
      time_length: time_length,
      pages_length: pages_length,
      pages_length_in_variant: /(?:\A|\s+|p)(\d{1,9})(?:p|\s+|\z)/ # to exclude ISBN-10 and ISBN-13
    }
end
config() click to toggle source
# File lib/reading/csv/config.rb, line 4
def self.config
  @config
end

Private Class Methods

isbn_regex() click to toggle source
# File lib/reading/csv/config.rb, line 128
def isbn_regex
  return @isbn_regex unless @isbn_regex.nil?
  isbn_lookbehind = "(?<=\\A|\\s|#{config.fetch(:csv).fetch(:separator)})"
  isbn_lookahead = "(?=\\z|\\s|#{config.fetch(:csv).fetch(:separator)})"
  isbn_bare_regex = /(?:\d{3}[-\s]?)?[A-Z\d]{10}/ # also includes ASIN
  @isbn_regex = /#{isbn_lookbehind}#{isbn_bare_regex.source}#{isbn_lookahead}/
end
sources_regex() click to toggle source
# File lib/reading/csv/config.rb, line 136
def sources_regex
  return @sources_regex unless @sources_regex.nil?
  isbn = "(#{isbn_regex.source})"
  url_name = "([^#{config.fetch(:csv).fetch(:separator)}]+)"
  url = "(https?://[^\\s#{config.fetch(:csv).fetch(:separator)}]+)"
  url_prename = "#{url_name}#{config.fetch(:csv).fetch(:short_separator)}#{url}"
  url_postname = "#{url}#{config.fetch(:csv).fetch(:short_separator)}#{url_name}"
  @sources_regex = /#{isbn}|#{url_prename}|#{url_postname}|#{url}/
end