diff --git a/script/book.rb b/script/book.rb index bd61b27f9b..73fbba3237 100644 --- a/script/book.rb +++ b/script/book.rb @@ -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 @@ -50,6 +51,7 @@ def initialize(edition, language_code) @edition = edition @language_code = language_code @chapters = [] + @xrefs = {} end def front_matter @@ -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 diff --git a/script/update-book2.rb b/script/update-book2.rb index a15a6f5b96..71511b2e73 100644 --- a/script/update-book2.rb +++ b/script/update-book2.rb @@ -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}/" @@ -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 @@ -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 @@ -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 = ""