Skip to content

Commit

Permalink
progress on several issues #17, #8, #7, #18
Browse files Browse the repository at this point in the history
* image hash is not section specific, it's for the whole document
  -- thats a github rule.
* navigation documents can have images.
* start moving some duplicate code into methods.
* create interim yaml-n files. Not wizardy yet - good for debugging though.
  • Loading branch information
Cecil committed Jan 11, 2017
1 parent 97f6994 commit 0a84582
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 29 deletions.
82 changes: 57 additions & 25 deletions ebook-builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'yaml'
require 'kramdown'
require 'gfmlink'
require 'fileutils'
include FileUtils
Shoes.app :width => 800 do
yaml_fl = ARGV[1]
#puts "yaml file #{yaml_fl}"
Expand Down Expand Up @@ -65,6 +67,7 @@
cfg['syntax_highlight'] = true # true until render_code is working
cfg['toc'] = {}
cfg['sections'] = {}
cfg['images'] = {}
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|
Expand Down Expand Up @@ -111,6 +114,7 @@
File.open("#{dir}/.ebook/ebook.yaml", 'w') do |f|
YAML.dump(cfg, f)
end
cp "#{dir}/.ebook/ebook.yaml", "#{dir}/.ebook/ebook-1.yaml"
end
end
end
Expand All @@ -128,7 +132,7 @@
"as many times as you like. Downloads will be shown below. 'Save' when ready to move on."
flow do
button "Proceed" do
@image_dirs = []
@image_hash = {}
@header_hash = {}
@link_hash = {}
@menu_list = []
Expand All @@ -144,9 +148,6 @@
#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
Expand All @@ -161,41 +162,44 @@
menu_list: @menu_list, input: cfg['input_format']
}).to_preprocess
end
download_images(cfg, @image_hash)
# note all images are in one dir (it's the github way)
Dir.chdir(".ebook/images") do
here = Dir.getwd
@image_hash.each do |k, v|
next if File.exists?("#{here}/#{v}")
if confirm "Download to #{here}/#{v}"
download k, save: "#{here}/#{v}"
@err_box.append("downloaded #{here}/#{v} <- #{k}\n")
end
end
end
#Dir.chdir(".ebook/images") do
# here = Dir.getwd
# @image_hash.each do |k, v|
# next if File.exists?("#{here}/#{v}")
# if confirm "Download to #{here}/#{v}"
# download k, save: "#{here}/#{v}"
# @err_box.append("downloaded #{here}/#{v} <- #{k}\n")
# cfg['images'][k] = v
# end
# end
#end
end
foo = cfg['sections'][section]['images'] = @image_hash

#cfg['sections'][section]['images'] = @image_hash
foo = cfg['sections'][section]['headers'] = @header_hash
end

