start of book work
This commit is contained in:
Родитель
0c790c1428
Коммит
8a7219a610
1
Gemfile
1
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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
# t.string :code
|
||||
# t.timestamps
|
||||
class Book < ActiveRecord::Base
|
||||
has_many :chapters
|
||||
end
|
|
@ -0,0 +1,7 @@
|
|||
# t.string :title
|
||||
# t.belongs_to :book
|
||||
# t.timestamps
|
||||
class Chapter < ActiveRecord::Base
|
||||
belongs_to :book
|
||||
has_many :sections
|
||||
end
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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
|
33
db/schema.rb
33
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"
|
||||
|
|
|
@ -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
|
Загрузка…
Ссылка в новой задаче