giblish 0.2.12 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,3 @@
1
- #!/usr/bin/ruby
2
-
3
1
  require_relative "utils"
4
2
  require_relative "version"
5
3
 
@@ -18,8 +16,8 @@ class CmdLineParser
18
16
  Options:
19
17
  -h --help show this help text
20
18
  -v --version show version nr and exit
21
- -f --format <format> the output format, currently html5 or pdf are supported
22
- *html5* is used if -f is not supplied
19
+ -f --format <format> the output format, currently html or pdf are supported
20
+ *html* is used if -f is not supplied
23
21
  -n --no-build-ref suppress generation of a reference document at the destination
24
22
  tree root.
25
23
  -r --resource-dir <dir> specify a directory where fonts, themes, css and other
@@ -50,10 +48,16 @@ class CmdLineParser
50
48
  tree (DirectoryRoot on Apache) and not the full absolute
51
49
  path to the css directory.
52
50
  -g --git-branches <regExp> if the source_dir_top is located within a git repo,
53
- generate docs for all remote branches that matches
51
+ generate docs for all _remote branches on origin_ that matches
54
52
  the given regular expression. Each git branch will
55
53
  be generated to a separate subdir under the destination
56
54
  root dir.
55
+ NOTE: To do this, giblish will _explicitly check out the
56
+ matching branches and merge them with the corresponding
57
+ branch on origin_.
58
+ NOTE 2: In bash, use double quotes around your regexp if
59
+ you need to quote it. Single quotes are treated as part
60
+ of the regexp itself.
57
61
  -t --git-tags <regExp> if the source_dir_top is located within a git repo,
58
62
  generate docs for all tags that matches the given
59
63
  regular expression. Each tag will be generated to
@@ -66,7 +70,7 @@ class CmdLineParser
66
70
  doc ids to resolve relative paths between the
67
71
  generated documents
68
72
  --log-level set the log level explicitly. Must be one of
69
- debug, info, warn (default), error or fatal.
73
+ debug, info (default), warn, error or fatal.
70
74
  ENDHELP
71
75
 
72
76
  def initialize(cmdline_args)
@@ -102,7 +106,7 @@ ENDHELP
102
106
  private
103
107
 
104
108
  def set_log_level
105
- log_level = @args[:logLevel] || "warn"
109
+ log_level = @args[:logLevel] || "info"
106
110
  case log_level
107
111
  when "debug" then Giblog.logger.sev_threshold = Logger::DEBUG
108
112
  when "info" then Giblog.logger.sev_threshold = Logger::INFO
data/lib/giblish/core.rb CHANGED
@@ -1,465 +1,316 @@
1
- #!/usr/bin/env ruby
2
- #
3
- # Converts a tree of asciidoc docs to pdf/html
4
- #
5
- #
6
-
7
1
  require "find"
8
2
  require "fileutils"
9
3
  require "logger"
10
4
  require "pathname"
11
- require "asciidoctor"
12
- require "asciidoctor-pdf"
13
5
 
14
- require_relative "cmdline"
15
6
  require_relative "buildindex"
7
+ require_relative "docconverter"
16
8
  require_relative "docid"
17
-
18
- # Base class for document converters. It contains a hash of
19
- # conversion options used by derived classes
20
- class DocConverter
21
- # a common set of converter options used for all output formats
22
- COMMON_CONVERTER_OPTS = {
23
- safe: Asciidoctor::SafeMode::UNSAFE,
24
- header_footer: true,
25
- mkdirs: true
26
- }.freeze
27
-
28
- # the giblish attribute defaults used if nothing else
29
- # is required by the user
30
- DEFAULT_ATTRIBUTES = {
31
- "source-highlighter" => "rouge",
32
- "xrefstyle" => "short"
33
- }.freeze
34
-
35
- # setup common options that are used regardless of the
36
- # specific output format used
37
- attr_reader :converter_options
38
-
39
- # Public: Setup common converter options. Required options are:
40
- # :srcDirRoot
41
- # :dstDirRoot
42
- # :resourceDir
43
- def initialize(options)
44
- @paths = Giblish::PathManager.new(
45
- options[:srcDirRoot], options[:dstDirRoot], options[:resourceDir]
46
- )
47
-
48
- @user_style = options[:userStyle]
49
- @converter_options = COMMON_CONVERTER_OPTS.dup
50
- @converter_options[:attributes] = DEFAULT_ATTRIBUTES.dup
51
- @converter_options[:backend] = options[:backend]
52
- end
53
-
54
- def convert_str(input_str, src_path, output_file = nil)
55
- unless input_str.is_a?(String)
56
- raise ArgumentError("Trying to invoke convert_str with non-string!")
9
+ require_relative "docinfo"
10
+ require_relative "buildgraph"
11
+
12
+ module Giblish
13
+
14
+ # Parse a directory tree and convert all asciidoc files matching the
15
+ # supplied critera to the supplied format
16
+ class FileTreeConverter
17
+
18
+ attr_reader :converter
19
+
20
+ # Required options:
21
+ # srcDirRoot
22
+ # dstDirRoot
23
+ # resourceDir
24
+ def initialize(options)
25
+ @options = options.dup
26
+
27
+ @paths = Giblish::PathManager.new(
28
+ @options[:srcDirRoot],
29
+ @options[:dstDirRoot],
30
+ @options[:resourceDir]
31
+ )
32
+ @processed_docs = []
33
+ @converter = converter_factory
57
34
  end
