Sha256: cb96d94b9c0f47010679a48c0622251811364ae202fc6780ea617e59a0cff044

Contents?: true

Size: 1.05 KB

Versions: 7

Compression:

Stored size: 1.05 KB

Contents

module Kitabu
  module Parser
    class PDF < Base
      def parse
        apply_footnotes!
        spawn_command ["prince", with_footnotes_file.to_s, "-o", pdf_file.to_s]
      end

      def apply_footnotes!
        html = Nokogiri::HTML(html_file.read)

        # https://github.com/sparklemotion/nokogiri/issues/339
        html.css("html").first.tap do |element|
          next unless element
          element.delete("xmlns")
          element.delete("xml:lang")
        end

        html.css("p.footnote[id^='_fn']").each do |fn|
          fn.node_name = "span"
          fn.set_attribute("class", "fn")

          html.css("[href='##{fn["id"]}']").each do |link|
            link.add_next_sibling(fn)
          end
        end

        File.open(with_footnotes_file, "w") {|f| f << html.to_xhtml}
      end

      def with_footnotes_file
        root_dir.join("output/#{name}.pdf.html")
      end

      def html_file
        root_dir.join("output/#{name}.html")
      end

      def pdf_file
        root_dir.join("output/#{name}.pdf")
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
kitabu-1.0.6 lib/kitabu/parser/pdf.rb
kitabu-1.0.5 lib/kitabu/parser/pdf.rb
kitabu-1.0.4 lib/kitabu/parser/pdf.rb
kitabu-1.0.3 lib/kitabu/parser/pdf.rb
kitabu-1.0.2 lib/kitabu/parser/pdf.rb
kitabu-1.0.1 lib/kitabu/parser/pdf.rb
kitabu-1.0.0 lib/kitabu/parser/pdf.rb