Skip to content

Commit

Permalink
book: generate cross-references correctly
Browse files Browse the repository at this point in the history
Currently, the cross references in the book are handled incorrectly: the
links contain a bogus reference to the current section, and the
`ch00/*.html` files are not written correctly.

As a consequences, most of the links in the books are wrong.

Let's fix this.

Reported-by: Todd Zullinger <tmz@pobox.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Nov 18, 2023
1 parent 5e723c6 commit 6c380fd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
11 changes: 11 additions & 0 deletions script/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def self.all_books
end

attr_accessor :chapters
attr_accessor :xrefs
attr_accessor :ebook_pdf
attr_accessor :ebook_epub
attr_accessor :ebook_mobi
Expand All @@ -50,6 +51,7 @@ def initialize(edition, language_code)
@edition = edition
@language_code = language_code
@chapters = []
@xrefs = {}
end

def front_matter
Expand Down Expand Up @@ -138,6 +140,15 @@ def save
file.write("---\nredirect_to: \"book/#{@language_code}/v#{@edition}\"\n---\n")
end
end

FileUtils.mkdir_p(self.absolute_path("ch00"))
@xrefs.each do |id_xref, section|
path = self.absolute_path("ch00/#{id_xref}.html")
relurl = "#{section.relative_url(nil)}##{id_xref}"
File.open(path, 'w') do |file|
file.write("---\nredirect_to: \"#{relurl}\"\n---\n")
end
end
end
end

Expand Down
24 changes: 9 additions & 15 deletions script/update-book2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@ def genbook(language_code, &get_content)
schapter.save

# create xref
# csection = schapter.sections[1]
# xref = Xref.where(book_id: book.id, name: id_xref).first_or_create
# xref.section = csection
# xref.save
csection = schapter.sections[1] = Section.new(schapter, 1)
book.xrefs[id_xref] = csection

book_prefix = "book/#{language_code}/v#{edition}/"

Expand All @@ -141,7 +139,7 @@ def genbook(language_code, &get_content)
xlink&.each do |link|
xref = link.first
begin
html.gsub!(/href="1-.*?\.html\##{xref}"/, "href=\"ch00/#{xref}\"")
html.gsub!(/href="1-.*?\.html\##{xref}"/, "href=\"{{< relurl \"#{book_prefix}ch00/#{xref}\" >}}\"")
rescue StandardError
nil
end
Expand All @@ -151,7 +149,7 @@ def genbook(language_code, &get_content)
xlink&.each do |link|
xref = link.first
begin
html.gsub!(/href="\##{xref}"/, "href=\"ch00/#{xref}\"")
html.gsub!(/href="\##{xref}"/, "href=\"{{< relurl \"#{book_prefix}ch00/#{xref}\" >}}\"")
rescue StandardError
nil
end
Expand Down Expand Up @@ -185,17 +183,13 @@ def genbook(language_code, &get_content)
puts "::error::referenced image #{path} does not exit!"
end

# xref = Xref.where(book_id: book.id, name: id_xref).first_or_create
# xref.section = csection
# xref.save
book.xrefs[id_xref] = csection

# record all the xrefs
# sec.search(".//*[@id]").each do |id|
# id_xref = id.attribute("id").to_s
# xref = Xref.where(book_id: book.id, name: id_xref).first_or_create
# xref.section = csection
# xref.save
# end
sec.search(".//*[@id]").each do |id|
id_xref = id.attribute("id").to_s
book.xrefs[id_xref] = csection
end

section += 1
pretext = ""
Expand Down

0 comments on commit 6c380fd

Please sign in to comment.