Class: Doc::Builder

Inherits:
BaseTask show all
Defined in:
lib/doc/builder.rb

Instance Attribute Summary collapse

Attributes inherited from BaseTask

#config, #dir_name, #documentor, #title

Instance Method Summary collapse

Methods inherited from BaseTask

#control_files_exist?, #doc_dir, #eql?, #failed?, #hash, #loaded_gem_version, #run, #run?, state_methods, #succeeded?, #symlink_to

Constructor Details

#initialize(documentor, options) ⇒ Builder

Returns a new instance of Builder.



6
7
8
9
10
11
12
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
55
56
57
58
59
60
61
62
63
64
# File 'lib/doc/builder.rb', line 6

def initialize(documentor, options)
  super
  @index = options[:index].to_s if options[:index]
  @main = options[:main].to_s if options[:main]
  @source_dir = FSPath(options[:source_dir]).expand_path if options[:source_dir]
  @paths = Array(options[:paths]) if options[:paths]

  unless @source_dir || @paths
    raise 'both source_dir and paths are not set'
  end

  if @paths
    if @source_dir && !options[:no_auto_add_paths]
      children = @source_dir.children.select(&:file?).map(&:basename).map(&:to_s)
      @paths = children.grep(/^((mit-)?license|change(?:s|log)|readme|history|todo|copying|faq|legal)($|\.)/i) | @paths
    end

    if @main
      unless @paths.include?(@main)
        @paths = [@main] | @paths
      end
    else
      %w[^ /].map do |prefix|
        [/#{prefix}readme(?:\.(?:txt|rdoc|markdown|md))?$/i, /#{prefix}readme\./i]
      end.flatten.each do |readme_r|
        break if @main = @paths.grep(readme_r).first
      end
    end

    @paths.select! do |path|
      source_dir ? (source_dir / path).readable? : File.readable?(path)
    end
    @paths.uniq!

    unless @source_dir
      if @source_dir = FSPath.common_dir(*@paths)
        @paths = @paths.map{ |path| FSPath(path).relative_path_from(@source_dir).to_s }
      end
    end
  end

  chdir_source_dir do
    paths_info = []
    if paths
      Find.find(*paths) do |path|
        paths_info << [path, File.size(path), File.mtime(path).to_i]
      end
    end

    @config = {
      :title => title,
      :dir_name => dir_name,
      :index => index,
      :main => main,
      :source_dir => source_dir.to_s,
      :paths => paths_info,
    }
  end
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



5
6
7
# File 'lib/doc/builder.rb', line 5

def index
  @index
end

#mainObject (readonly)

Returns the value of attribute main.



5
6
7
# File 'lib/doc/builder.rb', line 5

def main
  @main
end

#pathsObject (readonly)

Returns the value of attribute paths.



5
6
7
# File 'lib/doc/builder.rb', line 5

def paths
  @paths
end

#source_dirObject (readonly)

Returns the value of attribute source_dir.



5
6
7
# File 'lib/doc/builder.rb', line 5

def source_dir
  @source_dir
end

Instance Method Details

#buildObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/doc/builder.rb', line 66

def build
  cmd = Command.new('sdoc', "_#{loaded_gem_version('sdoc')}_")
  cmd.add '--line-numbers'
  cmd.add '--all'
  cmd.add '--charset=utf-8'
  cmd.add '--tab-width=2'
  cmd.add "--title=#{title}"
  cmd.add "--output=#{doc_dir}"
  cmd.add "--main=#{main}" if main
  cmd.add *paths if paths

  chdir_source_dir do
    cmd.run
  end

  if control_files_exist? && index
    index_dir_name = 'custom_index'
    FileUtils.cp_r(index, doc_dir / index_dir_name)
    index_html = doc_dir / 'index.html'
    index_html.write(index_html.read.sub(/(<frame src=")[^"]+(" name="docwin" \/>)/, "\\1#{index_dir_name}/index.html\\2"))
  end
end