58
35
 
59
- # use the same options as when converting all docs
60
- # in the tree but make sure Asciidoctor doesn't write to file
61
- index_opts = @converter_options.dup
62
- index_opts.delete(:to_file)
63
- index_opts.delete(:to_dir)
36
+ def convert
37
+ # collect all doc ids and enable replacement of known doc ids with
38
+ # valid references to adoc files
39
+ manage_doc_ids if @options[:resolveDocid]
40
+
41
+ # traverse the src file tree and convert all files deemed as
42
+ # adoc files
43
+ Find.find(@paths.src_root_abs) do |path|
44
+ p = Pathname.new(path)
45
+ to_asciidoc(p) if adocfile? p
46
+ end if @paths.src_root_abs.directory?
47
+
48
+ # check if we shall build index or not
49
+ return if @options[:suppressBuildRef]
50
+
51
+ # build a dependency graph (only if we resolve docids...)
52
+ dep_graph_exist = if @options[:resolveDocid]
53
+ if Giblish::GraphBuilderGraphviz.supported
54
+ gb = Giblish::GraphBuilderGraphviz.new @processed_docs, @paths, {extension: @converter.converter_options[:fileext]}
55
+ @converter.convert_str gb.source, @paths.dst_root_abs, "graph"
56
+ else
57
+ Giblog.logger.warn { "Lacking access to needed tools for generating a visual dependency graph." }
58
+ Giblog.logger.warn { "The dependency graph will not be generated !!" }
59
+ false
60
+ end
61
+ else
62
+ false
63
+ end
64
64
 
65
- # load and convert the string using the converter options
66
- doc = Asciidoctor.load input_str, index_opts
67
- output = doc.convert index_opts
65
+ # build a reference index
66
+ ib = index_factory
67
+ @converter.convert_str ib.source(dep_graph_exist), @paths.dst_root_abs, "index"
68
68
 
69
- # determine the correct output path
70
- if output_file.nil?
71
- output_file = @paths.adoc_output_file(src_path,
72
- @converter_options[:fileext])
69
+ # clean up cached files and adoc resources
70
+ remove_diagram_temps if dep_graph_exist
71
+ GC.start
73
72
  end
74
73
 
75
- # write the converted document to file and return the doc
76
- doc.write output, output_file.to_s
77
- doc
78
- end
74
+ protected
79
75
 
80
- # Public: Convert one single adoc file using the specific conversion
81
- # options.
82
- #
83
- # filepath - a pathname with the absolute path to the input file to convert
84
- #
85
- # Returns: The resulting Asciidoctor::Document object
86
- def convert(filepath)
87
- unless filepath.is_a?(Pathname)
88
- raise ArgumentError, "Trying to invoke convert with non-pathname!"
76
+ # get the correct index builder type depending on supplied
77
+ # user options
78
+ def index_factory
79
+ raise "Internal logic error!" if @options[:suppressBuildRef]
80
+ SimpleIndexBuilder.new(@processed_docs, @paths,
81
+ @options[:resolveDocid])
89
82
  end
90
83
 
