From e6d5ad13dc1b723a82c0d2133ba47b278f03bcff Mon Sep 17 00:00:00 2001 From: Nando Vieira Date: Mon, 29 Jan 2024 09:10:48 -0800 Subject: [PATCH] Use `Nokogiri::HTML5.fragment` instead. This way we can stop searching for ``. --- lib/kitabu/exporter/epub.rb | 6 ++---- lib/kitabu/exporter/html.rb | 4 ++-- lib/kitabu/exporter/pdf.rb | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/kitabu/exporter/epub.rb b/lib/kitabu/exporter/epub.rb index 6b6c262..2311ee4 100644 --- a/lib/kitabu/exporter/epub.rb +++ b/lib/kitabu/exporter/epub.rb @@ -9,7 +9,7 @@ class Epub < Base def sections @sections ||= html.css(SECTION_SELECTOR).each_with_index.map do |chapter, index| - html = Nokogiri::HTML(chapter.inner_html) + html = Nokogiri::HTML5.fragment(chapter.inner_html) OpenStruct.new( index:, @@ -113,9 +113,7 @@ def write_sections! # Save file to disk. # File.open(section.filepath, "w") do |file| - content = section.html.css("body").to_xhtml.gsub( - %r{(.*?)}m, "\\1" - ) + content = section.html.to_xhtml page_title = section.html.css("h2").first.text.strip locals = config.merge(content:, page_title:) diff --git a/lib/kitabu/exporter/html.rb b/lib/kitabu/exporter/html.rb index 83e4f6e..d0667d2 100644 --- a/lib/kitabu/exporter/html.rb +++ b/lib/kitabu/exporter/html.rb @@ -69,13 +69,13 @@ def content # Parse layout file, making available all configuration entries. # private def render_layout - html = Nokogiri::HTML(content) + html = Nokogiri::HTML5.fragment(content) html = Footnotes::HTML.process(html) toc = TOC::HTML.generate(html) html = toc.html locals = config.merge( - content: html.css("body").first.inner_html, + content: html.to_html, toc: toc.toc ) diff --git a/lib/kitabu/exporter/pdf.rb b/lib/kitabu/exporter/pdf.rb index 969edd6..b7048bd 100644 --- a/lib/kitabu/exporter/pdf.rb +++ b/lib/kitabu/exporter/pdf.rb @@ -19,7 +19,7 @@ def export end def apply_footnotes! - html = Nokogiri::HTML(html_file.read) + html = Nokogiri::HTML5(html_file.read) html = Footnotes::PDF.process(html) create_html_file(html_for_print, html, "print") create_html_file(html_for_pdf, html, "pdf")