Skip to content

Commit

Permalink
for #5 I guess. Make ebook-builder.rb more wizard like, friendly
Browse files Browse the repository at this point in the history
* use @panel.clear {} for all tabs.
* declutter yaml, I hope.
* more Shoes-like GUI
* not complete it any world view. But, it's better.
  • Loading branch information
Cecil committed Dec 19, 2016
1 parent a3231e9 commit bdef3ea
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 170 deletions.
277 changes: 162 additions & 115 deletions ebook-builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,131 +13,170 @@

stack do
flow do
button "init ebook" do
# create a yaml and .ebook dir
dir = ask_open_folder
@ebook_dir_el.text = dir;
cfg = {}
cfg['doc_home'] = dir
cfg['nested'] = false
cfg['input_format'] = 'GFM'
cfg['toc'] = {}
cfg['sections'] = {}

if confirm "make .ebook directory at #{dir}"
Dir.mkdir("#{dir}/.ebook") unless Dir.exist?("#{dir}/.ebook")
Dir.mkdir("#{dir}/.ebook/images") unless Dir.exist? "#{dir}/.ebook/images"
Dir.chdir(cfg['doc_home']) do |d|
dirname = File.basename(d)
#cfg['sections'][dirname] = {dir: dirname, title: dirname, files: []}
Dir.glob("**/*.md") do |f|
flds = f.split('/')
if flds.size <= 1
# special case for one level documents.
if cfg['sections'][dirname] == nil
cfg['sections'][dirname] = {dir: dirname, title: dirname, files: []}
button "1 - init ebook" do
@panel.clear do
cfg = {}
stack do
para "Pick the directory with your document .md files"
para "Save will create and populate and .ebook folder there",
"that you should save at each phase You also need to point",
"to the top level github menu document like Home.md or README.md"
para "You also want to specify a Title"
flow do
para "Book Dir:"
@ebook_dir = edit_line width: 400
button "Select" do
dir = ask_open_folder
@ebook_dir.text = dir
end
flow do
para "Menu Doc: "
@ebook_menu = edit_line width: 400
button "Select" do
@ebook_menu.text = ask_open_file
end
end
flow do
para "Title "
@ebook_title = edit_line width: 400
end
button "Save" do
if @ebook_dir.text == nil || @ebook_title == nil || @ebook_menu == nil
alert "You are missing something!"
else
dir = cfg['doc_home'] = @ebook_dir.text
cfg['nested'] = false
cfg['input_format'] = 'GFM'
cfg['book_title'] = ""
cfg['toc'] = {}
cfg['sections'] = {}
Dir.mkdir("#{dir}/.ebook") unless Dir.exist?("#{dir}/.ebook")
Dir.mkdir("#{dir}/.ebook/images") unless Dir.exist? "#{dir}/.ebook/images"
Dir.chdir(cfg['doc_home']) do |d|
dirname = File.basename(d)
#cfg['sections'][dirname] = {dir: dirname, title: dirname, files: []}
Dir.glob("**/*.md") do |f|
flds = f.split('/')
if flds.size <= 1
# special case for one level documents.
if cfg['sections'][dirname] == nil
cfg['sections'][dirname] = {dir: dirname, title: dirname, files: []}
end
fa = cfg['sections'][dirname][:files]
fa << f unless f == '_Sidebar.md'
#puts "Special case: #{fa} in #{dirname}"
elsif flds.size > 1 && cfg['sections'][flds[0]] == nil
# create a new section
cfg['sections'][flds[0]] = flds[0]
#puts "creating new section #{flds[0]}"
dirname = flds[0]
cfg['sections'][dirname] = {dir: dirname, title: dirname, files: []}
cfg['sections'][dirname][:files] << flds[-1] unless flds[-1] == '_Sidebar.md'
else
cfg['sections'][dirname][:files] << flds[-1] unless flds[-1] == '_Sidebar.md'
end
end
end
cfg['toc']['root'] = File.basename(@ebook_menu.text)
cfg['toc']['files'] = [] # TODO: may not need
cfg['nested'] = true if cfg['sections'].size > 1
cfg['book_title'] = @ebook_title.text

