Class: SDoc::Merge

Inherits:
Object
  • Object
show all
Includes:
Templatable
Defined in:
lib/sdoc/merge.rb

Constant Summary collapse

FLAG_FILE =
"created.rid"

Instance Method Summary collapse

Methods included from Templatable

#eval_template, #include_template, #render_template

Constructor Details

#initializeMerge

Returns a new instance of Merge.



13
14
15
16
17
18
19
20
21
# File 'lib/sdoc/merge.rb', line 13

def initialize()
  @names = []
  @urls = []
  @op_dir = 'doc'
  @title = ''
  @directories = []
  template_dir = RDoc::Generator::SHtml.template_dir('merge')
@template_dir = Pathname.new File.expand_path(template_dir)
end

Instance Method Details

#append_path(subtree, path) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/sdoc/merge.rb', line 87

def append_path subtree, path
  subtree.map do |item|
    item[1] = path + '/' + item[1] unless item[1].empty?
    item[3] = append_path item[3], path
    item
  end
end

#check_directoriesObject



200
201
202
203
204
205
206
207
208
# File 'lib/sdoc/merge.rb', line 200

def check_directories
  @directories.each do |dir|
    unless File.exists?(File.join(dir, FLAG_FILE)) && 
    File.exists?(File.join(dir, RDoc::Generator::SHtml::TREE_FILE)) && 
    File.exists?(File.join(dir, RDoc::Generator::SHtml::SEARCH_INDEX_FILE))
      error "#{dir} does not seem to be an sdoc directory"
    end
  end
end

#copy_docsObject



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/sdoc/merge.rb', line 170

def copy_docs
  @directories.each_with_index do |dir, i|
    name = @names[i]
    index_dir = File.dirname(RDoc::Generator::SHtml::TREE_FILE)
    FileUtils.mkdir_p(File.join(@op_dir, name))
    
    Dir.new(dir).each do |item|
      if File.directory?(File.join(dir, item)) && item != '.' && item != '..' && item != index_dir
        FileUtils.cp_r File.join(dir, item), File.join(@op_dir, name, item), :preserve => true
      end
    end
  end
end

#copy_filesObject



184
185
186
187
188
189
190
191
# File 'lib/sdoc/merge.rb', line 184

def copy_files
  dir = @directories.first
  Dir.new(dir).each do |item|
    if item != '.' && item != '..' && item != RDoc::Generator::SHtml::FILE_DIR && item != RDoc::Generator::SHtml::CLASS_DIR
      FileUtils.cp_r File.join(dir, item), @op_dir, :preserve => true
    end
  end
end

#error(msg) ⇒ Object

Report an error message and exit

Raises:

  • (RDoc::Error)


213
214
215
# File 'lib/sdoc/merge.rb', line 213

def error(msg)
  raise RDoc::Error, msg
end

#extract_index_path(dir) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/sdoc/merge.rb', line 140

def extract_index_path dir
  filename = File.join dir, 'index.html'
  content = File.open(filename) { |f| f.read }
  match = content.match(/<frame\s+src="([^"]+)"\s+name="docwin"/mi)
  if match
    match[1]
  else
    ''
  end
end

#generate_index_fileObject



151
152
153
154
155
156
157
158
# File 'lib/sdoc/merge.rb', line 151

def generate_index_file
  templatefile = @template_dir + 'index.rhtml'
  outfile      = @outputdir + 'index.html'
  url          = @urls.empty? ? @names[0] : @urls[0]
  index_path   = url + '/' + extract_index_path(@directories[0])
 
  render_template templatefile, binding(), outfile
end

#merge(options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sdoc/merge.rb', line 23

def merge(options)
  parse_options options
  
@outputdir = Pathname.new( @op_dir )

  check_directories
  setup_output_dir
  setup_names
  copy_files
  copy_docs if @urls.empty?
  merge_search_index
  merge_tree
  generate_index_file
end

#merge_search_indexObject



95
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
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sdoc/merge.rb', line 95

