From bb4c509ab7d1a658f2484315144b1dda3976fef9 Mon Sep 17 00:00:00 2001 From: Cecil Date: Thu, 8 Dec 2016 23:00:25 -0700 Subject: [PATCH] for #5, 'preprocess' (kd-pre.rb) deal with non trival markdown. * try something hard like a copy of the Shoes wiki * download images in preprocess - not the end of this task * TODO comments highlight what need to been done in render phase. A whole f*cking lot and it might not be do-able. --- ebook.rb | 97 ++++++++++++++++++++++---------- kd-pre.rb | 23 +++++++- kramdown002.rb | 148 ------------------------------------------------- 3 files changed, 88 insertions(+), 180 deletions(-) delete mode 100644 kramdown002.rb diff --git a/ebook.rb b/ebook.rb index afe3930..6066025 100644 --- a/ebook.rb +++ b/ebook.rb @@ -18,21 +18,33 @@ @ebook_dir_el.text = dir; cfg = {} cfg['doc_home'] = dir - cfg['files'] = [] - cfg['chapters'] = [] + #cfg['files'] = [] + #cfg['chapters'] = [] + 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.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| - Dir.glob("**/*.md") do |f| - cfg['files'] << f unless File.basename(f) == "_Sidebar.md" + dirname = File.basename(d) + 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 + # create a new section + cfg['sections'][flds[0]] = + puts "creating new section #{flds[0]}" + dirname = flds[0] + cfg['sections'][dirname] = {dir: dirname, title: dirname, files: []} + end + cfg['sections'][dirname][:files] << flds[-1] unless flds[-1] == '_Sidebar.md' + #cfg['files'] << f unless File.basename(f) == "_Sidebar.md" end end File.open("#{dir}/.ebook/ebook.yaml", 'w') do |f| @@ -42,38 +54,63 @@ end button "preprocess" do require 'kd-pre' - @image_hash = {} + @image_dirs = [] @header_hash = {} @link_hash = {} Dir.chdir(cfg["doc_home"]) do - cfg['files'].each do |relpath| - d = File.dirname(relpath) - f = File.basename(relpath) - Dir.chdir(d) do - # returns an array, not an object - - pre_doc = Kramdown::Document.new(File.read(f), {img_hash: @image_hash, - hdr_hash: @header_hash, lnk_hash: @link_hash}).to_preprocess - end - Dir.chdir(".ebook/images") do - @image_hash.each do |k, v| - if !File.exists?("#{d}/#{v}") - download k, save: "#{d}/#{v}" - @err_box.append("downloaded #{d}/#{v} <- #{k}\n") - break unless confirm "Continue:" + cfg['sections'].keys.each do |section| + #puts "using #{section}" + #puts " #{cfg['sections'][section]}" + #puts " #{cfg['sections'][section][:files]}" + 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 + 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 + end + end end end end end end - puts "images: #{@image_hash}" - puts "headers: #{@header_hash}" - puts "links: #{@link_hash}" + 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 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" } diff --git a/kd-pre.rb b/kd-pre.rb index 999f18a..6e41712 100644 --- a/kd-pre.rb +++ b/kd-pre.rb @@ -6,7 +6,7 @@ module Kramdown module Converter class Preprocess < Base - + attr_accessor :image_hash def initialize(root, options) #puts "pre_proc init opts: #{options.inspect}" @@ -36,6 +36,24 @@ def convert_root(el) def convert_blank(el) #%{para("\n")} 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 + + #end of *this* TODO def convert_text(el) #%{para("#{el.value}", :margin_left => 0, :margin_right => 0)} @@ -77,11 +95,12 @@ def convert_smart_quote(el) end def convert_a(el) - puts "anchor: #{el.inspect}" + #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)] end diff --git a/kramdown002.rb b/kramdown002.rb deleted file mode 100644 index ca5dcbd..0000000 --- a/kramdown002.rb +++ /dev/null @@ -1,148 +0,0 @@ -# -*- encoding: utf-8 -*- -# https://github.com/Shoes3/shoes3/wiki -# http://www.w3schools.com/tags/tag_li.asp - -require("kramdown") - -def open_url(url) - if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ - system("start #{url}") - elsif RbConfig::CONFIG['host_os'] =~ /darwin/ - system("open #{url}") - elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/ - system("xdg-open #{url}") - end -end - -@image_cache = {} - -module Kramdown - module Converter - class Shoes < Base - def initialize(root, options) - super - end - - DISPATCHER = Hash.new {|h,k| h[k] = "convert_#{k}"} - - def convert(el) - send(DISPATCHER[el.type], el) - end - - def convert_root(el) - results = [] - el.children.each do |inner_el| - results << send(DISPATCHER[inner_el.type], inner_el) - end - results - end - - def convert_blank(el) - %{para("\n")} - end - - def convert_text(el) - %{para("#{el.value}", :margin_left => 0, :margin_right => 0)} - end - - def convert_header(el) - %{para(strong("#{el.options[:raw_text]}\n"), :margin_left => 6, :margin_right => gutter)} - end - - def convert_p(el) - results = [] - el.children.each do |inner_el| - results << send(DISPATCHER[inner_el.type], inner_el) - end - %[flow(:margin_left => 6, :margin_right => gutter) { #{results.join(";")} }] - end - - def convert_ul(el) - results = [] - el.children.each do |inner_el| - results << send(DISPATCHER[inner_el.type], inner_el) - end - results - end - - def convert_li(el) - results = [] - el.children.each do |inner_el| - results << %[flow(:margin_left => 30) { fill black; oval -10, 10, 6; #{send(DISPATCHER[inner_el.type], inner_el)} }] - end - results - end - #alias :convert_ol :convert_ul - #alias :convert_dl :convert_ul - - def convert_codeblock(el) - puts el.type - end - - def convert_smart_quote(el) - %{para("'", :margin_left => 0, :margin_right => 0)} - end - - def convert_a(el) - results = [] - el.children.each do |inner_el| - results << inner_el.value if inner_el.type.eql?(:text) - #send(DISPATCHER[inner_el.type], inner_el) - end - %[para(link("#{results.join}") { open_url("#{el.attr['href']}") })] - end - - def convert_strong(el) - %[para "STRONG"] - end - - def convert_codespan(el) - %[para "CODESPAN"] - end - - def convert_img(el) - puts el.attr['src'] - #%[image "#{el.attr['src']}"] # crashes shoes - url = el.attr['src'] - ext = File.extname(url); - %[para "IMAGE_HERE: #{el.attr['alt']}#{ext}"] - end - - def convert_typographic_sym(el) - %[para"??"] - end - - def convert_em(el) - %[para '-'] - end - end - end -end - -def rendering(e) - e.kind_of?(Array) ? (e.each { |n| rendering(n) }) : (eval e unless e.nil?) -end - -# need to get those images downloaded. Find them. Check if we have them -# download if not - -def pre_process(doc) - -end - -Shoes.app { - stack do - curdir = Dir.getwd - puts curdir - #Dir.mkdir('.ebook') unless Dir.exist?("#{curdir}/.ebook") - flow do - button "init ebook" do - end - button "render" do - doc = Kramdown::Document.new(File.read("/home/ccoupe/Projects/shoes3.wiki/chapter-8/Plot-Widget.md")).to_shoes - rendering(doc) - end - end - @ebook_dir_el = edit_line - end -}