<commit_before>require 'fileutils'
require 'rexml/parsers/pullparser'

module DocbookXslWrapper

  class Epub
    attr_reader :options, :xml

    def initialize(options)
      @options = options
      @xml     = Nokogiri::XML(File.open(@options.docbook, 'rb'))
    end

    def create
      # Nokogiri doesn't create directories, so we do it manually
      Dir.mkdir(File.join(options.destination, meta_inf_directory))
      Dir.mkdir(File.join(options.destination, oebps_directory))

      render_to_epub
      bundle_epub
    end

  private

    def render_to_epub
      errors = xslt_transform_and_rescue_because_it_currently_throws_unknown_runtime_error
      raise "Could not render as .epub to #{options.output}\n\n(#{errors})" unless errors.empty?
    end

    def xslt_transform_and_rescue_because_it_currently_throws_unknown_runtime_error
      begin
        errors = stylesheet.transform(xml, params)
      rescue
        errors = ''
      end
      errors
    end

    def stylesheet
      if options.stylsheet
        xsl = options.stylsheet
      else
        xsl = docbook_xsl_path
      end
      Nokogiri::XSLT(File.open(xsl, 'rb'))
    end

    def docbook_xsl_path
      case options.format
      when 'epub3'
        File.join(GEM_PATH, 'xsl', 'epub3', 'chunk.xsl')
      else
        File.join(GEM_PATH, 'xsl', 'epub', 'docbook.xsl')
      end
    end

    def params
      params_list = [
        'chunk.quietly', "#{verbosity}",
        'base.dir', File.join(options.destination, '/'),
      ]
      params_list.concat(css) if options.css
      params_list.concat(fonts) unless options.fonts.empty?

      Nokogiri::XSLT.quote_params(params_list)
    end

    def fonts
      ['epub.embedded.fonts', options.fonts.map {|f| File.basename(f)}.join(',')]
    end

    def css
      ['html.stylesheet', File.join(File.basename(options.css), '/')]
    end

    def bundle_epub
      copy_media_files_to_epub_dir
      create_mimetype_file if options.format == 'epub' # EPUB3 stylesheet creates this automatically

      quiet = options.verbose ? '' : '-q'
      # Double-quote stylesheet & file to help Windows cmd.exe
      zip_cmd = %Q(cd "#{options.destination}" && zip #{quiet} -X -r  "#{File.expand_path(options.output)}" "mimetype" "#{meta_inf_directory}" "#{oebps_directory}")
      puts zip_cmd if options.debug
      success = system(zip_cmd)
      raise "Could not bundle into .epub file to #{options.output}" unless success<commit_msg>Set some params for better epub output<commit_after>require 'fileutils'
require 'rexml/parsers/pullparser'

module DocbookXslWrapper

  class Epub
    attr_reader :options, :xml

    def initialize(options)
      @options = options
      @xml     = Nokogiri::XML(File.open(@options.docbook, 'rb'))
    end

    def create
      # Nokogiri doesn't create directories, so we do it manually
      Dir.mkdir(File.join(options.destination, meta_inf_directory))
      Dir.mkdir(File.join(options.destination, oebps_directory))

      render_to_epub
      bundle_epub
    end

  private

    def render_to_epub
      errors = xslt_transform_and_rescue_because_it_currently_throws_unknown_runtime_error
      raise "Could not render as .epub to #{options.output}\n\n(#{errors})" unless errors.empty?
    end

    def xslt_transform_and_rescue_because_it_currently_throws_unknown_runtime_error
      begin
        errors = stylesheet.transform(xml, params)
      rescue
        errors = ''
      end
      errors
    end

    def stylesheet
      xsl = docbook_xsl_path
      xsl = options.stylesheet unless options.stylesheet.empty?

      Nokogiri::XSLT(File.open(xsl, 'rb'))
    end

    def docbook_xsl_path
      case options.format
      when 'epub3'
        File.join(GEM_PATH, 'xsl', 'epub3', 'chunk.xsl')
      else
        File.join(GEM_PATH, 'xsl', 'epub', 'docbook.xsl')
      end
    end

    def params
      params_list = [
        'chunk.quietly', "#{verbosity}",
        'chunk.first.sections', 1,
        'othercredit.like.author.enabled', 1,
        'chapter.autolabel', 0,
        'section.autolabel', 0,
        'part.autolabel', 0,
        'base.dir', File.join(options.destination, '/'),
      ]
      params_list.concat(css) if options.css
      params_list.concat(fonts) unless options.fonts.empty?

      Nokogiri::XSLT.quote_params(params_list)
    end

    def fonts
      ['epub.embedded.fonts', options.fonts.map {|f| File.basename(f)}.join(',')]
    end

    def css
      ['html.stylesheet', File.join(File.basename(options.css), '/')]
    end

    def bundle_epub
      copy_media_files_to_epub_dir
      create_mimetype_file if options.format == 'epub' # EPUB3 stylesheet creates this automatically

      quiet = options.verbose ? '' : '-q'
      # Double-quote stylesheet & file to help Windows cmd.exe
      zip_cmd = %Q(cd "#{options.destination}" && zip #{quiet} -X -r  "#{File.expand_path(options.output)}" "mimetype" "#{meta_inf_directory}" "#{oebps_directory}")

      puts zip_cmd if options.debug
      success = system(zip_cmd)
      raise "Could not bundle into .epub file to #{options.output}" unless success
{'fraction_non_alphanumeric': 0.1096104912898806, 'fraction_numerical': 0.002153063221765512, 'mean_word_length': 2.86535552193646, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '1760890', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1634, 'n_words': 355}