From 8a7219a61002bad94389637618b66a7ff5fb8cc3 Mon Sep 17 00:00:00 2001 From: Scott Chacon Date: Tue, 17 Apr 2012 16:25:07 -0700 Subject: [PATCH] start of book work --- Gemfile | 1 + Gemfile.lock | 2 + app/controllers/doc_controller.rb | 38 +++--- app/models/book.rb | 5 + app/models/chapter.rb | 7 ++ app/models/doc_version.rb | 26 ++++ app/models/section.rb | 10 ++ config/routes.rb | 6 +- db/migrate/20120417221951_add_book_stuff.rb | 33 +++++ db/migrate/20120417224143_add_book_url.rb | 9 ++ db/schema.rb | 33 ++++- lib/tasks/book.rake | 131 ++++++++++++++++++++ 12 files changed, 275 insertions(+), 26 deletions(-) create mode 100644 app/models/book.rb create mode 100644 app/models/chapter.rb create mode 100644 app/models/section.rb create mode 100644 db/migrate/20120417221951_add_book_stuff.rb create mode 100644 db/migrate/20120417224143_add_book_url.rb create mode 100644 lib/tasks/book.rake diff --git a/Gemfile b/Gemfile index c45aa7aa0..ae2f0c99c 100644 --- a/Gemfile +++ b/Gemfile @@ -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 db77a1f87..6d6fdbfd7 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 cccb8ded0..df6af8c12 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 000000000..e7245b0ae --- /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 000000000..f7074127c --- /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 8812583a0..b86f0b7bf 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 000000000..9f4e4b574 --- /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 e7ac3dfb9..700fe4ecf 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 000000000..001c1e943 --- /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 000000000..103d626d6 --- /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 afb6374ec..4fe5c07ec 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 000000000..bb99cf4ae --- /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>/) + chapter_title = m[2] + toc[:title] = chapter_title + end + + # replace images + if images = raw.scan(/Insert (.*?).png/) + images.each do |img| + real = "

" + raw.gsub!("Insert #{img}.png", real) + end + end + + sections = raw.split('(.*?)<\/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 = ' 500 + `convert -thumbnail 500x #{image} #{convert_image}` + else + `cp #{image} #{convert_image}` + end + end + end + end + end + end + end +end