Class: DoubleDoc::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/double_doc/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(md_sources, options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
# File 'lib/double_doc/client.rb', line 8

def initialize(md_sources, options = {})
  @md_sources = [md_sources].flatten
  @options = options
end

Instance Attribute Details

#md_sourcesObject (readonly)

Returns the value of attribute md_sources.



6
7
8
# File 'lib/double_doc/client.rb', line 6

def md_sources
  @md_sources
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/double_doc/client.rb', line 6

def options
  @options
end

Instance Method Details

#processObject



13
14
15
16
17
18
19
20
21
22
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
51
52
53
54
# File 'lib/double_doc/client.rb', line 13

def process
  sources = md_sources.map do |source|
    if source.to_s =~ /\*/
      import_handler.load_paths.map do |path|
        Dir.glob(File.join(path, source))
      end
    else
      import_handler.find_file(source).path
    end
  end.flatten.uniq

  generated_md_files = []

  md_dst = Pathname.new(options[:md_destination])
  system('mkdir', '-p', md_dst.to_s)
  sources.each do |src|
    next if File.directory?(src)
    dst = md_dst + File.basename(src)
    puts "#{src} -> #{dst}" unless options[:quiet]

    if src.to_s =~ /\.md$/
      body = import_handler.resolve_imports(File.new(src))
    else
      body = File.read(src)
    end

    File.open(dst, 'w') do |out|
      out.write(body)
    end

    generated_md_files << dst
  end

  args = options[:args] || {}
  html_dst = Pathname.new(options[:html_destination]) if options[:html_destination]
  if html_dst || args[:html_destination]
    html_generator = DoubleDoc::HtmlGenerator.new(generated_md_files, options.merge(args))
    html_generator.generate
  end

  sources
end