Skip to content

Commit

Permalink
for #5, 'preprocess' (kd-pre.rb) deal with non trival markdown.
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Cecil committed Dec 9, 2016
1 parent c4d86ac commit bb4c509
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 180 deletions.
97 changes: 67 additions & 30 deletions ebook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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" }
Expand Down
23 changes: 21 additions & 2 deletions kd-pre.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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

Expand Down
148 changes: 0 additions & 148 deletions kramdown002.rb

This file was deleted.

0 comments on commit bb4c509

Please sign in to comment.