Skip to content

Commit

Permalink
Many things. Still not correct. Subject to change.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cecil committed Dec 15, 2016
1 parent bb4c509 commit 386ad06
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 44 deletions.
148 changes: 108 additions & 40 deletions ebook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
cfg['files'] = ["Plot-Widget.md"]
#@doc = "/home/ccoupe/Projects/shoes3.wiki/chapter-8/Plot-Widget.md"
end

stack do
flow do
button "init ebook" do
Expand All @@ -18,61 +19,92 @@
@ebook_dir_el.text = dir;
cfg = {}
cfg['doc_home'] = dir
#cfg['files'] = []
#cfg['chapters'] = []
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.entries(dir).each do |e|
# next if e[0] == '.'
# #puts e
# if File.directory?("#{dir}/#{e}")
# cfg['chapters'] << e
# end
#end
Dir.chdir(cfg['doc_home']) do |d|
dirname = File.basename(d)
cfg['sections'][dirname] = {dir: dirname, title: dirname, files: []}
Dir.glob("*/*.md") do |f|
#cfg['sections'][dirname] = {dir: dirname, title: dirname, files: []}
Dir.glob("**/*.md") do |f|
flds = f.split('/')
if flds.size > 1 && cfg['sections'][flds[0]] == nil
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]] =
puts "creating new section #{flds[0]}"
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
cfg['sections'][dirname][:files] << flds[-1] unless flds[-1] == '_Sidebar.md'
#cfg['files'] << f unless File.basename(f) == "_Sidebar.md"
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
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 "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)
@image_hash = {}
# find all the images
Dir.chdir(d) do
# returns an array, not an object -
pre_doc = Kramdown::Document.new(File.read(f, encoding: "UTF-8"), {img_hash: @image_hash,
hdr_hash: @header_hash, lnk_hash: @link_hash}).to_preprocess
#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
Expand All @@ -82,42 +114,78 @@
Dir.mkdir(d) if !Dir.exists?(d)
download k, save: "#{d}/#{v}"
@err_box.append("downloaded #{d}/#{v} <- #{k}\n")

else
break
break # need break out of the outer loop!
end
end
end
end
end
foo = cfg['sections'][section]['images'] = @image_hash
#puts "images: #{foo.inspect}"
end
end
cfg['images'] = @image_hash
# follow the TOC document and all the parts it has
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
#puts "images: #{@image_hash}"
#puts "headers: #{@header_hash}"
#puts "links: #{@link_hash}"
end


button "render" do
# this should just require the file that will be the ebook.rb copied/called
# place of Shoes.rb in the end app. Not anywhere near that.
# needs to be guided by the toc order (which we don't have yet)
require 'kd-render'
if cfg['chapters'] == nil

end
cfg['files'].each do |relpath|

render_doc = Kramdown::Document.new(File.read(@doc),
{ :syntax_highlighter => "rouge",
:syntax_highlighter_opts => { css_class: false, line_numbers: false, inline_theme: "github" }
}
).to_shoes
rendering(render_doc)
if cfg['nested']
cfg['sections'].each do |section, section_hash|
section_hash[:files].each do |fl|
#puts "render document #{fl}"
#para "document #{fl}"
docpath = File.join(cfg['doc_home'], section_hash[:dir], fl)
puts "deep render #{docpath}"
render_doc = Kramdown::Document.new(File.read(docpath),
{ :syntax_highlighter => "rouge",
:syntax_highlighter_opts => { css_class: false, line_numbers: false, inline_theme: "github" },
input: cfg['input_format']
}
).to_shoes
rendering(render_doc)
end
end
else
# flat dir of md
cfg['sections'].each do |section, section_hash|
section_hash[:files].each do |fl|
#puts "render document #{fl}"
#para "document #{fl}"
docpath = File.join(cfg['doc_home'], fl)
puts "flat render #{docpath}"
render_doc = Kramdown::Document.new(File.read(docpath),
{ :syntax_highlighter => "rouge",
:syntax_highlighter_opts => { css_class: false, line_numbers: false, inline_theme: "github" },
input: 'GFM', gfm_quirks: ['hard_wrap'],
}
).to_shoes
rendering(render_doc)
end
end
end
#cfg['files'].each do |relpath|
# render_doc = Kramdown::Document.new(File.read(@doc),
# { :syntax_highlighter => "rouge",
# :syntax_highlighter_opts => { css_class: false, line_numbers: false, inline_theme: "github" },
# input: 'GFM'
# }
# ).to_shoes
# rendering(render_doc)
#end
end
end
@ebook_dir_el = edit_line width: 400
Expand Down
22 changes: 18 additions & 4 deletions kd-pre.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def initialize(root, options)
@image_hash = options[:img_hash]
@header_hash = options[:hdr_hash]
@link_hash = options[:lnk_hash]
@menu_list = options[:menu_list]
#@section = options[:chapter]
#puts "setting up @image_hash #{@image_hash.inspect}"
end
super
Expand Down Expand Up @@ -57,6 +59,12 @@ def convert_html_element(el)