91
- Giblog.logger.info { "Processing: #{filepath}" }
92
-
93
- # create an asciidoc doc object and convert to requested
94
- # output using current conversion options
95
- @converter_options[:to_dir] = @paths.adoc_output_dir(filepath).to_s
96
- @converter_options[:base_dir] =
97
- Giblish::PathManager.closest_dir(filepath).to_s
98
- @converter_options[:to_file] =
99
- Giblish::PathManager.get_new_basename(filepath,
100
- @converter_options[:fileext])
101
-
102
- Giblog.logger.debug { "converter_options: #{@converter_options}" }
103
- # do the actual conversion
104
-
105
- Asciidoctor.convert_file filepath, @converter_options
106
- end
107
-
108
- protected
109
-
110
- # Protected: Adds the supplied backend specific options and
111
- # attributes to the base ones.
112
- # The following options must be provided by the derived class:
113
- # :fileext - a string with the filename extention to use for the
114
- # generated file
115
- #
116
- # backend_opts - the options specific to the asciidoctor backend
117
- # that the derived class supports
118
- # backend_attribs - the attributes specific to the asciidoctor backend
119
- # that the derived class supports
120
- def add_backend_options(backend_opts, backend_attribs)
121
- @converter_options = @converter_options.merge(backend_opts)
122
- @converter_options[:attributes] =
123
- @converter_options[:attributes].merge(backend_attribs)
124
- end
125
- end
126
-
127
- # Converts asciidoc files to html5 output.
128
- class HtmlConverter < DocConverter
129
- # Public: Setup common converter options. Required options are:
130
- # :srcDirRoot
131
- # :dstDirRoot
132
- # :resourceDir
133
- def initialize(options)
134
- super options
135
-
136
- # require access to asciidoc-rouge
137
- require "asciidoctor-rouge"
138
-
139
- # handle needed assets for the styling (css et al)
140
- html_attrib = setup_web_assets options[:webRoot]
141
-
142
- # Setting 'data-uri' makes asciidoctor embed images in the resulting
143
- # html file
144
- html_attrib["data-uri"] = 1
145
-
146
- # tell asciidoctor to use the html5 backend
147
- backend_options = { backend: "html5", fileext: "html" }
148
- add_backend_options backend_options, html_attrib
149
- end
84
+ # get the correct converter type
85
+ def converter_factory
86
+ case @options[:format]
87
+ when "html" then
88
+ HtmlConverter.new @paths, @options
89
+ when "pdf" then
90
+ PdfConverter.new @paths, @options
91
+ else
92
+ raise ArgumentError, "Unknown conversion format: #{@options[:format]}"
93
+ end
94
+ end
150
95
 
151
- private
96
+ # creates a DocInfo instance, fills it with basic info and
97
+ # returns the filled in instance so that derived implementations can
98
+ # add more data
99
+ def add_doc(adoc, adoc_stderr)
100
+ Giblog.logger.debug do
101
+ "Adding adoc: #{adoc} Asciidoctor stderr: #{adoc_stderr}"
102
+ end
103
+ Giblog.logger.debug {"Doc attributes: #{adoc.attributes}"}
152
104
 
153
- def setup_stylesheet_attributes(css_dir)
154
- return {} if @paths.resource_dir_abs.nil?
105
+ info = DocInfo.new(adoc: adoc, dst_root_abs: @paths.dst_root_abs, adoc_stderr: adoc_stderr)
106
+ @processed_docs << info
107
+ info
108
+ end
155
109
 
156
- # use the supplied stylesheet if there is one
157
- attrib = { "linkcss" => 1,
158
- "stylesdir" => css_dir,
159
- "stylesheet" => "giblish.css",
160
- "copycss!" => 1 }
110
+ def add_doc_fail(filepath, exception)
111
+ info = DocInfo.new
161
112
 
162
- # Make sure that a user supplied stylesheet ends with .css or .CSS
163
- @user_style &&
164
- attrib["stylesheet"] =
165
- /\.(css|CSS)$/ =~ @user_style ? @user_style : "#{@user_style}.css"
166
- Giblog.logger.debug {"stylesheet attributes: #{attrib}"}
167
- attrib
168
- end
113
+ # the only info we have is the source file name
114
+ info.converted = false
115
+ info.src_file = filepath
116
+ info.error_msg = exception.message
169
117
 
170
- # make sure that linked assets are available at dst_root
171
- def setup_web_assets(html_dir_root = nil)
172
- # only set this up if user has specified a resource dir
173
- return {} unless @paths.resource_dir_abs
118
+ @processed_docs << info
119
+ info
120
+ end
174
121
 