# clean up on aisle 10 - remove toc document
tocfn = cfg['toc']['root']
#puts "cleaning find #{tocfn}"
cfg['sections'].each do |sect, sect_hsh|
fa = sect_hsh[:files]
#puts "clean #{tocfn} from #{fa}"
fa.delete_if {|x| x == tocfn }
end
File.open("#{dir}/.ebook/ebook.yaml", 'w') do |f|
YAML.dump(cfg, f)
end
end
fa = cfg['sections'][dirname][:files]
fa << f unless f == '_Sidebar.md'
#puts "Special case: #{fa} in #{dirname}"
elsif flds.size > 1 && cfg['sections'][flds[0]] == nil
# create a new section
cfg['sections'][flds[0]] = flds[0]
#puts "creating new section #{flds[0]}"
dirname = flds[0]
cfg['sections'][dirname] = {dir: dirname, title: dirname, files: []}
cfg['sections'][dirname][:files] << flds[-1] unless flds[-1] == '_Sidebar.md'
else
cfg['sections'][dirname][:files] << flds[-1] unless flds[-1] == '_Sidebar.md'
end
end
end
if confirm("You need to the select the Table of Contents document")
toc = ask_open_file
cfg['toc']['root'] = File.basename(toc)
cfg['toc']['files'] = []
end
cfg['nested'] = true if cfg['sections'].size > 1
# clean up on aisle 10 - remove toc document
tocfn = cfg['toc']['root']
#puts "cleaning find #{tocfn}"
cfg['sections'].each do |sect, sect_hsh|
fa = sect_hsh[:files]
#puts "clean #{tocfn} from #{fa}"
fa.delete_if {|x| x == tocfn }
end
File.open("#{dir}/.ebook/ebook.yaml", 'w') do |f|
YAML.dump(cfg, f)
end
end
end


button "preprocess" do
button "2 - preprocess" do
require 'kd-pre'
@image_dirs = []
@header_hash = {}
@link_hash = {}
@menu_list = []
Dir.chdir(cfg["doc_home"]) do
cfg['sections'].keys.each do |section|
puts "using #{section}"
#puts " #{cfg['sections'][section]}"
#puts " #{cfg['sections'][section][:files]}"
@image_hash = {}
cfg['sections'][section][:files].each do |fname|
relpath = "#{cfg['sections'][section][:dir]}/#{fname}"
#puts "In dir #{relpath}"
d = File.dirname(relpath)
f = File.basename(relpath)
#puts "relpath d: #{d}, f: #{f}"
# find all the images, headers, urls(links)
# Grr - special case a flat document directory
if cfg['nested']
Dir.chdir(d) do
# pre_doc is an array
pre_doc = Kramdown::Document.new(File.read(f, encoding: "UTF-8"),
{img_hash: @image_hash, hdr_hash: @header_hash, lnk_hash: @link_hash,
menu_list: @menu_list, input: cfg['input_format']
}).to_preprocess
end
else
pre_doc = Kramdown::Document.new(File.read(f, encoding: "UTF-8"),
{img_hash: @image_hash, hdr_hash: @header_hash, lnk_hash: @link_hash,
menu_list: @menu_list, input: cfg['input_format']
}).to_preprocess
end
Dir.chdir(".ebook/images") do
here = Dir.getwd
@image_hash.each do |k, v|
if !File.exists?("#{here}/#{d}/#{v}")
if confirm "Download to #{here}/#{d}/#{v}"
Dir.mkdir(d) if !Dir.exists?(d)
download k, save: "#{d}/#{v}"
@err_box.append("downloaded #{d}/#{v} <- #{k}\n")
else
break # need break out of the outer loop!
@panel.clear do
para "This phase does a deeper dive into your .md documents and downloads any ",
"images from website that you have not downloaded before. It is safe to 'download' ",
"as many times as you like. Downloads will be shown below. 'Save' when ready to move on."
flow do
button "download" do
@image_dirs = []
@header_hash = {}
@link_hash = {}
@menu_list = []
Dir.chdir(cfg["doc_home"]) do
cfg['sections'].keys.each do |section|
puts "using #{section}"
#puts " #{cfg['sections'][section]}"
#puts " #{cfg['sections'][section][:files]}"
@image_hash = {}
cfg['sections'][section][:files].each do |fname|
relpath = "#{cfg['sections'][section][:dir]}/#{fname}"
#puts "In dir #{relpath}"
d = File.dirname(relpath)
f = File.basename(relpath)
#puts "relpath d: #{d}, f: #{f}"
# find all the images, headers, urls(links)
# Grr - special case a flat document directory
if cfg['nested']
Dir.chdir(d) do
# pre_doc is an array
pre_doc = Kramdown::Document.new(File.read(f, encoding: "UTF-8"),
{img_hash: @image_hash, hdr_hash: @header_hash, lnk_hash: @link_hash,
menu_list: @menu_list, input: cfg['input_format']
}).to_preprocess
end
else
pre_doc = Kramdown::Document.new(File.read(f, encoding: "UTF-8"),
{img_hash: @image_hash, hdr_hash: @header_hash, lnk_hash: @link_hash,
menu_list: @menu_list, input: cfg['input_format']
}).to_preprocess
end
Dir.chdir(".ebook/images") do
here = Dir.getwd
@image_hash.each do |k, v|
next if File.exists?("#{here}/#{d}/#{v}")
if confirm "Download to #{here}/#{d}/#{v}"
Dir.mkdir(d) if !Dir.exists?(d)
download k, save: "#{d}/#{v}"
@err_box.append("downloaded #{d}/#{v} <- #{k}\n")
end
end
end
end
foo = cfg['sections'][section]['images'] = @image_hash
#puts "images: #{foo.inspect}"
end
end
end

