Skip to content

Commit

Permalink
Merge pull request #917 from jnavila/local_progit2
Browse files Browse the repository at this point in the history
[RFC] Start task for publishing progit2 books from git
  • Loading branch information
peff authored Jan 18, 2017
2 parents 8d9f738 + 355e954 commit 6325d8e
Show file tree
Hide file tree
Showing 212 changed files with 196 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def link
link = params[:link]
@book = Book.where(:code => params[:lang], :edition => params[:edition]).first
xref = @book.xrefs.where(:name => link).first
return redirect_to "/book/#{@book.code}/v#{@book.edition}/#{xref.section.slug}##{xref.name}" unless @content
return redirect_to "/book/#{@book.code}/v#{@book.edition}/#{ERB::Util.url_encode(xref.section.slug)}##{xref.name}" unless @content
end

def section
Expand Down
4 changes: 2 additions & 2 deletions app/models/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def prev_slug
lang = self.book.code
prev_number = self.number - 1
if section = self.sections.where(:number => prev_number).first
return "/book/#{lang}/#{ERB::Util.url_encode(section.slug)}"
return "/book/#{lang}/v#{self.book.edition}/#{ERB::Util.url_encode(section.slug)}"
else
# find previous chapter
if ch = self.chapter.prev
if section = ch.last_section
return "/book/#{lang}/#{ERB::Util.url_encode(section.slug)}"
return "/book/#{lang}/v#{self.book.edition}/#{ERB::Util.url_encode(section.slug)}"
end
end
end
Expand Down
196 changes: 193 additions & 3 deletions lib/tasks/book2.rake
Original file line number Diff line number Diff line change
@@ -1,5 +1,195 @@
require 'zip'
require 'nokogiri'
require 'octokit'
require 'pathname'

def expand(content, path, &get_content)
content.gsub(/include::(\S+)\[\]/) do |line|
if File.dirname(path)=="."
new_fname = $1
else
new_fname = (Pathname.new(path).dirname + Pathname.new($1)).cleanpath.to_s
end
new_content = get_content.call(new_fname)
if new_content
expand(new_content.gsub("\xEF\xBB\xBF".force_encoding("UTF-8"), ''), new_fname) {|c| get_content.call (c)}
else
puts "#{new_fname} could not be resolved for expansion"
""
end
end
end

def repo_name(lang)
if lang == "en"
'progit/progit2'
else
'progit/progit2-'+ lang
end
end

desc "Generate book html directly from git repo"
task :remote_genbook2 => :environment do
template_dir = File.join(Rails.root, 'templates')

nav = '<div id="nav"><a href="[[nav-prev]]">prev</a> | <a href="[[nav-next]]">next</a></div>'
@octokit = Octokit::Client.new(:login => ENV['API_USER'], :password => ENV['API_PASS'])

if ENV['GENLANG']
books = Book.where(:edition => 2, :code => ENV['GENLANG'])
else
all_books = Book.where(:edition => 2)
books = all_books.select do |book|
repo_head = @octokit.ref(repo_name(book.code), "heads/master").object[:sha]
repo_head != book.ebook_html
end
end

books.each do |book|
repo = repo_name(book.code)
blob_content = Hash.new do |blobs, sha|
content = Base64.decode64( @octokit.blob(repo, sha, :encoding => 'base64' ).content )
blobs[sha] = content.force_encoding('UTF-8')
end

repo_tree = @octokit.tree(repo, "HEAD", :recursive => true)
atlas = JSON.parse(blob_content[repo_tree.tree.detect { |node| node[:path]=="atlas.json"}[:sha]])

chapters = {}
appnumber = 0
chnumber = 0
secnumber = 0
ids = {}

atlas['files'].each_with_index do |filename, index|
if filename =~ /book\/[0-9].*\/1-[^\/]*\.asc/
chnumber += 1
chapters ["ch#{secnumber}"] = ['chapter', chnumber, filename]
secnumber += 1
end
if filename =~ /book\/[A-C].*\.asc/
appnumber += 1
chapters ["ch#{secnumber}"] = ['appendix', appnumber, filename]
secnumber += 1
end
end
chapter_list = atlas['files'].select {|filename| filename =~ /book\/[0-9A-C].*\/1-[^\/]*\.asc/}

initial_content = "include::" + chapter_list.join("[]\n\ninclude::") + "[]\n"

content = expand(initial_content, "root.asc") do |filename|
file_handle = repo_tree.tree.detect { |tree| tree[:path] == filename }
if file_handle
blob_content[file_handle[:sha]]
end
end