def merge_search_index
  items = []
  @indexes = {}
  @directories.each_with_index do |dir, i|
    name = @names[i]
    url = @urls.empty? ? name : @urls[i]
    filename = File.join dir, RDoc::Generator::SHtml::SEARCH_INDEX_FILE
    data = open(filename).read.sub(/var search_data =\s*/, '')
    subindex = JSON.parse(data, :max_nesting => 0)
    @indexes[name] = subindex
    
    searchIndex = subindex["index"]["searchIndex"]
    longSearchIndex = subindex["index"]["longSearchIndex"]
    subindex["index"]["info"].each_with_index do |info, j|
      info[2] = url + '/' + info[2]
      info[6] = i
      items << {
        :info => info,
        :searchIndex => searchIndex[j],
        :longSearchIndex => name + ' ' + longSearchIndex[j]
      }
    end
  end
  items.sort! do |a, b|
    # type (class/method/file) or name or doc part or namespace
    [a[:info][5], a[:info][0], a[:info][6], a[:info][1]] <=> [b[:info][5], b[:info][0], b[:info][6], b[:info][1]]
  end
  
  index = {
    :searchIndex => items.map{|item| item[:searchIndex]},
    :longSearchIndex => items.map{|item| item[:longSearchIndex]},
    :info => items.map{|item| item[:info]}
  }
  search_data = {
    :index => index,
    :badges => @names
  }
  
  dst = File.join @op_dir, RDoc::Generator::SHtml::SEARCH_INDEX_FILE
  FileUtils.mkdir_p File.dirname(dst)
  File.open(dst, "w", 0644) do |f|
    f.write('var search_data = '); f.write(search_data.to_json(:max_nesting => 0))
  end
end

#merge_treeObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sdoc/merge.rb', line 63

def merge_tree
  tree = []
  @directories.each_with_index do |dir, i|
    name = @names[i]
    url = @urls.empty? ? name : @urls[i]
    filename = File.join dir, RDoc::Generator::SHtml::TREE_FILE
    data = open(filename).read.sub(/var tree =\s*/, '')
    subtree = JSON.parse(data, :max_nesting => 0)
    item = [
      name,
      url + '/' + extract_index_path(dir),
      '',
      append_path(subtree, url)
    ]
    tree << item
  end
  
  dst = File.join @op_dir, RDoc::Generator::SHtml::TREE_FILE
  FileUtils.mkdir_p File.dirname(dst)
  File.open(dst, "w", 0644) do |f|
    f.write('var tree = '); f.write(tree.to_json(:max_nesting => 0))
  end
end

#parse_options(options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sdoc/merge.rb', line 38

def parse_options(options)
  opts = OptionParser.new do |opt|     
    opt.banner = "Usage: sdoc-merge [options] directories"
    
    opt.on("-n", "--names [NAMES]", "Names of merged repositories. Comma separated") do |v|
      @names = v.split(',').map{|name| name.strip }
    end
    
    opt.on("-o", "--op [DIRECTORY]", "Set the output directory") do |v|
      @op_dir = v
    end
    
    opt.on("-t", "--title [TITLE]", "Set the title of merged file") do |v|
      @title = v
    end
    
    opt.on("-u", "--urls [URLS]", "Paths to merged docs. If you \n" +
                 "set this files and classes won't be actualy copied to merged build") do |v|
      @urls = v.split(' ').map{|name| name.strip }
    end
  end
  opts.parse! options
  @directories = options.dup
end

#setup_namesObject



160
161
162
163
164
165
166
167
168
# File 'lib/sdoc/merge.rb', line 160

def setup_names
  unless @names.size > 0
    @directories.each do |dir|
      name = File.basename dir
      name = File.basename File.dirname(dir) if name == 'doc'
      @names << name
    end
  end
end

#setup_output_dirObject



193
194
195
196
197
198
# File 'lib/sdoc/merge.rb', line 193

def setup_output_dir
  if File.exists? @op_dir
    error "#{@op_dir} allready exists"
  end
  FileUtils.mkdir_p @op_dir
end