Class: Guard::AsciiDoc

Inherits:
Plugin
  • Object
show all
Defined in:
lib/guard/asciidoc.rb

Constant Summary collapse

BACKEND_ALIASES =
{ 'html' => 'html5', 'docbook' => 'docbook5' }
DEFAULT_BACKEND =
'html5'
DEFAULT_OPTIONS =
{
  watch_dir: '.',
  watch_ext: %w(adoc),
  watch_hidden: false,
  run_on_start: false,
  always_build_all: false,
}
FILE_SEPARATOR =
::File::SEPARATOR
PLUGIN_KEYS =
[:any_return, :group, :watchers]
CONFIG_KEYS =
DEFAULT_OPTIONS.keys

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AsciiDoc

Returns a new instance of AsciiDoc.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/guard/asciidoc.rb', line 23

def initialize options = {}
  compiled_opts = DEFAULT_OPTIONS.merge
  compiled_opts[:watch_dir] = (options.delete :watch_dir) || '.' if options.key? :watch_dir
  compiled_opts.merge! options
  compiled_opts[:watch_dir] = '.' if compiled_opts[:watch_dir].to_s.empty?
  @asciidoc_opts = compiled_opts.except(*(PLUGIN_KEYS + CONFIG_KEYS))
  unless (@asciidoc_opts[:backend] = BACKEND_ALIASES[(backend = @asciidoc_opts[:backend])] || backend)
    @asciidoc_opts[:backend] = DEFAULT_BACKEND
  end
  case (attributes = @asciidoc_opts[:attributes] ||= {})
  when ::Hash
    attributes['env-guard'] = ''
  when ::Array
    attributes << 'env-guard'
  when ::String
    @asciidoc_opts[:attributes] += ' env-guard'
  end
  @asciidoc_opts[:safe] ||= :unsafe
  if (compiled_opts[:watchers] ||= []).empty? && (watch_dir = compiled_opts[:watch_dir])
    watch_dir = compiled_opts[:watch_dir] = (::Pathname.new watch_dir).cleanpath.to_s
    watch_re = watch_dir == '.' ? '' : %(^#{::Regexp.escape watch_dir}(?=#{FILE_SEPARATOR}))
    watch_re += compiled_opts[:watch_hidden] ? '.+?' : %((?:[^#{FILE_SEPARATOR}]|#{FILE_SEPARATOR}[^.])+?)
    watch_re += %((?:#{compiled_opts[:watch_ext].join '|'})$)
    watch_rx = ::Regexp.new watch_re
    compiled_opts[:watchers] = [(Watcher.new watch_rx)]
  end
  super compiled_opts
end

Instance Attribute Details

#asciidoc_optsObject (readonly)

Returns the value of attribute asciidoc_opts.



21
22
23
# File 'lib/guard/asciidoc.rb', line 21

def asciidoc_opts
  @asciidoc_opts
end

Instance Method Details

#run_allObject



65
66
67
# File 'lib/guard/asciidoc.rb', line 65

def run_all
  run match_all, force: true
end

#run_on_changes(paths) ⇒ Object Also known as: run_on_additions, run_on_modifications



69
70
71
# File 'lib/guard/asciidoc.rb', line 69

def run_on_changes paths
  options[:always_build_all] ? run_all : (run paths)
end

#startObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/guard/asciidoc.rb', line 52

def start
  Compat::UI.info 'Guard::AsciiDoc is now watching files'
  require 'asciidoctor'
  if !(::Asciidoctor::Converter.for asciidoc_opts[:backend]) &&
      (::Gem.loaded_specs.key? (converter_gem = %(asciidoctor-#{asciidoc_opts[:backend]})))
    require converter_gem
  end
  if (requires = asciidoc_opts.delete :requires)
    Array(requires).each {|require_| require require_ }
  end
  run_all if options[:run_on_start]
end