175
- # create dir for web assets directly under dst_root
176
- assets_dir = "#{@paths.dst_root_abs}/web_assets"
177
- Dir.exist?(assets_dir) || FileUtils.mkdir_p(assets_dir)
122
+ private
178
123
 
179
- # copy needed assets
180
- %i[css fonts images].each do |dir|
181
- src = "#{@paths.resource_dir_abs}/#{dir}"
182
- Dir.exist?(src) && FileUtils.copy_entry(src, "#{assets_dir}/#{dir}")
124
+ # remove cache dir and svg image created by asciidoctor-diagram
125
+ # when creating the document dependency graph
126
+ def remove_diagram_temps
127
+ adoc_diag_cache = @paths.dst_root_abs.join(".asciidoctor")
128
+ FileUtils.remove_dir(adoc_diag_cache) if adoc_diag_cache.directory?
129
+ Giblog.logger.info {"Removing cached files at: #{@paths.dst_root_abs.join("docdeps.svg").to_s}"}
130
+ @paths.dst_root_abs.join("docdeps.svg").delete
183
131
  end
184
132
 
185
- # find the path to the assets dir that is correct when called from a url,
186
- # taking the DirectoryRoot for the web site into consideration.
187
- if html_dir_root
188
- wr = Pathname.new(
189
- assets_dir
190
- ).relative_path_from Pathname.new(html_dir_root)
191
- Giblog.logger.info { "Relative web root: #{wr}" }
192
- assets_dir = "/" << wr.to_s
133
+ # convert a single adoc doc to whatever the user wants
134
+ def to_asciidoc(filepath)
135
+ adoc = nil
136
+ begin
137
+ # do the conversion and capture eventual errors that
138
+ # the asciidoctor lib writes to stderr
139
+ adoc_stderr = Giblish.with_captured_stderr do
140
+ adoc = @converter.convert filepath
141
+ end
142
+
143
+ add_doc(adoc, adoc_stderr)
144
+ rescue Exception => e
145
+ str = "Error when converting doc: #{e.message}\n"
146
+ e.backtrace.each {|l| str << "#{l}\n"}
147
+ Giblog.logger.error {str}
148
+
149
+ add_doc_fail(filepath, e)
150
+ end
193
151
  end
194
152
 
195
- Giblog.logger.info { "stylesheet dir: #{assets_dir}" }
196
- setup_stylesheet_attributes "#{assets_dir}/css"
197
- end
198
- end
199
-
200
- class PdfConverter < DocConverter
201
- def initialize(options)
202
- super options
203
-
204
- pdf_attrib = setup_pdf_attribs
153
+ # predicate that decides if a path is a asciidoc file or not
154
+ def adocfile?(path)
155
+ fs = path.basename.to_s
205
156
 
206
- backend_options = { backend: "pdf", fileext: "pdf" }
207
- add_backend_options backend_options, pdf_attrib
208
- end
209
-
210
- private
211
-
212
- def setup_pdf_attribs()
213
- # only set this up if user has specified a resource dir
214
- return {} unless @paths.resource_dir_abs
215
-
216
- pdf_attrib = {
217
- "pdf-stylesdir" => "#{@paths.resource_dir_abs}/themes",
218
- "pdf-style" => "giblish.yml",
219
- "pdf-fontsdir" => "#{@paths.resource_dir_abs}/fonts",
220
- "icons" => "font"
221
- }
222
-
223
- # Make sure that the stylesheet ends with .yml or YML
224
- @user_style &&
225
- pdf_attrib["pdf-style"] =
226
- /\.(yml|YML)$/ =~ @user_style ? @user_style : "#{@user_style}.yml"
157
+ unless @options[:excludeRegexp].nil?
158
+ # exclude file if user wishes
159
+ er = Regexp.new @options[:excludeRegexp]
160
+ return false unless er.match(fs).nil?
161
+ end
227
162
 
228
- pdf_attrib
229
- end
230
- end
231
-
232
- class TreeConverter
233
-
234
- # Required options:
235
- # srcDirRoot
236
- # dstDirRoot
237
- # resourceDir
238
- def initialize(options)
239
- @options = options.dup
240
-
241
- @paths = Giblish::PathManager.new(
242
- @options[:srcDirRoot],
243
- @options[:dstDirRoot],
244
- @options[:resourceDir]
245
- )
246
-
247
- # init_dst_root
248
-
249
- # prepare the index page if requested
250
- unless @options[:suppressBuildRef]
251
- @index_builder = if @options[:gitRepoRoot]
252
- GitRepoIndexBuilder.new(@paths,
253
- options[:resolveDocid],
254
- options[:gitRepoRoot])
255
- else
256
- SimpleIndexBuilder.new(@paths,
257
- options[:resolveDocid])
258
- end
163
+ # only include files
164
+ ir = Regexp.new @options[:includeRegexp]
165
+ return !ir.match(fs).nil?
259
166
  end
