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--db/migrate/20121211011752_add_chapter_shas.rb9
-rw-r--r--db/schema.rb3
-rw-r--r--lib/tasks/book.rake62
3 files changed, 51 insertions, 23 deletions
diff --git a/db/migrate/20121211011752_add_chapter_shas.rb b/db/migrate/20121211011752_add_chapter_shas.rb
new file mode 100644
index 00000000..0d1344e3
--- /dev/null
+++ b/db/migrate/20121211011752_add_chapter_shas.rb
@@ -0,0 +1,9 @@
+class AddChapterShas < ActiveRecord::Migration
+ def up
+ add_column :chapters, :sha, :string
+ end
+
+ def down
+ remove_column :chapters, :string
+ end
+end \ No newline at end of file
diff --git a/db/schema.rb b/db/schema.rb
index 78e53d43..8549b90f 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120925124736) do
+ActiveRecord::Schema.define(:version => 20121211011752) do
create_table "books", :force => true do |t|
t.string "code"
@@ -27,6 +27,7 @@ ActiveRecord::Schema.define(:version => 20120925124736) do
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "number"
+ t.string "sha"
end
add_index "chapters", ["book_id"], :name => "index_chapters_on_book_id"
diff --git a/lib/tasks/book.rake b/lib/tasks/book.rake
index cc89025e..c43e30f3 100644
--- a/lib/tasks/book.rake
+++ b/lib/tasks/book.rake
@@ -9,7 +9,7 @@ require 'octokit'
CONTENT_SERVER = ENV["CONTENT_SERVER"] || "http://localhost:3000"
-def generate_pages(lang, chapter, content)
+def generate_pages(lang, chapter, content, sha)
toc = {:title => '', :sections => []}
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
@@ -64,20 +64,22 @@ def generate_pages(lang, chapter, content)
nav = '<div id="nav"><a href="[[nav-prev]]">prev</a> | <a href="[[nav-next]]">next</a></div>'
html += nav
- data = {
- :lang => lang,
- :chapter => chapter,
- :section => section,
- :chapter_title => chapter_title,
- :section_title => section_title,
- :content => html,
- :token => ENV['UPDATE_TOKEN']
- }
-
- url = CONTENT_SERVER + "/publish"
- result = Nestful.post url, :format => :form, :params => data
-
- puts "\t\t#{chapter}.#{section} : #{chapter_title} . #{section_title} - #{html.size} (#{result})"
+ # create book (if needed)
+ book = Book.where(:code => lang).first_or_create
+
+ # create chapter (if needed)
+ schapter = book.chapters.where(:number => chapter).first_or_create
+ schapter.title = chapter_title.to_s
+ schapter.sha = sha
+ schapter.save
+
+ # create/update section
+ csection = schapter.sections.where(:number => section).first_or_create
+ csection.title = section_title.to_s
+ csection.html = html.to_s
+ csection.save
+
+ puts "\t\t#{chapter}.#{section} : #{chapter_title} . #{section_title} - #{html.size}"
section += 1
end
@@ -86,24 +88,40 @@ end
desc "Generate the book html for the sites (Using Octokit gem)"
task :remote_genbook => :environment do
+ @octokit = Octokit::Client.new(:login => ENV['API_USER'], :password => ENV['API_PASS'])
repo = 'progit/progit'
book = {}
blob_content = Hash.new do |blobs, sha|
- content = Base64.decode64( Octokit.blob( repo, sha, :encoding => 'base64' ).content )
+ content = Base64.decode64( @octokit.blob( repo, sha, :encoding => 'base64' ).content )
blobs[sha] = content.encode( 'utf-8', :undef => :replace )
end
- repo_tree = Octokit.tree(repo, "HEAD", :recursive => true)
+ repo_tree = @octokit.tree(repo, "HEAD", :recursive => true)
trees = repo_tree.tree.map {|tree| tree if tree.path =~ /\.markdown$/}.compact
trees.each do |tree|
#tree = trees.first
lang, section, chapter = tree.path.split("/")
section_number = $1 if section =~ /^(\d+)/
chapter_number = $1 if chapter =~ /chapter(\d+)\.markdown/
- #book[lang] ||= []
- content = blob_content[tree.sha]
- puts "*** #{tree.sha}- #{lang} - #{section} - #{chapter} - #{section_number}:#{chapter_number}"
- #book[lang] << {:sha => tree.sha, :section => section, :chapter => chapter, :blob => content}
- generate_pages(lang, chapter_number, content)
+
+ skip = false
+
+ if book = Book.where(:code => lang).first
+ c = book.chapters.where(:number => chapter_number.to_i).first
+ if c && (c.sha == tree.sha)
+ skip = true
+ end
+ end
+
+ skip = true if chapter_number.to_i == 0
+
+ puts "*** #{skip} #{tree.sha}- #{lang} - #{section} - #{chapter} - #{section_number}:#{chapter_number}"
+
+ if !skip
+ #book[lang] ||= []
+ content = blob_content[tree.sha]
+ #book[lang] << {:sha => tree.sha, :section => section, :chapter => chapter, :blob => content}
+ generate_pages(lang, chapter_number, content, tree.sha)
+ end
end
#p book
end