Module: Asciidoctor::Html::CLI
- Defined in:
- lib/asciidoctor/html/cli.rb
Overview
The command line interface
Constant Summary collapse
- DEFAULT_OPTIONS =
{ "config-file": "config.yml", watch: false }.freeze
- DEFAULT_DIRS =
{ "srcdir" => ".", "outdir" => "www" }.freeze
Class Method Summary collapse
- .generate_bookopts(config) ⇒ Object
- .generate_webmanifest(outdir, name, short_name) ⇒ Object
- .parse_opts ⇒ Object
- .read_config(config_file) ⇒ Object
- .run(opts = nil) ⇒ Object
- .setup_outdir(srcdir, outdir) ⇒ Object
Class Method Details
.generate_bookopts(config) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/asciidoctor/html/cli.rb', line 88 def self.generate_bookopts(config) book_opts = {} %i[title short_title authors base_url chapname].each do |opt| key = opt.to_s book_opts[opt] = config[key] if config.include?(key) end book_opts[:short_title] ||= book_opts[:title] book_opts end |
.generate_webmanifest(outdir, name, short_name) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/asciidoctor/html/cli.rb', line 81 def self.generate_webmanifest(outdir, name, short_name) filename = "#{outdir}/#{FAVICON_PATH}/site.webmanifest" puts "Generating\n #{filename}" puts File.write filename, Webmanifest.generate(name, short_name) end |
.parse_opts ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/asciidoctor/html/cli.rb', line 26 def self.parse_opts = DEFAULT_OPTIONS.dup OptionParser.new do |parser| parser.on("-w", "--watch", "Watch for file changes in SRCDIR. Default: unset") parser.on("-c", "--config-file CONFIG", "Location of config file. Default: #{[:"config-file"]}") parser.on("-v", "--version", "Print version and exit.") end.parse!(into: ) end |
.read_config(config_file) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/asciidoctor/html/cli.rb', line 38 def self.read_config(config_file) begin config = Psych.safe_load_file config_file rescue StandardError puts "Error opening configuration file\n #{config_file}" puts exit 1 end config_dir = Pathname(config_file).dirname %w[outdir srcdir].each do |prop| config[prop] = File.(config[prop] || DEFAULT_DIRS[prop], config_dir) end %w[chapters appendices].each do |prop| config[prop] ||= [] config[prop] = config[prop].map do |f| "#{config["srcdir"]}/#{f}" end end config end |
.run(opts = nil) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/asciidoctor/html/cli.rb', line 98 def self.run(opts = nil) opts ||= parse_opts if opts[:version] puts VERSION return end config = read_config opts[:"config-file"] outdir = config["outdir"] srcdir = config["srcdir"] book_opts = generate_bookopts config setup_outdir srcdir, outdir generate_webmanifest outdir, book_opts[:title], book_opts[:short_title] book = Book.new book_opts puts "Writing book to\n #{outdir}" puts book.write config["chapters"], config["appendices"], outdir, sitemap: true return unless opts[:watch] Filewatcher.new("#{srcdir}/*.adoc").watch do |changes| chapters = [] appendices = [] changes.each_key do |filename| puts "Detected change in\n #{filename}" puts chapters.append(filename) if config["chapters"].include?(filename) appendices.append(filename) if config["appendices"].include?(filename) end puts "Regenerating book:" puts " Chapters: #{chapters.map { |c| Pathname(c).basename }.join ", "}" unless chapters.empty? puts " Appendices: #{appendices.map { |a| Pathname(a).basename }.join ", "}" unless appendices.empty? puts book.write chapters, appendices, config["outdir"] end end |
.setup_outdir(srcdir, outdir) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/asciidoctor/html/cli.rb', line 59 def self.setup_outdir(srcdir, outdir) assets_out = "#{outdir}/#{ASSETS_PATH}" FileUtils.mkdir_p assets_out unless File.directory?(assets_out) %W[#{IMG_PATH} #{CSS_PATH} #{FAVICON_PATH}].each do |p| dir = "#{srcdir}/#{p}" next unless Dir.exist?(dir) puts "Copying\n #{dir}\nto\n #{assets_out}" puts FileUtils.cp_r dir, assets_out end rootdir = File.absolute_path "#{__dir__}/../../.." %W[#{CSS_PATH} #{FAVICON_PATH}].each do |p| dir = "#{outdir}/#{p}" next if Dir.exist?(dir) puts "Putting default '#{p}' files in\n #{dir}" puts FileUtils.cp_r "#{rootdir}/#{p}", assets_out end end |