260
167
 
261
- @conversion =
262
- case options[:format]
263
- when "html" then HtmlConverter.new options
264
- when "pdf" then PdfConverter.new options
265
- else
266
- raise ArgumentError, "Unknown conversion format: #{options[:format]}"
267
- end
168
+ # Register the asciidoctor extension that handles doc ids and traverse
169
+ # the source tree to collect all :docid: attributes found in document
170
+ # headers.
171
+ def manage_doc_ids
172
+ # Register the docid preprocessor hook
173
+ Giblish.register_extensions
174
+
175
+ # Make sure that no prior docid's are hangning around
176
+ DocidCollector.clear_cache
177
+ DocidCollector.clear_deps
178
+ idc = DocidCollector.new
179
+
180
+ # traverse the src file tree and collect ids from all
181
+ # .adoc or .ADOC files
182
+ Find.find(@paths.src_root_abs) do |path|
183
+ p = Pathname.new(path)
184
+ idc.parse_file(p) if adocfile? p
185
+ end if @paths.src_root_abs.directory?
186
+ idc
187
+ end
268
188
  end
269
189
 
270
- def generate_index(src_str, dst_dir)
271
- # use the same options as when converting all docs
272
- # in the tree but make sure we don't write to file
273
- index_opts = @conversion.converter_options.dup
274
- index_opts.delete(:to_file)
275
- index_opts.delete(:to_dir)
276
-
277
- # load and convert the document using the converter options
278
- doc = Asciidoctor.load src_str, index_opts
279
- output = doc.convert index_opts
280
-
281
- # write the converted document to an index file located at the
282
- # destination root
283
- index_filepath = dst_dir + "index.#{index_opts[:fileext]}"
284
- doc.write output, index_filepath.to_s
285
- end
190
+ class GitRepoConverter < FileTreeConverter
191
+ def initialize(options)
192
+ super(options)
193
+ # cache the top of the tree since we need to redefine the
194
+ # paths per branch/tag later on.
195
+ @master_paths = @paths.dup
196
+ @git_repo_root = options[:gitRepoRoot]
197
+ @git_repo = init_git_repo @git_repo_root, options[:localRepoOnly]
198
+ @user_branches = select_user_branches(options[:gitBranchRegexp])
199
+ @user_tags = select_user_tags(options[:gitTagRegexp])
200
+ end
286
201
 
287
- def to_asciidoc(filepath)
288
- adoc = nil
289
- begin
290
- # do the conversion and capture eventual errors that
291
- # the asciidoctor lib writes to stderr
292
- adoc_stderr = Giblish.with_captured_stderr do
293
- adoc = @conversion.convert filepath
202
+ # Render the docs from each branch/tag and add info to the
203
+ # summary page
204
+ def convert
205
+ (@user_branches + @user_tags).each do |co|
206
+ convert_one_checkout co
294
207
  end
295
208
 
296
- # build the reference index if the user wants it
297
- @options[:suppressBuildRef] || @index_builder.add_doc(adoc, adoc_stderr)
298
- rescue Exception => e
299
- str = "Error when converting doc: #{e.message}\n"
300
- e.backtrace.each { |l| str << "#{l}\n" }
301
- Giblog.logger.error { str }
302
- @options[:suppressBuildRef] || @index_builder.add_doc_fail(filepath, e)
209
+ # Render the summary page
210
+ index_builder = GitSummaryIndexBuilder.new @git_repo,
211
+ @user_branches,
212
+ @user_tags
213
+ @converter.convert_str index_builder.source, @master_paths.dst_root_abs, "index"
214
+ # clean up
215
+ GC.start
303
216
  end
304
- end
305
217
 
306
- def walk_dirs
307
- # collect all doc ids and enable replacement of known doc ids with
308
- # valid references to adoc files
309
- manage_doc_ids if @options[:resolveDocid]
218
+ protected
310
219
 
