Sha256: 64c07eb28afd08e7a44b685fa779abfe993b1fb59e66105a521ac7919c84b7a0

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

module Klipbook
  class ClippingsParser
    def extract_clippings_from(file_text)
      clippings_text_from(file_text).map { |clipping_text| build_clipping_from(clipping_text) }.compact
    end

    def build_clipping_from(clipping_text)
      return nil if clipping_text.blank?

      attributes = extract_attributes(clipping_text)

      Clipping.new(attributes)
    end

    def extract_attributes(clipping_text)

      lines = clipping_text.lstrip.lines.to_a

      return nil if lines.length < 2

      title_line = lines[0].strip
      metadata = lines[1].strip
      text_lines = lines[3..-1]

      return nil unless valid_metadata?(metadata)

      {
        title:    extract_title(title_line),
        author:   extract_author(title_line),
        type:     extract_type(metadata),
        location: extract_location(metadata),
        added_on: extract_added_date(metadata),
        text:     extract_text(text_lines)
      }
    end

  private

    def clippings_text_from(file_text)
      file_text.gsub("\r", '').split('==========')
    end

    def valid_metadata?(metadata)
      metadata.match(/^-.*Added on/)
    end

    def extract_title(title_line)
      if title_line =~ /\(.+\)\Z/
        title_line.scan(/(.*)\s+\(.+\)\Z/).first.first
      else
        title_line
      end
    end

    def extract_author(title_line)
      match = title_line.scan /\(([^\(]+)\)\Z/
      match.empty? ? nil : match.first.first
    end

    def extract_type(metadata)
      type = metadata.scan(/^-( Your)? (\w+)/).first[1]
      type.downcase.to_sym
    end

    def extract_added_date(metadata)
      metadata.scan(/Added on (.+)$/i).first.first
    end

    def extract_location(metadata)
      match = metadata.scan(/Loc(ation|\.) ([0-9]+-?)/)

      return nil if match.empty?

      location = match.first[1]
      location.to_i
    end

    def extract_text(text_lines)
      text_lines.join('').rstrip
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
klipbook-0.2.1 lib/klipbook/clippings_parser.rb