Skip to content

Commit

Permalink
Use Nokogiri::HTML5.fragment instead.
Browse files Browse the repository at this point in the history
This way we can stop searching for `<body>`.
  • Loading branch information
fnando committed Jan 29, 2024
1 parent bd9fcb8 commit e6d5ad1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
6 changes: 2 additions & 4 deletions lib/kitabu/exporter/epub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:,
Expand Down Expand Up @@ -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{<body>(.*?)</body>}m, "\\1"
)
content = section.html.to_xhtml

page_title = section.html.css("h2").first.text.strip
locals = config.merge(content:, page_title:)
Expand Down
4 changes: 2 additions & 2 deletions lib/kitabu/exporter/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
2 changes: 1 addition & 1 deletion lib/kitabu/exporter/pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit e6d5ad1

Please sign in to comment.