<commit_before># -*- coding: utf-8 -*-
module Kramdown
  module Converter
    class Idml < Base

      # Create an IDML converter with the given options.
      # @param[Kramdown::Element] root
      # @param[Hash, optional] options
      def initialize(root, options = {})
        super
        @options = {
          :idml_output_file_name => "idml_output.idml"
        }.merge(options)
        configure_rubyzip
      end

      # Writes an IDML file to disk (using @options[:idml_output_file_name]).
      # Contains a single story that is based on root.
      # @param[Kramdown::Element] root the kramdown root element
      # @return ???
      def convert(root)
        story_xml = compute_story_xml(root)
        files = collect_files_for_archive(story_xml)
        create_zip_archive(options[:idml_output_file_name], files)
      end

    protected

      def configure_rubyzip
        Zip.setup do |c|
          c.on_exists_proc = true
          c.continue_on_exists_proc = true
          c.unicode_names = true
        end
      end

      # Returns IDMLStory XML for root
      # @param[Kramdown::Element] root the root element
      # @return[String] the story XML as string
      def compute_story_xml(root)
        xml_string = Kramdown::Converter::IdmlStory.convert(root, @options)
      end

      # Builds array of all files to go into ZIP archive
      # @param[String] story_xml
      # @return[Array<Array>] Array of tuples (file_name and file_contents)
      def collect_files_for_archive(story_xml)
        [
          ['theStory', story_xml]
        ]
      end

      # Writes a ZIP archive to disk
      # @param[String] zip_archive_file_name a complete path to the output file location and name
      # @param[Array<Array>] files the files to include in archive. Tuple of name and contents.
      def create_zip_archive(zip_archive_file_name, files)
        Zip::OutputStream.open(zip_archive_file_name) { |zos|
          files.each do |(file_name, contents)|
            zos.put_next_entry(file_name)
            zos.puts contents
          end
        }
      end

    end
  end
end<commit_msg>Some tweaks to IDML converter (WIP)<commit_after># -*- coding: utf-8 -*-
module Kramdown
  module Converter
    class Idml < Base

      # Create an IDML converter with the given options.
      # @param[Kramdown::Element] root
      # @param[Hash, optional] options
      def initialize(root, options = {})
        super
        @options = {
          :idml_output_file_name => "idml_output.idml"
        }.merge(options)
        configure_rubyzip
      end

      # Writes an IDML file to disk (using @options[:idml_output_file_name]).
      # Contains a single story that is based on root.
      # @param[Kramdown::Element] root the kramdown root element
      # @return ???
      def convert(root)
        story_xml = compute_story_xml(root)
        files = collect_files_for_archive(story_xml)
        create_zip_archive(options[:idml_output_file_name], files)
      end

    protected

      def configure_rubyzip
        Zip.setup do |c|
          c.continue_on_exists_proc = true # overwrite existing ZIP archives when compressing
          c.unicode_names = true
        end
      end

      # Returns IDMLStory XML for root
      # @param[Kramdown::Element] root the root element
      # @return[String] the story XML as string
      def compute_story_xml(root)
        xml_string, _warnings = ::Kramdown::Converter::IdmlStory.convert(root, @options)
        xml_string
      end

      # Builds array of all files to go into ZIP archive
      # @param[String] story_xml
      # @return[Array<Array>] Array of tuples (file_name and file_contents)
      def collect_files_for_archive(story_xml)
        [
          ['mimetype', 'application/vnd.adobe.indesign-idml-package'],
          # META-INF is optional
          # http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/indesign/sdk/cs6/idml/idml-specification.pdf
          # Appendix A, 1.4.6 (p. 401)
          ['META-INF/container.xml', ""],
          ['META-INF/metadata.xml', ""],
          ['designmap.xml', ""],
          ['MasterSpreads/MasterSpread_A.xml', ""],
          ['Resources/Fonts.xml', ""],
          ['Resources/Graphic.xml', ""],
          ['Resources/Preferences.xml', ""],
          ['Resources/Styles.xml', ""],
          ['Spreads/Spread_spread1.xml', ""],
          ['Spreads/Spread_spread2.xml', ""],
          ['Stories/Story_story0.xml', story_xml],
          ['XML/BackingStory.xml', ""],
          ['XML/Mapping.xml', ""],
          ['XML/Tags.xml', ""]
        ]
      end

      # Writes a ZIP archive to disk
      # @param[String] zip_archive_file_name a complete path to the output file location and name
      # @param[Array<Array>] files the files to include in archive. Tuple of name and contents.
      def create_zip_archive(zip_archive_file_name, files)
        Zip::OutputStream.open(zip_archive_file_name) { |io|
          files.each do |(file_name, contents)|
            io.put_next_entry(file_name)
            io.write(contents)
          end
        }
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.10138428543575746, 'fraction_numerical': 0.0023396373562097877, 'mean_word_length': 2.355134074558535, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 31}, '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': '2621639', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1538, 'n_words': 381}