Sha256: 793dd1ad1a9b800d2bb089c6ff3164602d724e12d41654175cd5b51894c0eb88

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 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/tenderlove/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

1 entries across 1 versions & 1 rubygems

Version Path
kitabu-1.0.0.rc2 lib/kitabu/parser/pdf.rb