311
- # traverse the src file tree and convert all files that ends with
312
- # .adoc or .ADOC
313
- Find.find(@paths.src_root_abs) do |path|
314
- p = Pathname.new(path)
315
- to_asciidoc(p) if adocfile? p
220
+ def index_factory
221
+ GitRepoIndexBuilder.new(@processed_docs, @paths,
222
+ @options[:resolveDocid], @options[:gitRepoRoot])
316
223
  end
317
224
 
318
- # check if we shall build index or not
319
- return if @options[:suppressBuildRef]
320
-
321
- # build a reference index
322
- generate_index @index_builder.index_source, @paths.dst_root_abs
323
-
324
- # clean up adoc resources
325
- @index_builder = nil
326
- GC.start
327
- end
328
-
329
- private
330
-
331
- # predicate that decides if a path is a asciidoc file or not
332
- def adocfile?(path)
333
- fs = path.basename.to_s
334
-
335
- unless @options[:excludeRegexp].nil?
336
- # exclude file if user wishes
337
- er = Regexp.new @options[:excludeRegexp]
338
- return false unless er.match(fs).nil?
225
+ def add_doc(adoc, adoc_stderr)
226
+ info = super(adoc, adoc_stderr)
227
+
228
+ # Redefine the srcFile to mean the relative path to the git repo root
229
+ src_file = Pathname.new(info.src_file).relative_path_from(@git_repo_root).to_s
230
+ # Get the commit history of the doc
231
+ # (use a homegrown git log to get 'follow' flag)
232
+ gi = Giblish::GitItf.new(@git_repo_root)
233
+ gi.file_log(src_file).each do |i|
234
+ h = DocInfo::DocHistory.new
235
+ h.date = i["date"]
236
+ h.message = i["message"]
237
+ h.author = i["author"]
238
+ info.history << h
239
+ end
339
240
  end
340
241
 
341
- # only include files
342
- ir = Regexp.new @options[:includeRegexp]
343
- return !ir.match(fs).nil?
344
- end
242
+ private
345
243
 
346
- # Register the asciidoctor extension that handles doc ids and traverse
347
- # the source tree to collect all :docid: attributes found in document
348
- # headers.
349
- def manage_doc_ids
350
- # Register the docid preprocessor hook
351
- Giblish.register_extensions
352
-
353
- # Make sure that no prior docid's are hangning around
354
- Giblish::DocidCollector.clear_cache
355
- idc = Giblish::DocidCollector.new
356
-
357
- # traverse the src file tree and collect ids from all
358
- # .adoc or .ADOC files
359
- Find.find(@paths.src_root_abs) do |path|
360
- p = Pathname.new(path)
361
- idc.parse_file(p) if adocfile? p
362
- end
363
- idc
364
- end
365
- end
366
-
367
- class GitRepoParser
368
- def initialize(options)
369
- @options = options
370
- @paths = Giblish::PathManager.new(
371
- @options[:srcDirRoot],
372
- @options[:dstDirRoot],
373
- @options[:resourceDir]
374
- )
375
- @git_repo_root = options[:gitRepoRoot]
376
-
377
- # Sanity check git repo root
378
- @git_repo_root || raise(ArgumentError("No git repo root dir given"))
379
-
380
- # Connect to the git repo
381
- begin
382
- @git_repo = Git.open(@git_repo_root)
383
- rescue Exception => e
384
- raise "Could not find a git repo at #{@git_repo_root} !"\
244
+ def init_git_repo(git_repo_root, local_only)
245
+ # Sanity check git repo root
246
+ git_repo_root || raise(ArgumentError("No git repo root dir given"))
247
+
248
+ # Connect to the git repo
249
+ begin
250
+ git_repo = Git.open(git_repo_root)
251
+ rescue Exception => e
252
+ raise "Could not find a git repo at #{git_repo_root} !"\
385
253
  "\n\n(#{e.message})"
386
- end
254
+ end
387
255
 
388
- # fetch all remote refs if ok with user
389
- begin
390
- @git_repo.fetch unless options[:localRepoOnly]
391
- rescue Exception => e
392
- raise "Could not fetch from origin"\
256
+ # fetch all remote refs if ok with user
257
+ begin
258
+ git_repo.fetch unless local_only
259
+ rescue Exception => e
260
+ raise "Could not fetch from origin"\
393
261
  "(do you need '--local-only'?)!\n\n(#{e.message})"
262
+ end
263
+ git_repo
394
264
  end
395
265
 
