From 7e6785129c4c70ea9e689f810fadbf62d5913465 Mon Sep 17 00:00:00 2001 From: Adi Purnama Date: Wed, 12 Nov 2025 13:32:06 +0700 Subject: [PATCH 1/3] Ensure correct numerical sorting of TOC when chapters exceed 9 --- CHANGELOG.md | 1 + lib/kitabu/exporter/epub.rb | 7 ++++--- spec/kitabu/exporter/epub_spec.rb | 13 ++++++++++++- spec/spec_helper.rb | 22 +++++++++++----------- spec/support/mybook/text/05_Chapter 5.md | 3 +++ spec/support/mybook/text/06_Chapter 6.md | 3 +++ spec/support/mybook/text/07_Chapter 7.md | 3 +++ spec/support/mybook/text/08_Chapter 8.md | 3 +++ spec/support/mybook/text/09_Chapter 9.md | 3 +++ spec/support/mybook/text/10_Chapter 10.md | 3 +++ spec/support/mybook/text/11_Chapter 11.md | 3 +++ 11 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 spec/support/mybook/text/05_Chapter 5.md create mode 100644 spec/support/mybook/text/06_Chapter 6.md create mode 100644 spec/support/mybook/text/07_Chapter 7.md create mode 100644 spec/support/mybook/text/08_Chapter 8.md create mode 100644 spec/support/mybook/text/09_Chapter 9.md create mode 100644 spec/support/mybook/text/10_Chapter 10.md create mode 100644 spec/support/mybook/text/11_Chapter 11.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 136fd41..dd3408d 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 chapters exceed 9 ## 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/spec_helper.rb b/spec/spec_helper.rb index 6cecdc5..0422125 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -45,19 +45,19 @@ def bundle_install linux: false ) - cleaner = proc do - [ - TMPDIR, - SPECDIR.join("support/mybook/output") - ].each do |i| - FileUtils.rm_rf(i) - end + # cleaner = proc do + # [ + # TMPDIR, + # SPECDIR.join("support/mybook/output") + # ].each do |i| + # FileUtils.rm_rf(i) + # end - Dir.chdir File.expand_path("..", __dir__) - end + # Dir.chdir File.expand_path("..", __dir__) + # end - config.before(&cleaner) - config.after(&cleaner) + # config.before(&cleaner) + # config.after(&cleaner) config.before { FileUtils.mkdir_p(TMPDIR) } config.before do 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 From 6d1740ae971e479c0e35bea50d8a516bcaa35455 Mon Sep 17 00:00:00 2001 From: Adi Purnama Date: Wed, 12 Nov 2025 20:17:19 +0700 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd3408d..af01b75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +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 chapters exceed 9 +- Ensure correct numerical sorting of TOC when there are more than 10 chapters ## v3.1.0 From 94e21bfc36722e7c349b4ccd0defae5d12ad0047 Mon Sep 17 00:00:00 2001 From: Adi Purnama Date: Wed, 12 Nov 2025 20:20:37 +0700 Subject: [PATCH 3/3] Revert changes in spec_helper.rb --- spec/spec_helper.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0422125..6cecdc5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -45,19 +45,19 @@ def bundle_install linux: false ) - # cleaner = proc do - # [ - # TMPDIR, - # SPECDIR.join("support/mybook/output") - # ].each do |i| - # FileUtils.rm_rf(i) - # end + cleaner = proc do + [ + TMPDIR, + SPECDIR.join("support/mybook/output") + ].each do |i| + FileUtils.rm_rf(i) + end - # Dir.chdir File.expand_path("..", __dir__) - # end + Dir.chdir File.expand_path("..", __dir__) + end - # config.before(&cleaner) - # config.after(&cleaner) + config.before(&cleaner) + config.after(&cleaner) config.before { FileUtils.mkdir_p(TMPDIR) } config.before do