def convert_text(el)
#%{para("#{el.value}", :margin_left => 0, :margin_right => 0)}
dblbracket = el.value[/\[\[(.*)\]\]/]
if dblbracket
menu = dblbracket[2..-3].gsub(' ', '-')
@menu_list << "#{menu}.md"
puts "LINK: #{menu}"
end
end

def convert_header(el)
Expand Down Expand Up @@ -95,13 +103,12 @@ def convert_smart_quote(el)
end

def convert_a(el)
#puts "anchor: #{el.inspect}"
results = []
el.children.each do |inner_el|
results << inner_el.value if inner_el.type.eql?(:text)
end
@link_hash[results.join] = el.attr['href']
#%[para(link("#{results.join}") { open_url("#{el.attr['href']}") }, :margin_left => 0, :margin_right => 0)]
return nil
end

# TODO: syntax highlight not working (no errors - just doesn't return anything)
Expand All @@ -125,8 +132,15 @@ def convert_img(el)
url = el.attr['src']
ext = File.extname(url);
hsh = @image_hash
#puts "#{el.attr['src']} -> #{el.attr['alt']}#{ext} for #{hsh}"
hsh[url] = "#{el.attr['alt']}#{ext}"
fname = "#{el.attr['alt']}#{ext}"
fname.gsub!(' ', '-');
#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)
#puts "missing alt - using #{fname}"
end
hsh[url] = fname
end

def convert_typographic_sym(el)
Expand Down
17 changes: 17 additions & 0 deletions kd-render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ def convert_em(el)
%[para '-']
end

# TODO: fix these in kd-render
def convert_br(el)
end

def convert_blockquote(el)
end

def convert_table(el)
end

def convert_ol(el)
end

def convert_html_element(el)
end


def syntax_highlighter(converter, text, lang, type)
opts = converter.options[:syntax_highlighter_opts].dup
lexer = ::Rouge::Lexer.find_fancy(lang || opts[:default_lang], text)
Expand Down

2 comments on commit 386ad06

@IanTrudel
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is ebook.rb asking for a TOC filename? I would imagine ebook.rb is just a reader, not building anything. Why wouldn't we have a publish.rb to do those tasks and then ebook.rb is just the reader?

@ccoupe
Copy link
Member

@ccoupe ccoupe commented on 386ad06 Dec 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps ebook.rb should be named ebook-builder or ebook-publisher since it's purpose is to guide the 'author' into to creating and modifying the .yaml . As a GUI, yaml builder, it need to know which documents are 'naviigation' (those [[fun things]] ) and which are Content. Don't view the ebook requirements from the Shoe Manual and your proof on concept. Markdown and kramdown are much broader that what the Shoes Manual uses.

Since the important script is ebook_help.rb using a proper shoes_ebook.yam, l it really doesn't matter what the GUI builder script names are since anyone that gets that deep in the weeds would be writing Ruby code to fix things and we know how many are willing to do that. If you run the ebook code you'd learn you only have to suffer the ask_file dialog once.

Please sign in to comment.