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



86
87
88
89
90
91
92
93
94
# File 'lib/asciidoctor/html/cli.rb', line 86

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



79
80
81
82
83
84
# File 'lib/asciidoctor/html/cli.rb', line 79

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_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
55
# 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\n  #{config_file}"
    puts
    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



96
97
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
# File 'lib/asciidoctor/html/cli.rb', line 96

def self.run(opts = nil)
  opts ||= parse_opts
  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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/asciidoctor/html/cli.rb', line 57

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