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

Class Method Details

.generate_bookopts(config) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/asciidoctor/html/cli.rb', line 75

def self.generate_bookopts(config)
  book_opts = {}
  %i[title short_title author date se_id 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



69
70
71
72
73
# File 'lib/asciidoctor/html/cli.rb', line 69

def self.generate_webmanifest(outdir, name, short_name)
  filename = "#{outdir}/#{FAVICON_PATH}/site.webmanifest"
  puts "Generating #{filename}"
  File.write filename, Webmanifest.generate(name, short_name)
end

.parse_optsObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/asciidoctor/html/cli.rb', line 25

def self.parse_opts
  options = 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: #{options[:"config-file"]}")
  end.parse!(into: options)
  options
end

.read_config(config_file) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/asciidoctor/html/cli.rb', line 36

def self.read_config(config_file)
  begin
    config = Psych.safe_load_file config_file
  rescue StandardError
    puts "Error opening configuration file #{config_file}"
    exit 1
  end
  config_dir = Pathname(config_file).dirname
  %w[outdir srcdir].each do |prop|
    config[prop] = File.expand_path(config[prop] || DEFAULT_DIRS[prop], config_dir)
  end
  %w[chapters appendices].each do |prop|
    config[prop] ||= []
    config[prop] = config[prop].map do |f|
      File.expand_path(f, config_dir)
    end
  end
  config
end

.run(opts = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/asciidoctor/html/cli.rb', line 85

def self.run(opts = nil)
  opts ||= parse_opts
  config = read_config opts[:"config-file"]
  outdir = config["outdir"]
  book_opts = generate_bookopts config
  setup_outdir outdir
  generate_webmanifest outdir, book_opts[:title], book_opts[:short_title]
  book = Book.new book_opts
  puts "Writing book to #{outdir}"
  book.write config["chapters"], config["appendices"], config["outdir"]
  return unless opts[:watch]

  Filewatcher.new("#{config["srcdir"]}/*.adoc").watch do |changes|
    chapters = []
    appendices = []
    changes.each_key do |filename|
      puts "Detected change in #{filename}"
      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?
    book.write chapters, appendices, config["outdir"]
  end
end

.setup_outdir(outdir) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/asciidoctor/html/cli.rb', line 56

def self.setup_outdir(outdir)
  assets_dir = "#{outdir}/#{ASSETS_PATH}"
  FileUtils.mkdir_p assets_dir unless File.directory?(assets_dir)
  rootdir = File.absolute_path "#{__dir__}/../../.."
  %W[#{CSS_PATH} #{FAVICON_PATH}].each do |p|
    dir = "#{outdir}/#{p}"
    next if Dir.exist?(dir)

    puts "Generating #{dir}"
    FileUtils.cp_r "#{rootdir}/#{p}", assets_dir
  end
end