asciidoc = Asciidoctor::Document.new(content,template_dir: template_dir)
html = asciidoc.render
alldoc = Nokogiri::HTML(html)
number = 1

alldoc.xpath("//div[@class='sect1']").each_with_index do |entry, index |
chapter_type, chapter_number, filename = chapters ["ch#{index}"]
chapter = entry
chapter_title = entry.at("h2").content

next if !chapter_title
next if !chapter_number

number = chapter_number
if chapter_type == 'appendix'
puts "appendix #{chapter_number}"
number = 100 + chapter_number
end

pretext = entry.search("div[@class=sectionbody]/div/p").to_html
id_xref = chapter.at("h2").attribute('id').to_s

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

# create xref
csection = schapter.sections.where(:number => 1).first_or_create
xref = Xref.where(:book_id => book.id, :name => id_xref).first_or_create
xref.section = csection
xref.save

section = 1
chapter.search("div[@class=sect2]").each do |sec|

id_xref = sec.at("h3").attribute('id').to_s

section_title = sec.at("h3").content

html = sec.inner_html.to_s + nav

html.gsub!('<h3', '<h2')
html.gsub!(/\/h3>/, '/h2>')
html.gsub!('<h4', '<h3')
html.gsub!(/\/h4>/, '/h3>')
html.gsub!('<h5', '<h4')
html.gsub!(/\/h5>/, '/h4>')

