Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/git/git-scm.com.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/chapter.rb8
-rw-r--r--app/models/section.rb6
-rw-r--r--app/views/books/_chapter_listings.html.erb6
-rw-r--r--app/views/books/show.html.erb4
-rw-r--r--db/migrate/20141024175303_add_chapter_type.rb6
-rw-r--r--lib/tasks/book2.rake122
6 files changed, 93 insertions, 59 deletions
diff --git a/app/models/chapter.rb b/app/models/chapter.rb
index 6a29c1a6..9887eb25 100644
--- a/app/models/chapter.rb
+++ b/app/models/chapter.rb
@@ -29,4 +29,12 @@ class Chapter < ActiveRecord::Base
def first_section
self.sections.first
end
+
+ def cs_number
+ if self.chapter_type == 'appendix'
+ 'A' + self.chapter_number
+ else
+ self.chapter_number
+ end
+ end
end
diff --git a/app/models/section.rb b/app/models/section.rb
index ec2615d9..eb539aeb 100644
--- a/app/models/section.rb
+++ b/app/models/section.rb
@@ -67,7 +67,11 @@ class Section < ActiveRecord::Base
end
def cs_number
- self.chapter.number.to_s + '.' + self.number.to_s
+ if self.chapter.chapter_type == 'appendix'
+ 'A' + self.chapter.chapter_number.to_s + '.' + self.number.to_s
+ else
+ self.chapter.chapter_number.to_s + '.' + self.number.to_s
+ end
end
def index
diff --git a/app/views/books/_chapter_listings.html.erb b/app/views/books/_chapter_listings.html.erb
index 4ddd24dc..991939ea 100644
--- a/app/views/books/_chapter_listings.html.erb
+++ b/app/views/books/_chapter_listings.html.erb
@@ -1,15 +1,15 @@
<ol class='book-toc'>
- <% per_column = (@book.chapters.count / 3).ceil -%>
+ <% per_column = (@book.chapters.count / 3.0).ceil -%>
<% start = per_column * (column - 1) -%>
<% stop = start + (per_column - 1) -%>
<% @book.chapters[start..stop].each do |chapter| %>
<li class='chapter'>
- <h2><%= chapter.number %>. <a href="/book/<%= @book.code %>/v<%= @book.edition %>/<%=u chapter.sections.first.slug %>"><%=raw chapter.title %></a></h2>
+ <h2><%= chapter.cs_number %>. <a href="/book/<%= @book.code %>/v<%= @book.edition %>/<%=u chapter.sections.first.slug %>"><%=raw chapter.title %></a></h2>
<ol>
<% chapter.sections.each do |section| %>
<% if !section.title.empty? %>
<li>
- <%= chapter.number %>.<%= section.number %>
+ <%= chapter.cs_number %>.<%= section.number %>
<a href="/book/<%= @book.code %>/v<%= @book.edition %>/<%= u(section.slug) %>"><%=raw section.title %></a>
</li>
<% end %>
diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb
index 30450524..a5839707 100644
--- a/app/views/books/show.html.erb
+++ b/app/views/books/show.html.erb
@@ -30,12 +30,12 @@
<ol class='book-toc'>
<% @book.chapters.each do |chapter| %>
<li class='chapter'>
- <h2><%= chapter.number %>. <a href="/book/<%= @book.code %>/v<%= @book.edition %>/<%=u chapter.sections.first.slug %>"><%=raw chapter.title %></a></h2>
+ <h2><%= chapter.cs_number %>. <a href="/book/<%= @book.code %>/v<%= @book.edition %>/<%=u chapter.sections.first.slug %>"><%=raw chapter.title %></a></h2>
<ol>
<% chapter.sections.each do |section| %>
<% if !section.title.empty? %>
<li>
- <%= chapter.number %>.<%= section.number %>
+ <%= chapter.cs_number %>.<%= section.number %>
<a href="/book/<%= @book.code %>/v<%= @book.edition %>/<%= u(section.slug) %>"><%=raw section.title %></a>
</li>
<% end %>
diff --git a/db/migrate/20141024175303_add_chapter_type.rb b/db/migrate/20141024175303_add_chapter_type.rb
new file mode 100644
index 00000000..474a880d
--- /dev/null
+++ b/db/migrate/20141024175303_add_chapter_type.rb
@@ -0,0 +1,6 @@
+class AddChapterType < ActiveRecord::Migration
+ def change
+ add_column :chapters, :chapter_type, :string
+ add_column :chapters, :chapter_number, :string
+ end
+end
diff --git a/lib/tasks/book2.rake b/lib/tasks/book2.rake
index e9963921..57987512 100644
--- a/lib/tasks/book2.rake
+++ b/lib/tasks/book2.rake
@@ -8,68 +8,84 @@ task :genbook2 => :environment do
else
books = Book.where(:edition => 2, :processed => false)
end
+
+ nav = '<div id="nav"><a href="[[nav-prev]]">prev</a> | <a href="[[nav-next]]">next</a></div>'
+
books.each do |book|
html_file = download(book.ebook_html) # download processed html ebook
Zip::File.open(html_file) do |zip_file|
# Handle entries one by one
max_chapter = 0
- zip_file.glob("ch*.html").each do |entry|
- # Extract to file/directory/symlink
- puts "Extracting #{entry.name}"
- nav = '<div id="nav"><a href="[[nav-prev]]">prev</a> | <a href="[[nav-next]]">next</a></div>'
-
- if m = /ch(.*?).html/.match(entry.name)
- puts chapter_number = m[1].to_i
- content = entry.get_input_stream.read
- doc = Nokogiri::HTML(content)
- chapter = doc.at("section[@data-type=chapter]")
- chapter_title = doc.at("section[@data-type=chapter] > h1").text
-
- pretext = doc.at("section[@data-type=chapter] > p").to_html
-
- schapter = book.chapters.where(:number => chapter_number).first_or_create
- schapter.title = chapter_title.to_s
- schapter.sha = book.ebook_html
- schapter.save
-
- schapter.sections.delete_all # clear out this chapter before rebuilding it
-
- section = 1
- chapter.search("section[@data-type=sect1]").each do |sec|
- section_title = sec.attribute('data-pdf-bookmark')
- html = pretext + sec.inner_html.to_s + nav
-
- html.gsub!('<h3', '<h4')
- html.gsub!(/\/h3>/, '/h4>')
- html.gsub!('<h2', '<h3')
- html.gsub!(/\/h2>/, '/h3>')
- html.gsub!('<h1', '<h2')
- html.gsub!(/\/h1>/, '/h2>')
-
- if subsec = html.scan(/<h3>(.*?)<\/h3>/)
- subsec.each do |sub|
- sub = sub.first
- id = sub.gsub(' ', '-')
- html.gsub!(/<h3>#{sub}<\/h3>/, "<h3 id=\"#{id}\"><a href=\"##{id}\">#{sub}</a></h3>") rescue nil
- end
- end
- puts "\t\t#{chapter_number}.#{section} : #{chapter_title} . #{section_title} - #{html.size}"
-
- csection = schapter.sections.where(:number => section).first_or_create
- csection.title = section_title.to_s
- csection.html = html
- csection.save
-
- section += 1
- pretext = ""
+ # go through all the files and order them properly
+ chapters = {}
+ zip_file.glob("*.html").each do |entry|
+ if m = /(app|ch)(.*?).html/.match(entry.name)
+ chapter_type = m[1]
+ chapter_number = m[2]
+ if chapter_type == 'app'
+ chapters["xapp#{chapter_number}"] = ['appendix', chapter_number.to_i, entry.name]
+ else
+ chapters["ch#{chapter_number}"] = ['chapter', chapter_number.to_i, entry.name]
end
- extra = schapter.sections.where("number >= #{section}")
- extra.delete_all
end
- max_chapter = chapter_number if chapter_number > max_chapter
end
- extra = book.chapters.where("number > #{max_chapter}")
+
+ # sort and create the numbers in order
+ number = 0
+ chapters.sort.each_with_index do |data, index|
+ number = index + 1
+ chapter_type, chapter_number, file = data[1]
+ content = zip_file.find_entry(file).get_input_stream.read
+
+ doc = Nokogiri::HTML(content)
+ chapter = doc.at("section[@data-type=#{chapter_type}]")
+ chapter_title = doc.at("section[@data-type=#{chapter_type}] > h1").text
+ pretext = doc.at("section[@data-type=#{chapter_type}] > p").to_html
+
+ schapter = book.chapters.where(:number => number).first_or_create
+ schapter.title = chapter_title.to_s
+ schapter.chapter_type = chapter_type
+ schapter.chapter_number = chapter_number
+ schapter.sha = book.ebook_html
+ schapter.save
+
+ schapter.sections.delete_all # clear out this chapter before rebuilding it
+
+ section = 1
+ chapter.search("section[@data-type=sect1]").each do |sec|
+ section_title = sec.attribute('data-pdf-bookmark')
+ html = pretext + sec.inner_html.to_s + nav
+
+ html.gsub!('<h3', '<h4')
+ html.gsub!(/\/h3>/, '/h4>')
+ html.gsub!('<h2', '<h3')
+ html.gsub!(/\/h2>/, '/h3>')
+ html.gsub!('<h1', '<h2')
+ html.gsub!(/\/h1>/, '/h2>')
+
+ if subsec = html.scan(/<h3>(.*?)<\/h3>/)
+ subsec.each do |sub|
+ sub = sub.first
+ id = sub.gsub(' ', '-')
+ html.gsub!(/<h3>#{sub}<\/h3>/, "<h3 id=\"#{id}\"><a href=\"##{id}\">#{sub}</a></h3>") rescue nil
+ end
+ end
+
+ puts "\t\t#{chapter_type} #{chapter_number}.#{section} : #{chapter_title} . #{section_title} - #{html.size}"
+
+ csection = schapter.sections.where(:number => section).first_or_create
+ csection.title = section_title.to_s
+ csection.html = html
+ csection.save
+
+ section += 1
+ pretext = ""
+ end # loop through sections
+ extra = schapter.sections.where("number >= #{section}")
+ extra.delete_all
+ end # if it's a chapter
+ extra = book.chapters.where("number > #{number}")
extra.delete_all
end