#!/usr/bin/env ruby require 'gli' require 'bookingit' include GLI::App include FileUtils program_desc 'Manage a bookingit book' version Bookingit::VERSION desc 'Set up a new book' arg_name 'dir (defaults to .)' command :init do |c| c.action do |global_options,options,args| dir = args.shift || "." chdir dir do File.open('config.json','w') do |file| file.puts %{ { "front_matter": "front.md", "main_matter": "main.md", "back_matter": "back.md", } } end File.open("front.md",'w') do |file| file.puts "# Intro Goes Here" end File.open("main.md",'w') do |file| file.puts "# Main stuff here" end File.open("back.md",'w') do |file| file.puts "# Appendeces and whatnot" end end end end desc 'build your book from markdown files' arg_name 'config_file output_dir' command :build do |c| c.action do |global_options,options,args| puts pwd config = Bookingit::Config.new(File.read(args.shift),File.expand_path('.')) output_dir = args.shift mkdir output_dir unless Dir.exists?(output_dir) renderer = Bookingit::Renderer.new redcarpet = Redcarpet::Markdown.new(renderer, no_intra_emphasis: true, tables: true, fenced_code_blocks: true, autolink: true, strikethrough: true, superscript: true) chdir output_dir do File.open('index.html','w') do |index| index.puts %{ } index.puts "
    " %w(front_matter main_matter back_matter).each do |matter| index.puts "
  1. #{matter}
      " config.send(matter).chapters.each_with_index do |chapter,i| contents = if chapter.path.nil? chapter.sections.map(&:path).map { |path| File.read(path) }.join("\n") else File.read(chapter.path) end output_file = "#{matter}_#{i+1}.html" File.open(output_file,'w') do |file| file.puts redcarpet.render(contents) end title = (Array(renderer.headers[1]) + Array(renderer.headers[2]) + Array(renderer.headers[3]) + Array(renderer.headers[4]) + Array(renderer.headers[5]) + Array(renderer.headers[6])).first index.puts "
    1. #{title}
    2. " end index.puts "
  2. " end index.puts "
" index.puts %{ } end end end end pre do |global,command,options,args| true end post do |global,command,options,args| end on_error do |exception| true end exit run(ARGV)