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

.build_jupyterlite(rootdir, outdir) ⇒ Object



101
102
103
104
105
106
# File 'lib/asciidoctor/html/cli.rb', line 101

def self.build_jupyterlite(rootdir, outdir)
  jupyterlite_outdir = "#{outdir}/#{Jupyterlite::PATH}"
  return if File.directory?(jupyterlite_outdir)

  system %(#{rootdir}/exe/build-jupyterlite -o "#{jupyterlite_outdir}" "#{rootdir}/#{Jupyterlite::PATH}")
end

.generate_bookopts(config) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/asciidoctor/html/cli.rb', line 91

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



84
85
86
87
88
89
# File 'lib/asciidoctor/html/cli.rb', line 84

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



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

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"]}")
    parser.on("-v", "--version", "Print version and exit.")
  end.parse!(into: options)
  options
end

.read_config(config_file) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/asciidoctor/html/cli.rb', line 39

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|
      "#{config["srcdir"]}/#{f}"
    end
  end
  config["jupyterlite"] = config["jupyterlite"] || false
  config
end

.run(opts = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/asciidoctor/html/cli.rb', line 108

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, jupyterlite: config["jupyterlite"]
  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, jupyterlite: false) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/asciidoctor/html/cli.rb', line 61

def self.setup_outdir(srcdir, outdir, jupyterlite: false)
  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
  build_jupyterlite(rootdir, outdir) if jupyterlite
end