From 386ad06d2968f966e36f8671e3b1d4cd3b9c1738 Mon Sep 17 00:00:00 2001 From: Cecil Date: Wed, 14 Dec 2016 22:00:03 -0700 Subject: [PATCH] Many things. Still not correct. Subject to change. --- ebook.rb | 148 +++++++++++++++++++++++++++++++++++++-------------- kd-pre.rb | 22 ++++++-- kd-render.rb | 17 ++++++ 3 files changed, 143 insertions(+), 44 deletions(-) diff --git a/ebook.rb b/ebook.rb index 6066025..9b3ad55 100644 --- a/ebook.rb +++ b/ebook.rb @@ -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 @@ -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 @@ -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 diff --git a/kd-pre.rb b/kd-pre.rb index 6e41712..01fd2ac 100644 --- a/kd-pre.rb +++ b/kd-pre.rb @@ -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 @@ -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) @@ -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) @@ -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) diff --git a/kd-render.rb b/kd-render.rb index 9bfb472..dd0e89b 100644 --- a/kd-render.rb +++ b/kd-render.rb @@ -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)