-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* preprocess can download images * preprocess knows about about subdirs (or not) * much more cogitating needed to deal with internal links (#section in html) Github markdown doesn't seem to have an anchor-here. But we want them to create the ebook navigation menus on the sidebar.
- Loading branch information
Cecil
committed
Dec 3, 2016
1 parent
e16ffb5
commit 9d1b819
Showing
6 changed files
with
440 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,89 @@ | ||
Shoes.app { | ||
|
||
require 'yaml' | ||
Shoes.app :width => 800 do | ||
yaml_fl = ARGV[1] | ||
cfg = {} | ||
if yaml_fl | ||
cfg = YAML.load_file(yaml_fl) | ||
else # debugging | ||
cfg['doc_home'] = "/home/ccoupe/Projects/shoes3.wiki/chapter-8" | ||
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 | ||
# create a yaml and .ebook dir | ||
dir = ask_open_folder | ||
@ebook_dir_el.text = dir; | ||
cfg = {} | ||
cfg['doc_home'] = dir | ||
cfg['files'] = [] | ||
cfg['chapters'] = [] | ||
if confirm "make .ebook directory at #{dir}" | ||
Dir.mkdir('.ebook') unless Dir.exist?("#{curdir}/.ebook") | ||
Dir.mkdir(".ebook/images") unless Dir.exist? "#{curdir}/.ebook/images" | ||
# create a bare yaml. | ||
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| | ||
Dir.glob("**/*.md") do |f| | ||
cfg['files'] << f unless File.basename(f) == "_Sidebar.md" | ||
end | ||
end | ||
File.open("#{dir}/.ebook/ebook.yaml", 'w') do |f| | ||
YAML.dump(cfg, f) | ||
end | ||
end | ||
end | ||
button "load" do | ||
# load the yaml file | ||
yf = ask_open_file | ||
button "preprocess" do | ||
require 'kd-pre' | ||
@image_hash = {} | ||
@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:" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
puts "images: #{@image_hash}" | ||
puts "headers: #{@header_hash}" | ||
puts "links: #{@link_hash}" | ||
end | ||
|
||
button "render" do | ||
# parse the yaml | ||
require 'ebook-kd' | ||
doc = Kramdown::Document.new(File.read("/home/ccoupe/Projects/shoes3.wiki/chapter-8/Plot-Widget.md")).to_shoes | ||
rendering(doc) | ||
require 'kd-render' | ||
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) | ||
end | ||
end | ||
end | ||
@ebook_dir_el = edit_line width: 400 | ||
@err_box = edit_box heigth: 300, width: 780 | ||
end | ||
} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
# -*- encoding: utf-8 -*- | ||
# Preprocess for img and links | ||
|
||
require("kramdown") | ||
|
||
module Kramdown | ||
module Converter | ||
class Preprocess < Base | ||
|
||
|
||
def initialize(root, options) | ||
#puts "pre_proc init opts: #{options.inspect}" | ||
if options | ||
@image_hash = options[:img_hash] | ||
@header_hash = options[:hdr_hash] | ||
@link_hash = options[:lnk_hash] | ||
#puts "setting up @image_hash #{@image_hash.inspect}" | ||
end | ||
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) | ||
#puts "hdr: #{el.options[:raw_text]} #{el.options[:level]}" | ||
@header_hash[el.options[:raw_text]] = el.options[:level] | ||
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_smart_quote(el) | ||
#%{para("'", :margin_left => 0, :margin_right => 0)} | ||
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 | ||
#%[para(link("#{results.join}") { open_url("#{el.attr['href']}") }, :margin_left => 0, :margin_right => 0)] | ||
end | ||
|
||
# TODO: syntax highlight not working (no errors - just doesn't return anything) | ||
def convert_codespan(el) | ||
#puts el.type | ||
##puts highlight_code(el.value, el.attr['class'], :span) | ||
##h = ::Kramdown::Converter.syntax_highlighter(@options[:syntax_highlighter]) | ||
##puts h.call(self, el.value, el.attr['class'], :span) | ||
#puts syntax_highlighter(self, el.value, el.attr['class'], :span) | ||
end | ||
|
||
def convert_codeblock(el) | ||
#puts el.type | ||
end | ||
|
||
def convert_strong(el) | ||
#%[para "STRONG"] | ||
end | ||
|
||
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}" | ||
end | ||
|
||
def convert_typographic_sym(el) | ||
#%[para"??"] | ||
end | ||
|
||
def convert_em(el) | ||
#%[para '-'] | ||
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) | ||
return nil unless lexer | ||
|
||
opts[:wrap] = false if type == :span | ||
|
||
formatter = ::Rouge::Formatters::ShoesFormatter.new(opts) | ||
formatter.format(lexer.lex(text)) | ||
end | ||
# TODO: end | ||
end | ||
end | ||
end | ||
|
||
def pre_process(e) | ||
e.kind_of?(Array) ? (e.each { |n| pre_process(n) }) : (eval e unless e.nil?) | ||
return | ||
end | ||
|
||
|
||
|
||
|
Oops, something went wrong.