giblish 0.7.6 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,11 +8,11 @@ require_relative "version"
8
8
  class CmdLineParser
9
9
  attr_accessor :args
10
10
 
11
- USAGE = <<~ENDUSAGE.freeze
11
+ USAGE = <<~ENDUSAGE
12
12
  Usage:
13
13
  giblish [options] source_dir_top dest_dir_top
14
14
  ENDUSAGE
15
- HELP = <<ENDHELP.freeze
15
+ HELP = <<ENDHELP
16
16
  Options:
17
17
  -h --help show this help text
18
18
  -v --version show version nr and exit
@@ -20,7 +20,7 @@ class CmdLineParser
20
20
  *html* is used if -f is not supplied
21
21
  -n --no-build-ref suppress generation of a reference document at the destination
22
22
  tree root.
23
- --index-basename set the name of the generated index file (default 'index').
23
+ --index-basename set the name of the generated index file (default 'index').
24
24
  -r --resource-dir <dir> specify a directory where fonts, themes, css and other
25
25
  central stuff needed for document generation are located.
26
26
  The resources are expected to be located in a subfolder
@@ -30,7 +30,7 @@ class CmdLineParser
30
30
  -s --style <name> The style information used when converting the documents
31
31
  using the -r option for specifying resource directories.
32
32
  For html this is a name of a css file, for pdf, this is
33
- the name of an yml file. You can specify only the
33
+ the name of an yml file. You can specify only the
34
34
  basename of the file and giblish will use the suffix
