diff options
author | Scott Chacon <schacon@gmail.com> | 2012-04-18 03:25:07 +0400 |
---|---|---|
committer | Scott Chacon <schacon@gmail.com> | 2012-05-02 23:11:11 +0400 |
commit | 8a7219a61002bad94389637618b66a7ff5fb8cc3 (patch) | |
tree | bea305d6634b72c774928d3989725ba418509d6c | |
parent | 0c790c14286bdd8095efcb9fd1be570cfdc41588 (diff) |
start of book work
-rw-r--r-- | Gemfile | 1 | ||||
-rw-r--r-- | Gemfile.lock | 2 | ||||
-rw-r--r-- | app/controllers/doc_controller.rb | 38 | ||||
-rw-r--r-- | app/models/book.rb | 5 | ||||
-rw-r--r-- | app/models/chapter.rb | 7 | ||||
-rw-r--r-- | app/models/doc_version.rb | 26 | ||||
-rw-r--r-- | app/models/section.rb | 10 | ||||
-rw-r--r-- | config/routes.rb | 6 | ||||
-rw-r--r-- | db/migrate/20120417221951_add_book_stuff.rb | 33 | ||||
-rw-r--r-- | db/migrate/20120417224143_add_book_url.rb | 9 | ||||
-rw-r--r-- | db/schema.rb | 33 | ||||
-rw-r--r-- | lib/tasks/book.rake | 131 |
12 files changed, 275 insertions, 26 deletions
@@ -20,6 +20,7 @@ gem 'rubyzip' gem 'octokit' gem 'dalli' gem 'diff-lcs' +gem 'redcarpet' # Gems used only for assets and not required # in production environments by default. diff --git a/Gemfile.lock b/Gemfile.lock index db77a1f8..6d6fdbfd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -133,6 +133,7 @@ GEM rake (0.9.2.2) rdoc (3.12) json (~> 1.4) + redcarpet (1.11.4) rest-client (1.6.7) mime-types (>= 1.16) rubyzip (0.9.7) @@ -198,6 +199,7 @@ DEPENDENCIES octokit pg rails (= 3.2.2) + redcarpet rest-client rubyzip sass-rails diff --git a/app/controllers/doc_controller.rb b/app/controllers/doc_controller.rb index cccb8ded..df6af8c1 100644 --- a/app/controllers/doc_controller.rb +++ b/app/controllers/doc_controller.rb @@ -18,33 +18,10 @@ class DocController < ApplicationController doc_version = DocVersion.latest_for(params[:file]) end - @versions = [] - versions = DocVersion.latest_versions(params[:file]) - unchanged = [] - for i in 0..(versions.size-2) - v = versions[i] - prev = versions[i+1] - sha2 = v.doc.blob_sha - sha1 = prev.doc.blob_sha - if sha1 == sha2 - unchanged << v.version.name - else - if unchanged.size > 0 - if unchanged.size == 1 - @versions << {:name => "#{unchanged.first} no changes", :changed => false} - else - @versions << {:name => "#{unchanged.last} → #{unchanged.first} no changes", :changed => false} - end - unchanged = [] - end - @versions << {:name => v.version.name, :time => v.version.committed, :diff => Doc.get_diff(sha2, sha1), :changed => true} - end - end - @versions = @versions[0,20] - if doc_version.nil? redirect_to :ref else + @versions = DocVersion.version_changes(params[:file], 20) @last = DocVersion.last_changed(params[:file]) @version = doc_version.version @file = doc_version.doc_file @@ -53,8 +30,21 @@ class DocController < ApplicationController end def book + lang = params[:lang] || 'en' + end + + def book_section end + def book_update + # TODO: check credentials + lang = params[:lang] + section = params[:section] + content = params[:content] + end + + # API Methods to update book content # + def videos end diff --git a/app/models/book.rb b/app/models/book.rb new file mode 100644 index 00000000..e7245b0a --- /dev/null +++ b/app/models/book.rb @@ -0,0 +1,5 @@ +# t.string :code +# t.timestamps +class Book < ActiveRecord::Base + has_many :chapters +end diff --git a/app/models/chapter.rb b/app/models/chapter.rb new file mode 100644 index 00000000..f7074127 --- /dev/null +++ b/app/models/chapter.rb @@ -0,0 +1,7 @@ +# t.string :title +# t.belongs_to :book +# t.timestamps +class Chapter < ActiveRecord::Base + belongs_to :book + has_many :sections +end diff --git a/app/models/doc_version.rb b/app/models/doc_version.rb index 8812583a..b86f0b7b 100644 --- a/app/models/doc_version.rb +++ b/app/models/doc_version.rb @@ -25,6 +25,32 @@ class DocVersion < ActiveRecord::Base for_doc(doc_name).joins(:version).where(['versions.name=?', version_name]).first end + def self.version_changes(file, size = 20) + versions = [] + unchanged = [] + vers = DocVersion.latest_versions(file) + for i in 0..(vers.size-2) + v = vers[i] + prev = vers[i+1] + sha2 = v.doc.blob_sha + sha1 = prev.doc.blob_sha + if sha1 == sha2 + unchanged << v.version.name + else + if unchanged.size > 0 + if unchanged.size == 1 + versions << {:name => "#{unchanged.first} no changes", :changed => false} + else + versions << {:name => "#{unchanged.last} → #{unchanged.first} no changes", :changed => false} + end + unchanged = [] + end + versions << {:name => v.version.name, :time => v.version.committed, :diff => Doc.get_diff(sha2, sha1), :changed => true} + end + end + versions[0,size] + end + def index if BONSAI file = self.doc_file diff --git a/app/models/section.rb b/app/models/section.rb new file mode 100644 index 00000000..9f4e4b57 --- /dev/null +++ b/app/models/section.rb @@ -0,0 +1,10 @@ +# t.string :title +# t.string :slug +# t.text :plain +# t.text :html +# t.string :source_url +# t.belongs_to :chapter +# t.timestamps +class Section < ActiveRecord::Base + belongs_to :chapter +end diff --git a/config/routes.rb b/config/routes.rb index e7ac3dfb..700fe4ec 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,10 +6,14 @@ Gitscm::Application.routes.draw do match "/ref/:file" => "doc#man" match "/ref/:file/:version" => "doc#man", :version => /[^\/]+/ match "/test" => "doc#test" - match "/book" => "doc#book" match "/videos" => "doc#videos" match "/doc/ext" => "doc#ext" + match "/book" => "doc#book" + match "/book/:lang" => "doc#book" + match "/book/:lang/:slug" => "doc#book_section" + match "/book/update" => "doc#book_update" + match "/about" => "about#index" match "/community" => "community#index" diff --git a/db/migrate/20120417221951_add_book_stuff.rb b/db/migrate/20120417221951_add_book_stuff.rb new file mode 100644 index 00000000..001c1e94 --- /dev/null +++ b/db/migrate/20120417221951_add_book_stuff.rb @@ -0,0 +1,33 @@ +class AddBookStuff < ActiveRecord::Migration + def up + create_table :books do |t| + t.string :code + t.timestamps + end + add_index :books, [:code] + + create_table :chapters do |t| + t.string :title + t.belongs_to :book + t.timestamps + end + add_index :chapters, [:book_id] + + create_table :sections do |t| + t.string :title + t.string :slug + t.text :plain + t.text :html + t.belongs_to :chapter + t.timestamps + end + add_index :sections, [:slug] + add_index :sections, [:chapter_id] + end + + def down + drop_table :books + drop_table :chapters + drop_table :sections + end +end diff --git a/db/migrate/20120417224143_add_book_url.rb b/db/migrate/20120417224143_add_book_url.rb new file mode 100644 index 00000000..103d626d --- /dev/null +++ b/db/migrate/20120417224143_add_book_url.rb @@ -0,0 +1,9 @@ +class AddBookUrl < ActiveRecord::Migration + def up + add_column :sections, :source_url, :string + end + + def down + remove_column :sections, :source_url + end +end diff --git a/db/schema.rb b/db/schema.rb index afb6374e..4fe5c07e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,24 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120417001921) do +ActiveRecord::Schema.define(:version => 20120417224143) do + + create_table "books", :force => true do |t| + t.string "code" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + add_index "books", ["code"], :name => "index_books_on_code" + + create_table "chapters", :force => true do |t| + t.string "title" + t.integer "book_id" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + add_index "chapters", ["book_id"], :name => "index_chapters_on_book_id" create_table "doc_files", :force => true do |t| t.string "name" @@ -48,6 +65,20 @@ ActiveRecord::Schema.define(:version => 20120417001921) do t.datetime "updated_at", :null => false end + create_table "sections", :force => true do |t| + t.string "title" + t.string "slug" + t.text "plain" + t.text "html" + t.integer "chapter_id" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + t.string "source_url" + end + + add_index "sections", ["chapter_id"], :name => "index_sections_on_chapter_id" + add_index "sections", ["slug"], :name => "index_sections_on_slug" + create_table "versions", :force => true do |t| t.string "name" t.string "commit_sha" diff --git a/lib/tasks/book.rake b/lib/tasks/book.rake new file mode 100644 index 00000000..bb99cf4a --- /dev/null +++ b/lib/tasks/book.rake @@ -0,0 +1,131 @@ +require 'redcarpet' +require 'awesome_print' + +# export GITBOOK_DIR=../../writing/progit/ + +def generate_pages(lang, chapter, content) + toc = {:title => '', :sections => []} + + doc = Maruku.new(content) + markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML) + markdown.render(content) + + raw = doc.to_html + if m = raw.match(/<h1(.*?)>(.*?)<\/h1>/) + chapter_title = m[2] + toc[:title] = chapter_title + end + + # replace images + if images = raw.scan(/Insert (.*?).png/) + images.each do |img| + real = "<center><img src=\"/figures/ch#{chapter}/#{img}-tn.png\"></center><br/>" + raw.gsub!("Insert #{img}.png", real) + end + end + + sections = raw.split('<h2') + section = 0 + sections.each do |sec| + + section_title = '' + if section_match = sec.match(/>(.*?)<\/h2>/) + section_title = section_match[1] + toc[:sections] << [section, section_title] + else + toc[:sections] << [section, nil] + end + + if lang == 'en' + pname = "../../../book/ch#{chapter}-#{section}.html" + else + FileUtils.mkdir("../../../book/#{lang}") rescue nil + pname = "../../../book/#{lang}/ch#{chapter}-#{section}.html" + end + + full_title = section_match ? "#{chapter_title} #{section_title}" : chapter_title + layout = lang == 'en' ? 'master' : 'translation' + html = "--- +layout: #{layout} +title: Pro Git #{chapter}.#{section} #{full_title} +--- +" + if section_match + sec = '<h2' + sec + else + html += "<h1>Chapter #{chapter}</h1>" + end + + html += sec + + nav = "<div id='nav'> +<a href='[[nav-prev]]'>prev</a> | <a href='[[nav-next]]'>next</a> +</div>" + html += nav + + File.open(pname, 'w+') { |f| f.write(html) } + section += 1 + end + toc +end + +# generate the site +desc "Generate the book html for the site" +task :genbook do + genlang = ENV['GENLANG'] + gitbook_dir = ENV['GITBOOK_DIR'] + + if !File.exists?(gitbook_dir) + puts "No such directory" + exit + end + + Dir.chdir(File.join(gitbook_dir, 'translations')) do + Dir.glob("*").each do |lang| + chapter_number = 0 + toc = [] + next if genlang && genlang != lang + Dir.chdir(lang) do + Dir.glob("*").each do |chapter| + puts 'generating : ' + lang + '/' + chapter + content = '' + Dir.chdir(chapter) do + Dir.glob('*').each do |section| + content += File.read(section) + end + chapter_number += 1 + toc << [chapter_number, generate_pages(lang, chapter_number, content)] + end rescue nil + end + end + end + end + +end + +# generate the site +desc "Convert images" +task :convert_images do + Dir.chdir('figures') do + Dir.glob("*").each do |chapter| + Dir.chdir(chapter) do + Dir.glob("*").each do |image| + puts image + (im, ending) = image.split('.') + if ending == 'png' and im[-3, 3] != '-tn' + convert_image = "#{im}-tn.png" + if !File.exists?(convert_image) + width_out = `exiftool #{image} | grep 'Image Width'` + width = width_out.scan(/: (\d+)/).first.first.to_i + if width > 500 + `convert -thumbnail 500x #{image} #{convert_image}` + else + `cp #{image} #{convert_image}` + end + end + end + end + end + end + end +end |