396
- # initialize summary builder
397
- @index_builder = GitSummaryIndexBuilder.new @git_repo
266
+ # Get the branches/tags the user wants to parse
267
+ def select_user_branches(checkout_regexp)
268
+ return unless @options[:gitBranchRegexp]
398
269
 
399
- # Get the branches the user wants to parse
400
- if options[:gitBranchRegexp]
401
- regexp = Regexp.new options[:gitBranchRegexp]
402
- @user_branches = @git_repo.branches.remote.select do |b|
270
+ regexp = Regexp.new checkout_regexp
271
+ user_checkouts = @git_repo.branches.remote.select do |b|
403
272
  # match branches but remove eventual HEAD -> ... entry
404
273
  regexp.match b.name unless b.name =~ /^HEAD/
405
274
  end
406
- Giblog.logger.debug { "selected git branches: #{@user_branches}" }
407
-
408
- # Render the docs from each branch and add info to the
409
- # summary page
410
- @user_branches.each do |b|
411
- render_one_branch b, @options
412
- @index_builder.add_branch b
413
- end
275
+ Giblog.logger.debug {"selected git branches: #{user_checkouts}"}
276
+ user_checkouts
414
277
  end
415
278
 
416
- # Get the tags the user wants to parse
417
- if options[:gitTagRegexp]
418
- regexp = Regexp.new options[:gitTagRegexp]
419
- @user_tags = @git_repo.tags.select do |t|
420
- regexp.match t.name
421
- end
279
+ def select_user_tags(tag_regexp)
280
+ return [] unless tag_regexp
422
281
 
423
- # Render the docs from each branch and add info to the
424
- # summary page
425
- @user_tags.each do |t|
426
- render_one_branch t, @options, true
427
- @index_builder.add_tag t
282
+ regexp = Regexp.new @options[:gitTagRegexp]
283
+ tags = @git_repo.tags.select do |t|
284
+ regexp.match t.name
428
285
  end
286
+ tags
429
287
  end
430
288
 
431
- # Render the summary page
432
- tc = TreeConverter.new options
433
- tc.generate_index @index_builder.index_source, @paths.dst_root_abs
434
-
435
- # clean up
436
- @index_builder = nil
437
- GC.start
438
- end
439
-
440
- def render_one_branch(b, opts, is_tag = false)
441
- # work with local options
442
- options = opts.dup
289
+ def convert_one_checkout(co)
290
+ # determine if we are called with a tag or a branch
291
+ is_tag = (co.respond_to?(:tag?) && co.tag?)
443
292
 
444
- # check out the branch in question and make sure it is
445
- # up-to-date
446
- Giblog.logger.info { "Checking out #{b.name}" }
447
- @git_repo.checkout b.name
293
+ Giblog.logger.info {"Checking out #{co.name}"}
294
+ @git_repo.checkout co.name
448
295
 
449
- unless is_tag
450
- Giblog.logger.info { "Merging with origin/#{b.name}" }
451
- @git_repo.merge "origin/#{b.name}"
452
- end
296
+ unless is_tag
297
+ # if this is a branch, make sure it is up-to-date
298
+ Giblog.logger.info {"Merging with origin/#{co.name}"}
299
+ @git_repo.merge "origin/#{co.name}"
300
+ end
453
301
 
454
- # assign a branch-unique dst-dir
455
- dir_name = b.name.tr("/", "_") << "/"
302
+ # assign a checkout-unique dst-dir
303
+ dir_name = co.name.tr("/", "_") << "/"
456
304
 
457
- # Assign the branch specific dir as new destination root
458
- options[:dstDirRoot] = @paths.dst_root_abs.realpath.join(dir_name).to_s
305
+ # Update needed base class members before converting a new checkout
306
+ @processed_docs = []
307
+ @paths.dst_root_abs = @master_paths.dst_root_abs.realpath.join(dir_name)
308
+ # @converter.paths = @paths
459
309
 
460
- # Parse and render docs using given args
461
- Giblog.logger.info { "Render docs to dir #{options[:dstDirRoot]}" }
462
- tc = TreeConverter.new options
463
- tc.walk_dirs
310
+ # Parse and convert docs using given args
311
+ Giblog.logger.info {"Convert docs into dir #{@paths.dst_root_abs}"}
312
+ # parent_convert
313
+ FileTreeConverter.instance_method(:convert).bind(self).call
314
+ end
464
315
  end
465
- end
316
+ end