# Process the toc/menu documents if available and github nested
if cfg['toc']['root'] && cfg['have_nav']
@menu_list = []
@img_hash = {} # it is possible that a nav doc has images
Dir.chdir(cfg['doc_home']) do |p|
f = "#{p}/#{cfg['toc']['root']}"
puts "process toc #{f}"
pre_toc = Kramdown::Document.new(File.read(f, encoding: "UTF-8"),
{ menu_list: @menu_list, input: cfg['input_format']
{ img_hash: @img_hash, menu_list: @menu_list, input: cfg['input_format']
}).to_menuparse
puts "first level #{@menu_list}"
# Getting tricksy and clumsy. Stumble or Dance?
#puts "first level #{@menu_list}"
cfg['toc']['section_order'] = []
cfg['toc']['files'] = []
@menu_list.each do |md|
cfg['sections'].each do |sect_k, sect_v|
sect_files = cfg['sections'][sect_k][:files]
pos = sect_files.find_index(md)
if pos
#puts "Found #{md} in #{sect_k}"
puts "Found #{md} in #{sect_k}"
cfg['toc']['section_order'] << sect_k
cfg['toc']['files'] << md
sect_files.delete_at(pos)
Expand All @@ -207,9 +211,10 @@
sect = cfg['sections'][d]
f = cfg['toc']['files'][i]
@menu_list = []
@img_hash = {}
@err_box.append "toc process #{d}/#{f}\n"
pre_toc = Kramdown::Document.new(File.read("#{d}/#{f}", encoding: "UTF-8"),
{ menu_list: @menu_list, input: cfg['input_format']
{ img_hash: @image_hash, menu_list: @menu_list, input: cfg['input_format']
}).to_menuparse
cfg['sections'][d][:display_order] = []
@menu_list.each do |md|
Expand All @@ -222,12 +227,13 @@
end
end
end
# download images and set cfg[images]
download_images(cfg, @img_hash)
@err_box.append("Done - You can save if you want\n")
end
else
# TODO: we need to do better perhaps set section_order
@err_box.append("Done - You can save if you want\n")
puts "no toc to deal with, faking it"
puts "no toc to deal with"
end
end
end
Expand All @@ -237,6 +243,7 @@
File.open("#{cfg['doc_home']}/.ebook/ebook.yaml", 'w') do |f|
YAML.dump(cfg, f)
end
cp "#{cfg['doc_home']}/.ebook/ebook.yaml", "#{cfg['doc_home']}/.ebook/ebook-2.yaml"
end
end
@err_box = edit_box height: 300, width: 680
Expand All @@ -251,10 +258,10 @@
para "Save will write to #{cfg['doc_home']}/.ebook/ebook.yaml"
para "Important: Save before you move to other options."
flow do
button "cancel" do
button "Quit" do
Shoes.quit
end
button "save" do
button "Save" do
cfg['toc']['section_order'] = []
# TODO: Magic occurs
el_v.each_index do |i|
Expand All @@ -263,11 +270,14 @@
cfg['toc']['section_order'][ord-1] = el_t[i].text
end
end
# TODO: Ick - clear out anything
File.open("#{cfg['doc_home']}/.ebook/ebook.yaml", 'w') do |f|
YAML.dump(cfg, f)
end
cp "#{cfg['doc_home']}/.ebook/ebook.yaml", "#{cfg['doc_home']}/.ebook/ebook-3.yaml"
end
end

section_stack = stack do
section_names = cfg['sections'].keys.each do |pos|
flow do
Expand All @@ -293,6 +303,7 @@
button "Quit" do
Shoes.quit
end

button "Save" do
sect[:display_order] = []
el_v.each_index do |i|
Expand All @@ -304,12 +315,14 @@
File.open("#{cfg['doc_home']}/.ebook/ebook.yaml", 'w') do |f|
YAML.dump(cfg, f)
end
cp "#{cfg['doc_home']}/.ebook/ebook.yaml", "#{cfg['doc_home']}/.ebook/ebook-4.yaml"
end

list_box items: cfg['toc']['section_order'] do |lb|
item = lb.text
@chapter.clear do
sect = cfg['sections'][item]
if sect[:display_order]
if sect[:display_order] && (! sect[:display_order].empty?)
files = sect[:display_order]
else
files = sect[:files]
Expand Down Expand Up @@ -353,4 +366,23 @@
@panel = stack do
end
end

# utility methods

def download_images(cfg, img_hsh)
# note all images are in one dir (it's the github way)
where = "#{cfg['doc_home']}/.ebook/images"
mkdir_p where
Dir.chdir(where) do
here = Dir.getwd
img_hsh.each do |k, v|
next if File.exists?("#{here}/#{v}")
if confirm "Download to #{here}/#{v}"
download k, save: "#{here}/#{v}"
@err_box.append("downloaded #{here}/#{v} <- #{k}\n")
cfg['images'][k] = v
end
end
end
end
end
3 changes: 2 additions & 1 deletion kd-render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ def convert_img(el)
url = el.attr['src']
ext = File.extname(url);
#%[para "IMAGE_HERE: #{@cfg['doc_home']}/#{el.attr['alt']}#{ext}"]
lcl = @cfg['sections'][@chapter]['images'][url]
#lcl = @cfg['sections'][@chapter]['images'][url]
lcl = @cfg['images'][url]
if lcl
#puts "found local image #{lcl}"
%[image "#{@cfg['doc_home']}/.ebook/images/#{lcl}"]
Expand Down
2 changes: 1 addition & 1 deletion kd-toc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def convert_img(el)
hsh = @image_hash
fname = "#{el.attr['alt']}#{ext}"
fname.gsub!(' ', '-');
#puts "alt tag: #{fname}"
puts "alt tag: #{fname}"
if !fname || fname[0] == '.' # start of extension
# someone didn't put an alt tag like they were supposed to do.
fname = File.basename(url)
Expand Down
4 changes: 2 additions & 2 deletions show_ebook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Shoes::Ebook
require 'search_picky'
def render_file (cfg, sect_nm, dir, file)
puts "parse: #{dir} #{file}"
render_doc = Kramdown::Document.new(File.read(File.join(dir, file)),
render_doc = Kramdown::Document.new(File.read(File.join(dir, file), encoding: "UTF-8"),
{ :syntax_highlighter => "rouge",
:syntax_highlighter_opts => { css_class: false, line_numbers: false, inline_theme: "github" },
cfg: cfg, chapter: sect_nm, input: cfg['input_format'], hard_wrap: false,
Expand Down Expand Up @@ -68,7 +68,7 @@ def load_docs(cfg)
#puts "going into #{sect_name}"
sect = cfg['sections'][sect_name]
sect[:display_order].each do |fl|
#puts "render #{cfg['doc_home']}/#{cfg[sect]}/#{sect_name}/#{fl}"
puts "render #{cfg['doc_home']}/#{cfg[sect]}/#{sect_name}/#{fl}"
contents = render_file(cfg, sect_name, "#{cfg['doc_home']}/#{sect_name}", fl)
landing = {title: fl, code: contents}
cfg['code_struct'] << landing
Expand Down

0 comments on commit 0a84582

Please sign in to comment.