if xlink = html.scan(/href=\"1-.*?\.html\#(.*?)\"/)
xlink.each do |link|
xref = link.first
html.gsub!(/href=\"1-.*?\.html\##{xref}\"/, "href=\"ch00/#{xref}\"") rescue nil
end
end

if xlink = html.scan(/href=\"\#(.*?)\"/)
xlink.each do |link|
xref = link.first
html.gsub!(/href=\"\##{xref}\"/, "href=\"ch00/#{xref}\"") rescue nil
end
end

if subsec = html.scan(/<img src="(.*?)"/)
subsec.each do |sub|
sub = sub.first
html.gsub!(/<img src="#{sub}"/, "<img src=\"/book/en/v2/#{sub}\"") 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 = pretext + html
csection.save

xref = Xref.where(:book_id => book.id, :name => id_xref).first_or_create
xref.section = csection
xref.save

# record all the xrefs
(sec.search(".//*[@id]")).each do |id|
id_xref = id.attribute('id').to_s
puts id_xref
xref = Xref.where(:book_id => book.id, :name => id_xref).first_or_create
xref.section = csection
xref.save
end

section += 1
pretext = ""
end
end
repo_head = @octokit.ref(repo, "heads/master").object[:sha]
book.ebook_html = repo_head
book.save

book.sections.each do |section|
section.set_slug
section.save
end
end
end

desc "Generate the book html for the sites (by downloading from atlas)"
task :genbook2 => :environment do
Expand Down Expand Up @@ -29,8 +219,8 @@ task :genbook2 => :environment do
navi = toc['navigations']['navigation']
elsif toc['navigation']
navi = toc['navigation']['navigation']
end
end

navi.each_with_index do |chthing, index|
if chthing['type'] == 'appendix'
appnumber += 1
Expand All @@ -56,7 +246,7 @@ task :genbook2 => :environment do
doc = Nokogiri::HTML(content)
chapter = doc.at("section[@data-type=#{chapter_type}]")
chapter_title = title

next if !chapter_title
next if !chapter_number

Expand Down
Binary file added public/book/en/v2/images/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/2fa-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/account-settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/advance-master.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/advance-testing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/areas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/avatar-crop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/basic-branching-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/basic-branching-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/basic-branching-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/basic-branching-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/basic-branching-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/basic-branching-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/basic-merging-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/basic-merging-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/basic-rebase-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/book/en/v2/images/basic-rebase-2.png
Binary file added public/book/en/v2/images/basic-rebase-3.png
Binary file added public/book/en/v2/images/basic-rebase-4.png
Binary file added public/book/en/v2/images/benevolent-dictator.png
Binary file added public/book/en/v2/images/bitnami.png
Binary file added public/book/en/v2/images/blink-01-start.png
Binary file added public/book/en/v2/images/blink-02-pr.png
Binary file added public/book/en/v2/images/blink-04-email.png
Binary file added public/book/en/v2/images/blink-04-pr-comment.png
Binary file added public/book/en/v2/images/blink-06-final.png
Binary file added public/book/en/v2/images/branch-and-history.png
Binary file added public/book/en/v2/images/branch_widget_mac.png
Binary file added public/book/en/v2/images/branch_widget_win.png
Binary file added public/book/en/v2/images/centralized.png
Binary file added public/book/en/v2/images/centralized_workflow.png
Binary file added public/book/en/v2/images/checkout-master.png
Binary file added public/book/en/v2/images/clean.png
Binary file added public/book/en/v2/images/collaborators.png
Binary file added public/book/en/v2/images/commit-and-tree.png
Binary file added public/book/en/v2/images/commits-and-parents.png
Binary file added public/book/en/v2/images/cover.png
Binary file added public/book/en/v2/images/data-model-1.png
Binary file added public/book/en/v2/images/data-model-2.png
Binary file added public/book/en/v2/images/data-model-3.png
Binary file added public/book/en/v2/images/data-model-4.png
Binary file added public/book/en/v2/images/deltas.png
Binary file added public/book/en/v2/images/distributed.png
Binary file added public/book/en/v2/images/double-dot.png
Binary file added public/book/en/v2/images/egit.png
Binary file added public/book/en/v2/images/email-settings.png
Binary file added public/book/en/v2/images/emoji.png
Binary file added public/book/en/v2/images/forkbutton.png
Binary file added public/book/en/v2/images/git-bash.png
Binary file added public/book/en/v2/images/git-diff-check.png
Binary file added public/book/en/v2/images/git-fusion-boot.png
Binary file added public/book/en/v2/images/git-gui.png
Binary file added public/book/en/v2/images/git-instaweb.png
Binary file added public/book/en/v2/images/git-osx-installer.png
Binary file added public/book/en/v2/images/git-tfs-ct.png
Binary file added public/book/en/v2/images/github_mac.png
Binary file added public/book/en/v2/images/github_win.png
Binary file added public/book/en/v2/images/gitk.png
Binary file added public/book/en/v2/images/gitlab-broadcast.png
Binary file added public/book/en/v2/images/gitlab-groups.png
Binary file added public/book/en/v2/images/gitlab-menu.png
Binary file added public/book/en/v2/images/gitlab-users.png
Binary file added public/book/en/v2/images/head-to-master.png
Binary file added public/book/en/v2/images/head-to-testing.png
Binary file added public/book/en/v2/images/hubot.png
Binary file added public/book/en/v2/images/integration-manager.png
Binary file added public/book/en/v2/images/interesting-rebase-1.png
Binary file added public/book/en/v2/images/interesting-rebase-4.png
Binary file added public/book/en/v2/images/interesting-rebase-5.png
Binary file added public/book/en/v2/images/large-merges-1.png
Binary file added public/book/en/v2/images/large-merges-2.png
Binary file added public/book/en/v2/images/lifecycle.png
Binary file added public/book/en/v2/images/local.png
Binary file added public/book/en/v2/images/lr-branches-1.png
Binary file added public/book/en/v2/images/lr-branches-2.png
Binary file added public/book/en/v2/images/maint-01-email.png
Binary file added public/book/en/v2/images/maint-02-merge.png
Binary file added public/book/en/v2/images/maint-03-email-resp.png
Binary file added public/book/en/v2/images/maint-04-target.png
Binary file added public/book/en/v2/images/maint-05-mentions.png
Binary file added public/book/en/v2/images/maint-06-unsubscribe.png
Binary file added public/book/en/v2/images/maint-09-contrib.png
Binary file added public/book/en/v2/images/maint-11-transfer.png
Binary file added public/book/en/v2/images/managed-team-1.png
Binary file added public/book/en/v2/images/managed-team-2.png
Binary file added public/book/en/v2/images/managed-team-3.png
Binary file added public/book/en/v2/images/managed-team-flow.png
Binary file added public/book/en/v2/images/markdown-01-example.png
Binary file added public/book/en/v2/images/markdown-02-tasks.png
Binary file added public/book/en/v2/images/markdown-05-quote.png
Binary file added public/book/en/v2/images/markdown-07-emoji.png
Binary file added public/book/en/v2/images/mentions-01-syntax.png
Binary file added public/book/en/v2/images/mentions-02-render.png
Binary file added public/book/en/v2/images/mentions-03-closed.png
Binary file added public/book/en/v2/images/merging-workflows-1.png
Binary file added public/book/en/v2/images/merging-workflows-2.png
Binary file added public/book/en/v2/images/merging-workflows-3.png
Binary file added public/book/en/v2/images/merging-workflows-5.png
Binary file added public/book/en/v2/images/new-repo.png
Binary file added public/book/en/v2/images/neworg.png
Binary file added public/book/en/v2/images/newrepo.png
Binary file added public/book/en/v2/images/newrepoform.png
Binary file added public/book/en/v2/images/notifications.png
Binary file added public/book/en/v2/images/orgs-01-page.png
Binary file added public/book/en/v2/images/orgs-02-teams.png
Binary file added public/book/en/v2/images/orgs-03-audit.png
Binary file added public/book/en/v2/images/p4merge.png
Binary file added public/book/en/v2/images/posh-git.png
Binary file added public/book/en/v2/images/pr-01-fail.png
Binary file added public/book/en/v2/images/pr-02-merge-fix.png
Binary file added public/book/en/v2/images/public-small-1.png
Binary file added public/book/en/v2/images/public-small-2.png
Binary file added public/book/en/v2/images/public-small-3.png
Binary file added public/book/en/v2/images/rebasing-1.png
Binary file added public/book/en/v2/images/rebasing-2.png
Binary file added public/book/en/v2/images/remote-branches-1.png
Binary file added public/book/en/v2/images/remote-branches-2.png
Binary file added public/book/en/v2/images/remote-branches-3.png
Binary file added public/book/en/v2/images/remote-branches-4.png
Binary file added public/book/en/v2/images/remote-branches-5.png
Binary file added public/book/en/v2/images/replace1.png
Binary file added public/book/en/v2/images/replace2.png
Binary file added public/book/en/v2/images/replace3.png
Binary file added public/book/en/v2/images/replace4.png
Binary file added public/book/en/v2/images/replace5.png
Binary file added public/book/en/v2/images/reposettingslink.png
Binary file added public/book/en/v2/images/rerere1.png
Binary file added public/book/en/v2/images/rerere2.png
Binary file added public/book/en/v2/images/rerere3.png
Binary file added public/book/en/v2/images/reset-checkout.png
Binary file added public/book/en/v2/images/reset-ex1.png
Binary file added public/book/en/v2/images/reset-ex2.png
Binary file added public/book/en/v2/images/reset-ex3.png
Binary file added public/book/en/v2/images/reset-ex4.png
Binary file added public/book/en/v2/images/reset-ex5.png
Binary file added public/book/en/v2/images/reset-ex6.png
Binary file added public/book/en/v2/images/reset-hard.png
Binary file added public/book/en/v2/images/reset-mixed.png
Binary file added public/book/en/v2/images/reset-path1.png
Binary file added public/book/en/v2/images/reset-path2.png
Binary file added public/book/en/v2/images/reset-path3.png
Binary file added public/book/en/v2/images/reset-soft.png
Binary file added public/book/en/v2/images/reset-squash-r1.png
Binary file added public/book/en/v2/images/reset-squash-r2.png
Binary file added public/book/en/v2/images/reset-squash-r3.png
Binary file added public/book/en/v2/images/reset-start.png
Binary file added public/book/en/v2/images/reset-workflow.png
Binary file added public/book/en/v2/images/scripting-07-status.png
Binary file added public/book/en/v2/images/signup.png
Binary file added public/book/en/v2/images/small-team-1.png
Binary file added public/book/en/v2/images/small-team-2.png
Binary file added public/book/en/v2/images/small-team-3.png
Binary file added public/book/en/v2/images/small-team-4.png
Binary file added public/book/en/v2/images/small-team-5.png
Binary file added public/book/en/v2/images/small-team-6.png
Binary file added public/book/en/v2/images/small-team-7.png
Binary file added public/book/en/v2/images/small-team-flow.png
Binary file added public/book/en/v2/images/smudge.png
Binary file added public/book/en/v2/images/snapshots.png
Binary file added public/book/en/v2/images/ssh-keys.png
Binary file added public/book/en/v2/images/topic-branches-1.png
Binary file added public/book/en/v2/images/topic-branches-2.png
Binary file added public/book/en/v2/images/two-branches.png
Binary file added public/book/en/v2/images/undomerge-reset.png
Binary file added public/book/en/v2/images/undomerge-revert.png
Binary file added public/book/en/v2/images/undomerge-revert2.png
Binary file added public/book/en/v2/images/undomerge-revert3.png
Binary file added public/book/en/v2/images/undomerge-start.png
Binary file added public/book/en/v2/images/vs-1.png
Binary file added public/book/en/v2/images/vs-2.png
Binary file added public/book/en/v2/images/your-profile.png
Binary file added public/book/en/v2/images/zsh-oh-my.png
Binary file added public/book/en/v2/images/zsh-prompt.png

0 comments on commit 6325d8e

Please sign in to comment.