<commit_before># frozen_string_literal: true

module EPub
  class Publication
    private_class_method :new
    attr_reader :id, :content_file, :content, :toc

    # Class Methods

    def self.clear_cache
      Cache.clear
    end

    def self.from(epub, options = {}) # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
      return null_object if epub.blank?
      noid = epub.is_a?(Hash) ? epub[:id] : epub
      return null_object unless Valid.noid?(noid)
      file = epub[:file] if epub.is_a?(Hash)
      Cache.cache(noid, file) if file.present?
      return null_object unless Cache.cached?(noid)
      valid_epub = Validator.from(noid)
      return null_object if valid_epub.is_a?(ValidatorNullObject)
      publication = new(valid_epub)
      if file.present?
        sql_lite = EPub::SqlLite.from(publication)
        sql_lite.create_table
        sql_lite.load_chapters
      end
      publication
    rescue StandardError => e
      ::EPub.logger.info("Publication.from(#{epub}, #{options}) raised #{e}")
      null_object
    end

    def self.null_object
      PublicationNullObject.send(:new)
    end

    # Instance Methods

    def chapter_title_from_toc(chapter_href)
      # Navigation can be way more complicated than this, so this is a WIP
      toc.xpath("//nav[@type='toc']/ol/li/a[@href='#{chapter_href}']").text || ""
    end

    def chapters
      chapters = []
      i = 0
      content.xpath("//spine/itemref/@idref").each do |idref|
        i += 1<commit_msg>Added another way to get chapter titles from the epub toc<commit_after># frozen_string_literal: true

module EPub
  class Publication
    private_class_method :new
    attr_reader :id, :content_file, :content, :toc

    # Class Methods

    def self.clear_cache
      Cache.clear
    end

    def self.from(epub, options = {}) # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
      return null_object if epub.blank?
      noid = epub.is_a?(Hash) ? epub[:id] : epub
      return null_object unless Valid.noid?(noid)
      file = epub[:file] if epub.is_a?(Hash)
      Cache.cache(noid, file) if file.present?
      return null_object unless Cache.cached?(noid)
      valid_epub = Validator.from(noid)
      return null_object if valid_epub.is_a?(ValidatorNullObject)
      publication = new(valid_epub)
      if file.present?
        sql_lite = EPub::SqlLite.from(publication)
        sql_lite.create_table
        sql_lite.load_chapters
      end
      publication
    rescue StandardError => e
      ::EPub.logger.info("Publication.from(#{epub}, #{options}) raised #{e}")
      null_object
    end

    def self.null_object
      PublicationNullObject.send(:new)
    end

    # Instance Methods

    def chapter_title_from_toc(chapter_href)
      # Navigation can be way more complicated than this, so this is a WIP
      title = toc.xpath("//nav[@type='toc']/ol/li/a[@href='#{chapter_href}']").text || ""
      # Many more ifs will come to infest this space...
      if title.blank?
        chapter_href = File.basename(chapter_href)
        title = toc.xpath("//nav[@type='toc']/ol/li/a[@href='#{chapter_href}']").text || ""
      end

      ::EPub.logger.error("Can't find chapter title for #{chapter_href}") if title.blank?
      title
    end

    def chapters
      chapters = []
      i = 0
      content.xpath("//spine/itemref/@idref").each do |idref|
        i += 1
{'fraction_non_alphanumeric': 0.1129976580796253, 'fraction_numerical': 0.00117096018735363, 'mean_word_length': 2.887372013651877, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5866755', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1110, 'n_words': 251}