diff --git a/CHANGELOG.md b/CHANGELOG.md index 136fd41..af01b75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - Remove inline alignment from tables. - Render `
` for images with title. - Render abbreviations with (e.g. `*[CSS]: Cascading Style Sheets`). +- Ensure correct numerical sorting of TOC when there are more than 10 chapters ## v3.1.0 diff --git a/lib/kitabu/exporter/epub.rb b/lib/kitabu/exporter/epub.rb index 716f5ec..e8ebba6 100644 --- a/lib/kitabu/exporter/epub.rb +++ b/lib/kitabu/exporter/epub.rb @@ -8,13 +8,14 @@ class Epub < Base def sections @sections ||= - html.css(SECTION_SELECTOR).each_with_index.map do |chapter, index| + html.css(SECTION_SELECTOR).each.with_index(1).map do |chapter, index| html = Nokogiri::HTML5.fragment(chapter.inner_html) + section_number = index.to_s.rjust(2, '0') OpenStruct.new( index:, - filename: "section_#{index}.html", - filepath: tmp_dir.join("section_#{index}.html").to_s, + filename: "section_#{section_number}.html", + filepath: tmp_dir.join("section_#{section_number}.html").to_s, html: ) end diff --git a/spec/kitabu/exporter/epub_spec.rb b/spec/kitabu/exporter/epub_spec.rb index afa999c..073da70 100644 --- a/spec/kitabu/exporter/epub_spec.rb +++ b/spec/kitabu/exporter/epub_spec.rb @@ -5,10 +5,21 @@ describe Kitabu::Exporter::Epub do let(:root) { SPECDIR.join("support/mybook") } - it "generates e-pub" do + before do Kitabu::Exporter::HTML.export(root) Kitabu::Exporter::Epub.export(root) + end + it "generates e-pub" do expect(root.join("output/mybook.epub")).to be_file end + + it "adds leading zero to 1 digit number" do + sections = root.join("output/epub").glob("section*.{xhtml,html}") + sections = sections.map do |path| + path.to_s.split("/").last + end + expected = 1.upto(12).map {|i| "section_#{i.to_s.rjust(2, '0')}.html" } + expect(sections).to eq(expected) + end end diff --git a/spec/support/mybook/text/05_Chapter 5.md b/spec/support/mybook/text/05_Chapter 5.md new file mode 100644 index 0000000..aea40dd --- /dev/null +++ b/spec/support/mybook/text/05_Chapter 5.md @@ -0,0 +1,3 @@ +## Chapter 5 + +This is Chapter 5 diff --git a/spec/support/mybook/text/06_Chapter 6.md b/spec/support/mybook/text/06_Chapter 6.md new file mode 100644 index 0000000..2b84380 --- /dev/null +++ b/spec/support/mybook/text/06_Chapter 6.md @@ -0,0 +1,3 @@ +## Chapter 6 + +This is Chapter 6 diff --git a/spec/support/mybook/text/07_Chapter 7.md b/spec/support/mybook/text/07_Chapter 7.md new file mode 100644 index 0000000..3bd57c8 --- /dev/null +++ b/spec/support/mybook/text/07_Chapter 7.md @@ -0,0 +1,3 @@ +## Chapter 7 + +This is Chapter 7 diff --git a/spec/support/mybook/text/08_Chapter 8.md b/spec/support/mybook/text/08_Chapter 8.md new file mode 100644 index 0000000..9416525 --- /dev/null +++ b/spec/support/mybook/text/08_Chapter 8.md @@ -0,0 +1,3 @@ +## Chapter 8 + +This is Chapter 8 diff --git a/spec/support/mybook/text/09_Chapter 9.md b/spec/support/mybook/text/09_Chapter 9.md new file mode 100644 index 0000000..01b85f7 --- /dev/null +++ b/spec/support/mybook/text/09_Chapter 9.md @@ -0,0 +1,3 @@ +## Chapter 9 + +This is Chapter 9 diff --git a/spec/support/mybook/text/10_Chapter 10.md b/spec/support/mybook/text/10_Chapter 10.md new file mode 100644 index 0000000..44415aa --- /dev/null +++ b/spec/support/mybook/text/10_Chapter 10.md @@ -0,0 +1,3 @@ +## Chapter 10 + +This is Chapter 10 diff --git a/spec/support/mybook/text/11_Chapter 11.md b/spec/support/mybook/text/11_Chapter 11.md new file mode 100644 index 0000000..78fd39b --- /dev/null +++ b/spec/support/mybook/text/11_Chapter 11.md @@ -0,0 +1,3 @@ +## Chapter 11 + +This is Chapter 11