button "Save" do
# follow the TOC document and all the parts it points to
#puts "menu_list: #{@menu_list.uniq.sort}"
#cfg['toc']['files'] = @menu_list.uniq
#cfg['images'] = @image_hash
cfg['headers'] = @header_hash
cfg['links'] = @link_hash
# rewrite the ebook.yaml
File.open("#{cfg['doc_home']}/.ebook/ebook.yaml", 'w') do |f|
YAML.dump(cfg, f)
end
end
end
foo = cfg['sections'][section]['images'] = @image_hash
#puts "images: #{foo.inspect}"
end
end
# follow the TOC document and all the parts it points to
puts "menu_list: #{@menu_list.uniq.sort}"
cfg['toc']['files'] = @menu_list.uniq
#cfg['images'] = @image_hash
cfg['headers'] = @header_hash
cfg['links'] = @link_hash
# rewrite the ebook.yaml
File.open("#{cfg['doc_home']}/.ebook/ebook.yaml", 'w') do |f|
YAML.dump(cfg, f)
end
@err_box = edit_box height: 300, width: 680
end

button "order doc" do
button "3- order sections" do
@panel.clear do
el_v = []
el_t = []
Expand Down Expand Up @@ -176,7 +215,7 @@
end
end

button "order sections" do
button "4 -order files" do
@panel.clear do
para "Select section to view the documents. Order them from 1. 0 means delete"
para "Important: Save after modifing each section before selecting the next!"
Expand All @@ -203,7 +242,11 @@
item = lb.text
@chapter.clear do
sect = cfg['sections'][item]
files = sect[:files]
if sect[:display_order]
files = sect[:display_order]
else
files = sect[:files]
end
files.each_index do |i|
flow do
eln = edit_line text: "#{i+1}", width: 30
Expand All @@ -219,7 +262,7 @@
end
end

button "render" do
button "5 - test render" do
# This is used to testing the yaml & rendering. It creates a
# Shoes window which does the rendering using help_ebook.rb (a Shoes module)
# copy the current yaml to 'shoes_ebook.yaml' it what ever dir
Expand All @@ -231,11 +274,15 @@
require 'help_ebook'
window(:width => 720, :height => 640, &Shoes.make_ebook("The Shoes eBook"))
end

button "6 - Create an app" do
@panel.clear do
para "Create your \"#{cfg['book_title']}\" ebook for #{RUBY_PLATFORM}. May gods have mercy on you!"
button "Package" do
end
end
end
end
@panel = stack do
@ebook_dir_el = edit_line width: 400
@err_box = edit_box heigth: 300, width: 780
end
end
end
File renamed without changes.
2 changes: 1 addition & 1 deletion help_picky.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def Shoes.make_help_page(book_title = "The Shoes Manual")
proc do
extend Shoes::Manual
docs = load_docs Shoes::Manual.path

puts docs.inspect
style(Shoes::Image, :margin => 8, :margin_left => 100)
style(Shoes::Code, :stroke => "#C30")
style(Shoes::LinkHover, :stroke => green, :fill => nil)
Expand Down
2 changes: 1 addition & 1 deletion kramdown001.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def rendering(e)
end

Shoes.app {
doc = Kramdown::Document.new(File.read("manual-en.txt"), { :syntax_highlighter => "rouge", :syntax_highlighter_opts => { css_class: false, line_numbers: false, inline_theme: "github" } }).to_shoes
doc = Kramdown::Document.new(File.read("fts-md/manual-en.txt"), { :syntax_highlighter => "rouge", :syntax_highlighter_opts => { css_class: false, line_numbers: false, inline_theme: "github" } }).to_shoes

info doc.inspect
rendering(doc)
Expand Down
Loading

0 comments on commit bdef3ea

Please sign in to comment.