35
35
  associated with the output format (i.e specify 'mystyle'
36
36
  and the mystyle.css and mystyle.yml will be used for html
@@ -38,20 +38,20 @@ class CmdLineParser
38
38
  -i --include <regexp> include only files with a path that matches the supplied
39
39
  regexp (defaults to '.*\.(?i)adoc$' meaning it matches all
40
40
  files ending in .adoc case-insensitive). The matching is made
41
- on the full path (i.e. the regex '^.*my.*' matches the path
41
+ on the full path (i.e. the regex '^.*my.*' matches the path
42
42
  /my/file.adoc).
43
43
  -j --exclude <regexp> exclude files with a path that matches the supplied
44
44
  regexp (no files are excluded by default). The matching is made
45
- on the full path (i.e. the regex '^.*my.*' matches the path
45
+ on the full path (i.e. the regex '^.*my.*' matches the path
46
46
  /my/file.adoc).
47
47
  -w --web-path <path> Specifies the URL path to where the generated html documents
48
48
  will be deployed (only needed when serving the html docs via
49
49
  a web server).
50
50
  E.g.
51
- If the docs are deployed to 'www.example.com/site_1/blah',
51
+ If the docs are deployed to 'www.example.com/site_1/blah',
52
52
  this flag shall be set to '/site_1/blah'. This switch is only
53
53
  used when generating html. giblish use this to link the deployed
54
- html docs with the correct stylesheet.
54
+ html docs with the correct stylesheet.
55
55
  -g --git-branches <regExp> if the source_dir_top is located within a git repo,
56
56
  generate docs for all _remote branches on origin_ that matches
57
57
  the given regular expression. Each git branch will
@@ -69,10 +69,10 @@ class CmdLineParser
69
69
  a separate subdir under the destination root dir.
70
70
  -c --local-only do not try to fetch git info from any remotes of
71
71
  the repo before generating documents.
72
- -a --attribute <key>=<value> set a document or asciidoctor attribute.
73
- The contents of this flag is passed directly to the
74
- underlying asciidoctor tool, for details above the
75
- syntax and available attributes, see the documentation for
72
+ -a --attribute <key>=<value> set a document or asciidoctor attribute.
73
+ The contents of this flag is passed directly to the
74
+ underlying asciidoctor tool, for details above the
75
+ syntax and available attributes, see the documentation for
76
76
  asciidoctor. This option can be specified more than once.
77
77
  -d --resolve-docid use two passes, the first to collect :docid:
78
78
  attributes in the doc headers, the second to
@@ -107,9 +107,9 @@ class CmdLineParser
107
107
  Set this to the file system path where the generated html
108
108
  docs will be deployed (if different from dst_dir_top):
109
109
  E.g.
110
- If the generated html docs will be deployed to the folder
110
+ If the generated html docs will be deployed to the folder
111
111
  '/var/www/mysite/blah/mydocs,'
112
- this is what you shall set the path to.
112
+ this is what you shall set the path to.
113
113
  --log-level set the log level explicitly. Must be one of
114
114
  debug, info (default), warn, error or fatal.
115
115
  ENDHELP
@@ -144,15 +144,15 @@ ENDHELP
144
144
  def set_log_level
145
145
  log_level = @args[:logLevel] || "info"
146
146
  case log_level
147
- when "debug" then Giblog.logger.sev_threshold = Logger::DEBUG
148
- when "info" then Giblog.logger.sev_threshold = Logger::INFO
149
- when "warn" then Giblog.logger.sev_threshold = Logger::WARN
150
- when "error" then Giblog.logger.sev_threshold = Logger::ERROR
151
- when "fatal" then Giblog.logger.sev_threshold = Logger::FATAL
152
- else
153
- puts "Invalid log level specified. Run with -h to see supported levels"
154
- puts USAGE
155
- exit 1
147
+ when "debug" then Giblog.logger.sev_threshold = Logger::DEBUG
148
+ when "info" then Giblog.logger.sev_threshold = Logger::INFO
149
+ when "warn" then Giblog.logger.sev_threshold = Logger::WARN
150
+ when "error" then Giblog.logger.sev_threshold = Logger::ERROR
151
+ when "fatal" then Giblog.logger.sev_threshold = Logger::FATAL
152
+ else
153
+ puts "Invalid log level specified. Run with -h to see supported levels"
154
+ puts USAGE
155
+ exit 1
156
156
  end
157
157
  end
158
158
 
@@ -164,21 +164,21 @@ ENDHELP
164
164
  def parse_cmdline(cmdline_args)
165
165
  # default values for cmd line switches
166
166
  @args = {
167
- help: false,
168
- version: false,
169
- force: true,
170
- format: "html",
171
- # note that the single quotes are important for the regexp
172
- includeRegexp: '.*\.(?i)adoc$',
173
- excludeRegexp: nil,
174
- flatten: false,
175
- suppressBuildRef: false,
176
- indexBaseName: "index",
177
- localRepoOnly: false,
178
- resolveDocid: false,
179
- makeSearchable: false,
180
- searchAssetsDeploy: nil,
181
- webPath: nil
167
+ help: false,
168
+ version: false,
169
+ force: true,
170
+ format: "html",
171
+ # note that the single quotes are important for the regexp
172
+ includeRegexp: '.*\.(?i)adoc$',
173
+ excludeRegexp: nil,
174
+ flatten: false,
175
+ suppressBuildRef: false,
176
+ indexBaseName: "index",
177
+ localRepoOnly: false,
178
+ resolveDocid: false,
179
+ makeSearchable: false,
180
+ searchAssetsDeploy: nil,
181
+ webPath: nil
182
182
  }
183
183
 
184
184
  # set default log level
@@ -191,35 +191,35 @@ ENDHELP
191
191
  next_arg = unflagged_args.first
192
192
  cmdline_args.each do |arg|
193
193
  case arg
194
- when "-h", "--help" then @args[:help] = true
195
- when "-v", "--version" then @args[:version] = true
196
- when "-f", "--format " then next_arg = :format
197
- when "-r", "--resource-dir" then next_arg = :resourceDir
198
- when "-n", "--no-build-ref" then @args[:suppressBuildRef] = true
199
- when "--index-basename" then next_arg = :indexBaseName
200
- when "-i", "--include" then next_arg = :includeRegexp
201
- when "-j", "--exclude" then next_arg = :excludeRegexp
202
- when "-g", "--git-branches" then next_arg = :gitBranchRegexp
203
- when "-t", "--git-tags" then next_arg = :gitTagRegexp
204
- when "-c", "--local-only" then @args[:localRepoOnly] = true
205
- when "-a", "--attribute" then next_arg = :attributes
206
- when "-d", "--resolve-docid" then @args[:resolveDocid] = true
207
- when "-m", "--make-searchable" then @args[:makeSearchable] = true
208
- when "-mp", "--search-assets-deploy" then next_arg = :searchAssetsDeploy
209
- when "-s", "--style" then next_arg = :userStyle
210
- when "-w", "--web-path" then next_arg = :webPath
211
- when "--log-level" then next_arg = :logLevel
212
- else
213
- if next_arg
214
- if next_arg == :attributes
215
- # support multiple invocations of -a
216
- add_attribute arg
217
- else
218
- @args[next_arg] = arg
219
- end
220
- unflagged_args.delete(next_arg)
194
+ when "-h", "--help" then @args[:help] = true
195
+ when "-v", "--version" then @args[:version] = true
196
+ when "-f", "--format " then next_arg = :format
197
+ when "-r", "--resource-dir" then next_arg = :resourceDir
198
+ when "-n", "--no-build-ref" then @args[:suppressBuildRef] = true
199
+ when "--index-basename" then next_arg = :indexBaseName
200
+ when "-i", "--include" then next_arg = :includeRegexp
201
+ when "-j", "--exclude" then next_arg = :excludeRegexp
202
+ when "-g", "--git-branches" then next_arg = :gitBranchRegexp
203
+ when "-t", "--git-tags" then next_arg = :gitTagRegexp
204
+ when "-c", "--local-only" then @args[:localRepoOnly] = true
205
+ when "-a", "--attribute" then next_arg = :attributes
206
+ when "-d", "--resolve-docid" then @args[:resolveDocid] = true
207
+ when "-m", "--make-searchable" then @args[:makeSearchable] = true
208
+ when "-mp", "--search-assets-deploy" then next_arg = :searchAssetsDeploy
209
+ when "-s", "--style" then next_arg = :userStyle
210
+ when "-w", "--web-path" then next_arg = :webPath
211
+ when "--log-level" then next_arg = :logLevel
212
+ else
213
+ if next_arg
214
+ if next_arg == :attributes
215
+ # support multiple invocations of -a
216
+ add_attribute arg
217
+ else
218
+ @args[next_arg] = arg
221
219
  end
222
- next_arg = unflagged_args.first
220
+ unflagged_args.delete(next_arg)
221
+ end
222
+ next_arg = unflagged_args.first
223
223
  end
224
224
  end
225
225
  end
@@ -227,7 +227,7 @@ ENDHELP
227
227
  # adds the str (must be in key=value format) to the
228
228
  # user defined attributes
229
229
  def add_attribute(attrib_str)
230
- kv = attrib_str.split('=')
230
+ kv = attrib_str.split("=")
231
231
  if kv.length != 2
232
232
  puts "Invalid attribute format: #{attrib_str} Must be <key>=<value>"
233
233
  exit 1
@@ -272,7 +272,7 @@ ENDHELP
272
272
  # The user wants to parse a git repo, check that the srcDirRoot is within a
273
273
  # git repo if the user wants to generate git-branch specific docs
274
274
  @args[:gitRepoRoot] = Giblish::PathManager.find_gitrepo_root(
275
- @args[:srcDirRoot]
275
+ @args[:srcDirRoot]
276
276
  )
277
277
  return unless @args[:gitRepoRoot].nil?
278
278
 
data/lib/giblish/core.rb CHANGED
@@ -11,11 +11,9 @@ require_relative "docinfo"
11
11
  require_relative "buildgraph"
12
12
 
13
13
  module Giblish
14
-
15
14
  # Parse a directory tree and convert all asciidoc files matching the
16
15
  # supplied critera to the supplied format
17
16
  class FileTreeConverter
18
-
19
17
  attr_reader :converter
20
18
 
21
19
  # Required options:
@@ -26,29 +24,29 @@ module Giblish
26
24
  @options = options.dup
27
25
 
28
26
  @paths = Giblish::PathManager.new(
29
- @options[:srcDirRoot],
30
- @options[:dstDirRoot],
31
- @options[:resourceDir],
32
- @options[:makeSearchable]
27
+ @options[:srcDirRoot],
28
+ @options[:dstDirRoot],
29
+ @options[:resourceDir],
30
+ @options[:makeSearchable]
33
31
  )
34
32
 
35
33
  # set the path to the search data that will be sent to the cgi search script
36
34
  deploy_search_path = if @options[:makeSearchable]
37
- @options[:searchAssetsDeploy].nil? ?
38
- @paths.search_assets_abs : Pathname.new(@options[:searchAssetsDeploy]).join("search_assets")
39
- else
40
- nil
41
- end
35
+ if @options[:searchAssetsDeploy].nil?
36
+ @paths.search_assets_abs
37
+ else
38
+ Pathname.new(@options[:searchAssetsDeploy]).join("search_assets")
39
+ end
40
+ end
42
41
 
43
42
  @deploy_info = Giblish::DeploymentPaths.new(
44
- @options[:webPath],
45
- deploy_search_path
43
+ @options[:webPath],
44
+ deploy_search_path
46
45
  )
47
46
  @processed_docs = []
48
47
  @converter = converter_factory
49
48
  end
50
49
 
51
-
52
50
  # convert all adoc files
53
51
  # return true if all conversions went ok, false if at least one
54
52
  # failed
@@ -63,17 +61,20 @@ module Giblish
63
61
  # traverse the src file tree and convert all files deemed as
64
62
  # adoc files
65
63
  conv_error = false
66
- Find.find(@paths.src_root_abs) do |path|
67
- p = Pathname.new(path)
68
- begin
69
- to_asciidoc(p) if adocfile? p
70
- rescue Exception => e
71
- str = "Error when converting file #{path.to_s}: #{e.message}\nBacktrace:\n"
72
- e.backtrace.each { |l| str << " #{l}\n" }
73
- Giblog.logger.error { str }
74
- conv_error = true
64
+ if @paths.src_root_abs.directory?
65
+ Find.find(@paths.src_root_abs) do |path|
66
+ p = Pathname.new(path)
67
+ begin
68
+ to_asciidoc(p) if adocfile? p
69
+ rescue StandardError => e
70
+ str = String.new("Error when converting file "\
71
+ "#{path}: #{e.message}\nBacktrace:\n")
72
+ e.backtrace.each { |l| str << " #{l}\n" }
73
+ Giblog.logger.error { str }
74
+ conv_error = true
75
+ end
75
76
  end
76
- end if @paths.src_root_abs.directory?
77
+ end
77
78
 
78
79
  # create necessary search assets if needed
79
80
  create_search_assets if @options[:makeSearchable]
@@ -96,16 +97,14 @@ module Giblish
96
97
  adoc_logger = Giblish::AsciidoctorLogger.new Logger::Severity::WARN
97
98
  gb = graph_builder_factory
98
99
  errors = @converter.convert_str(
99
- gb.source(
100
- @options[:makeSearchable]
101
- ),
102
- @paths.dst_root_abs,
103
- "graph",
104
- logger: adoc_logger
100
+ gb.source(make_searchable: @options[:makeSearchable]),
101
+ @paths.dst_root_abs,
102
+ "graph",
103
+ logger: adoc_logger
105
104
  )
106
105
  gb.cleanup
107
106
  !errors
108
- rescue Exception => e
107
+ rescue StandardError => e
109
108
  Giblog.logger.warn { e.message }
110
109
  Giblog.logger.warn { "The dependency graph will not be generated !!" }
111
110
  end
@@ -117,12 +116,13 @@ module Giblish
117
116
  adoc_logger = Giblish::AsciidoctorLogger.new Logger::Severity::WARN
118
117
  ib = index_factory
119
118
  @converter.convert_str(
120
- ib.source(
121
- dep_graph_exist, @options[:makeSearchable]
122
- ),
123
- @paths.dst_root_abs,
124
- @options[:indexBaseName],
125
- logger: adoc_logger
119
+ ib.source(
120
+ dep_graph_exists: dep_graph_exist,
121
+ make_searchable: @options[:makeSearchable]
122
+ ),
123
+ @paths.dst_root_abs,
124
+ @options[:indexBaseName],
125
+ logger: adoc_logger
126
126
  )
127
127
 
128
128
  # clean up cached files and adoc resources
@@ -133,6 +133,7 @@ module Giblish
133
133
  # user options
134
134
  def index_factory
135
135
  raise "Internal logic error!" if @options[:suppressBuildRef]
136
+
136
137
  SimpleIndexBuilder.new(@processed_docs, @converter, @paths, @deploy_info,
137
138
  @options[:resolveDocid])
138
139
  end
@@ -145,9 +146,9 @@ module Giblish
145
146
  # get the correct converter type
146
147
  def converter_factory
147
148
  case @options[:format]
148
- when "html" then
149
+ when "html"
149
150
  HtmlConverter.new @paths, @deploy_info, @options
150
- when "pdf" then
151
+ when "pdf"
151
152
  PdfConverter.new @paths, @deploy_info, @options
152
153
  else
153
154
  raise ArgumentError, "Unknown conversion format: #{@options[:format]}"
@@ -184,16 +185,13 @@ module Giblish
184
185
 
185
186
  # convert a single adoc doc to whatever the user wants
186
187
  def to_asciidoc(filepath)
187
- adoc = nil
188
- begin
189
- adoc_logger = Giblish::AsciidoctorLogger.new Logger::Severity::WARN
190
- adoc = @converter.convert(filepath, logger: adoc_logger)
188
+ adoc_logger = Giblish::AsciidoctorLogger.new Logger::Severity::WARN
189
+ adoc = @converter.convert(filepath, logger: adoc_logger)
191
190
 
192
- add_doc(adoc, adoc_logger.user_info_str.string)
193
- rescue Exception => e
194
- add_doc_fail(filepath, e)
195
- raise
196
- end
191
+ add_doc(adoc, adoc_logger.user_info_str.string)
192
+ rescue StandardError => e
193
+ add_doc_fail(filepath, e)
194
+ raise
197
195
  end
198
196
 
199
197
  # predicate that decides if a path is a asciidoc file or not
@@ -207,7 +205,7 @@ module Giblish
207
205
 
208
206
  # only include files matching the include regexp
209
207
  ir = Regexp.new @options[:includeRegexp]
210
- return !ir.match(fs).nil?
208
+ !ir.match(fs).nil?
211
209
  end
212
210
 
213
211
  def manage_searchability(opts)
@@ -218,15 +216,12 @@ module Giblish
218
216
  IndexHeadings.clear_index
219
217
 
220
218
  # propagate user-given id attributes to the indexing class
219
+ # if there are any
221
220
  attr = opts[:attributes]
222
- if !attr.nil?
223
- if attr.has_key?("idprefix")
224
- IndexHeadings.id_elements[:id_prefix] = attr["idprefix"]
225
- end
226
- if attr.has_key?("idseparator")
227
- IndexHeadings.id_elements[:id_separator] = attr["idseparator"]
228
- end
229
- end
221
+ return if attr.nil?
222
+
223
+ IndexHeadings.id_elements[:id_prefix] = attr["idprefix"] if attr.key?("idprefix")
224
+ IndexHeadings.id_elements[:id_separator] = attr["idseparator"] if attr.key?("idseparator")
230
225
  end
231
226
 
232
227
  # top_dir
@@ -256,6 +251,8 @@ module Giblish
256
251
 
257
252
  # traverse the src file tree and copy all published adoc files
258
253
  # to the search_assets dir
254
+ return unless @paths.src_root_abs.directory?
255
+
259
256
  Find.find(@paths.src_root_abs) do |path|
260
257
  p = Pathname.new(path)
261
258
  next unless adocfile? p
@@ -263,7 +260,7 @@ module Giblish
263
260
  dst_dir = assets_dir.join(@paths.reldir_from_src_root(p))
264
261
  FileUtils.mkdir_p(dst_dir)
265
262
  FileUtils.cp(p.to_s, dst_dir)
266
- end if @paths.src_root_abs.directory?
263
+ end
267
264
  end
268
265
 
269
266
  # Register the asciidoctor extension that handles doc ids and traverse
@@ -280,14 +277,17 @@ module Giblish
280
277
 
281
278
  # traverse the src file tree and collect ids from all
282
279
  # .adoc or .ADOC files
283
- Find.find(@paths.src_root_abs) do |path|
284
- p = Pathname.new(path)
285
- idc.parse_file(p) if adocfile? p
286
- end if @paths.src_root_abs.directory?
280
+ if @paths.src_root_abs.directory?
281
+ Find.find(@paths.src_root_abs) do |path|
282
+ p = Pathname.new(path)
283
+ idc.parse_file(p) if adocfile? p
284
+ end
285
+ end
287
286
  idc
288
287
  end
289
288
  end
290
289
 
290
+ # Converts all adoc files within a git repo
291
291
  class GitRepoConverter < FileTreeConverter
292
292
  def initialize(options)
293
293
  super(options)
@@ -308,7 +308,13 @@ module Giblish
308
308
  def convert
309
309
  conv_error = false
310
310
  (@user_branches + @user_tags).each do |co|
311
- conv_error = conv_error || convert_one_checkout(co)
311
+ has_error = convert_one_checkout(co)
312
+ if has_error == true
313
+ conv_error = true
314
+ end
315
+ rescue
316
+ conv_error = true
317
+ next
312
318
  end
313
319
 
314
320
  # Render the summary page
@@ -316,10 +322,10 @@ module Giblish
316
322
  @user_branches,
317
323
  @user_tags
318
324
 
319
- conv_error = conv_error || @converter.convert_str(
320
- index_builder.source,
321
- @master_paths.dst_root_abs,
322
- "index"
325
+ conv_error ||= @converter.convert_str(
326
+ index_builder.source,
327
+ @master_paths.dst_root_abs,
328
+ "index"
323
329
  )
324
330
 
325
331
  # clean up
@@ -366,7 +372,7 @@ module Giblish
366
372
  # Connect to the git repo
367
373
  begin
368
374
  git_repo = Git.open(git_repo_root)
369
- rescue Exception => e
375
+ rescue StandardError => e
370
376
  raise "Could not find a git repo at #{git_repo_root} !"\
371
377
  "\n\n(#{e.message})"
372
378
  end
@@ -374,7 +380,7 @@ module Giblish
374
380
  # fetch all remote refs if ok with user
375
381
  begin
376
382
  git_repo.fetch unless local_only
377
- rescue Exception => e
383
+ rescue StandardError => e
378
384
  raise "Could not fetch from origin"\
379
385
  "(do you need '--local-only'?)!\n\n(#{e.message})"
380
386
  end
@@ -398,30 +404,29 @@ module Giblish
398
404
  return [] unless tag_regexp
399
405
 
400
406
  regexp = Regexp.new @options[:gitTagRegexp]
401
- tags = @git_repo.tags.select do |t|
407
+ @git_repo.tags.select do |t|
402
408
  regexp.match t.name
403
409
  end
404
- tags
405
410
  end
406
411
 
407
412
  # convert all docs from one particular git commit
408
413
  # returns true if at least one doc failed to convert
409
414
  # and false if everything went ok.
410
- def convert_one_checkout(co)
415
+ def convert_one_checkout(checkout)
411
416
  # determine if we are called with a tag or a branch
412
- is_tag = (co.respond_to?(:tag?) && co.tag?)
417
+ is_tag = (checkout.respond_to?(:tag?) && checkout.tag?)
413
418
 
414
- Giblog.logger.info { "Checking out #{co.name}" }
415
- @git_repo.checkout co.name
419
+ Giblog.logger.info { "Checking out #{checkout.name}" }
420
+ @git_repo.checkout checkout.name
416
421
 
417
422
  unless is_tag
418
423
  # if this is a branch, make sure it is up-to-date
419
- Giblog.logger.info { "Merging with origin/#{co.name}" }
420
- @git_repo.merge "origin/#{co.name}"
424
+ Giblog.logger.info { "Merging with origin/#{checkout.name}" }
425
+ @git_repo.merge "origin/#{checkout.name}"
421
426
  end
422
427
 
423
428
  # assign a checkout-unique dst-dir
424
- dir_name = co.name.tr("/", "_") << "/"
429
+ dir_name = checkout.name.tr("/", "_") << "/"
425
430
 
426
431
  # Update needed base class members before converting a new checkout
427
432
  @processed_docs = []
@@ -436,7 +441,11 @@ module Giblish
436
441
  # Parse and convert docs using given args
437
442
  Giblog.logger.info { "Convert docs into dir #{@paths.dst_root_abs}" }
438
443
  # parent_convert
439
- FileTreeConverter.instance_method(:convert).bind(self).call
444
+ begin
445
+ FileTreeConverter.instance_method(:convert).bind_call(self)
446
+ rescue => e
447
+ raise "convert_one_checkout has a failure with #{co}!\n\n(#{e.message})"
448
+ end
440
449
  end
441
450
  end
442
- end
451
+ end