yard_ghurt 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -2
- data/Gemfile +0 -18
- data/README.md +28 -15
- data/Rakefile +24 -37
- data/bin/yard_ghurt +5 -16
- data/lib/yard_ghurt.rb +22 -34
- data/lib/yard_ghurt/anchor_links.rb +98 -107
- data/lib/yard_ghurt/gfm_fix_task.rb +210 -208
- data/lib/yard_ghurt/ghp_sync_task.rb +46 -56
- data/lib/yard_ghurt/util.rb +83 -33
- data/lib/yard_ghurt/version.rb +4 -16
- data/yard_ghurt.gemspec +18 -29
- metadata +23 -23
@@ -1,23 +1,11 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
1
|
# encoding: UTF-8
|
3
2
|
# frozen_string_literal: true
|
4
3
|
|
5
4
|
#--
|
6
5
|
# This file is part of YardGhurt.
|
7
|
-
# Copyright (c) 2019 Jonathan Bradley Whited
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# it under the terms of the GNU Lesser General Public License as published by
|
11
|
-
# the Free Software Foundation, either version 3 of the License, or
|
12
|
-
# (at your option) any later version.
|
13
|
-
#
|
14
|
-
# YardGhurt is distributed in the hope that it will be useful,
|
15
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
-
# GNU Lesser General Public License for more details.
|
18
|
-
#
|
19
|
-
# You should have received a copy of the GNU Lesser General Public License
|
20
|
-
# along with YardGhurt. If not, see <https://www.gnu.org/licenses/>.
|
6
|
+
# Copyright (c) 2019-2021 Jonathan Bradley Whited
|
7
|
+
#
|
8
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
21
9
|
#++
|
22
10
|
|
23
11
|
|
@@ -33,29 +21,29 @@ module YardGhurt
|
|
33
21
|
###
|
34
22
|
# Fix (find & replace) text in the GitHub Flavored Markdown (GFM) files in the YARDoc directory,
|
35
23
|
# for differences between the two formats.
|
36
|
-
#
|
24
|
+
#
|
37
25
|
# You can set {dry_run} on the command line:
|
38
26
|
# rake yard_gfm_fix dryrun=true
|
39
|
-
#
|
27
|
+
#
|
40
28
|
# @example What I Use
|
41
29
|
# YardGhurt::GFMFixTask.new() do |task|
|
42
30
|
# task.arg_names = [:dev]
|
43
31
|
# task.dry_run = false
|
44
32
|
# task.fix_code_langs = true
|
45
33
|
# task.md_files = ['index.html']
|
46
|
-
#
|
47
|
-
# task.before = Proc.new() do |
|
34
|
+
#
|
35
|
+
# task.before = Proc.new() do |t2,args|
|
48
36
|
# # Delete this file as it's never used (index.html is an exact copy)
|
49
|
-
# YardGhurt.rm_exist(File.join(
|
50
|
-
#
|
37
|
+
# YardGhurt.rm_exist(File.join(t2.doc_dir,'file.README.html'))
|
38
|
+
#
|
51
39
|
# # Root dir of my GitHub Page for CSS/JS
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
40
|
+
# ghp_root_dir = YardGhurt.to_bool(args.dev) ? '../../esotericpig.github.io' : '../../..'
|
41
|
+
#
|
42
|
+
# t2.css_styles << %Q(<link rel="stylesheet" type="text/css" href="#{ghp_root_dir}/css/prism.css" />)
|
43
|
+
# t2.js_scripts << %Q(<script src="#{ghp_root_dir}/js/prism.js"></script>)
|
56
44
|
# end
|
57
45
|
# end
|
58
|
-
#
|
46
|
+
#
|
59
47
|
# @example Using All Options
|
60
48
|
# YardGhurt::GFMFixTask.new(:yard_fix) do |task|
|
61
49
|
# task.anchor_db = {'tests' => 'Testing'} # #tests => #Testing
|
@@ -76,192 +64,192 @@ module YardGhurt
|
|
76
64
|
# task.js_scripts << '<script>document.write("Hello World!");</script>'
|
77
65
|
# task.md_files = ['index.html']
|
78
66
|
# task.verbose = false
|
79
|
-
#
|
67
|
+
#
|
80
68
|
# task.before = Proc.new() {|task,args| puts "Hi, #{args.name}!"}
|
81
69
|
# task.during = Proc.new() {|task,args,file| puts "#{args.name} can haz #{file}?"}
|
82
70
|
# task.after = Proc.new() {|task,args| puts "Goodbye, #{args.name}!"}
|
83
71
|
# end
|
84
|
-
#
|
85
|
-
# @author Jonathan Bradley Whited
|
72
|
+
#
|
73
|
+
# @author Jonathan Bradley Whited
|
86
74
|
# @since 1.1.0
|
87
75
|
###
|
88
76
|
class GFMFixTask < Rake::TaskLib
|
89
77
|
# This is important so that a subsequent call to this task will not write the CSS again.
|
90
|
-
#
|
78
|
+
#
|
91
79
|
# @return [String] the comment tag of where to place {css_styles}
|
92
|
-
#
|
80
|
+
#
|
93
81
|
# @see add_css_styles!
|
94
82
|
CSS_COMMENT = "<!-- #{self} CSS - Do NOT remove this comment! -->"
|
95
|
-
|
83
|
+
|
96
84
|
# This is important so that a subsequent call to this task will not write the JS again.
|
97
|
-
#
|
85
|
+
#
|
98
86
|
# @return [String] the comment tag of where to place {js_scripts}
|
99
|
-
#
|
87
|
+
#
|
100
88
|
# @see add_js_scripts!
|
101
89
|
JS_COMMENT = "<!-- #{self} JS - Do NOT remove this comment! -->"
|
102
|
-
|
90
|
+
|
103
91
|
# @example
|
104
92
|
# task.arg_names = [:dev]
|
105
|
-
#
|
93
|
+
#
|
106
94
|
# # @param task [self]
|
107
95
|
# # @param args [Rake::TaskArguments] the args specified by {arg_names}
|
108
96
|
# task.after = Proc.new do |task,args|
|
109
97
|
# puts args.dev
|
110
98
|
# end
|
111
|
-
#
|
99
|
+
#
|
112
100
|
# @return [Proc,nil] the Proc ( +respond_to?(:call)+ ) to call at the end of this task or +nil+;
|
113
101
|
# default: +nil+
|
114
102
|
attr_accessor :after
|
115
|
-
|
103
|
+
|
116
104
|
# The anchor links to override in the database.
|
117
|
-
#
|
105
|
+
#
|
118
106
|
# The keys are GFM anchor IDs and the values are their equivalent YARDoc anchor IDs.
|
119
|
-
#
|
107
|
+
#
|
120
108
|
# @return [Hash] the custom database (key-value pairs) of GFM anchor links to YARDoc anchor links;
|
121
109
|
# default: +{}+
|
122
|
-
#
|
110
|
+
#
|
123
111
|
# @see build_anchor_links_db
|
124
112
|
# @see AnchorLinks#merge_anchor_ids!
|
125
113
|
attr_accessor :anchor_db
|
126
|
-
|
114
|
+
|
127
115
|
# @return [Array<Symbol>,Symbol] the custom arg(s) for this task; default: +[]+
|
128
116
|
attr_accessor :arg_names
|
129
|
-
|
117
|
+
|
130
118
|
# @example
|
131
119
|
# task.arg_names = [:dev]
|
132
|
-
#
|
120
|
+
#
|
133
121
|
# # @param task [self]
|
134
122
|
# # @param args [Rake::TaskArguments] the args specified by {arg_names}
|
135
123
|
# task.before = Proc.new do |task,args|
|
136
124
|
# puts args.dev
|
137
125
|
# end
|
138
|
-
#
|
126
|
+
#
|
139
127
|
# @return [Proc,nil] the Proc ( +respond_to?(:call)+ ) to call at the beginning of this task or +nil+;
|
140
128
|
# default: +nil+
|
141
129
|
attr_accessor :before
|
142
|
-
|
130
|
+
|
143
131
|
# @example
|
144
132
|
# task.css_styles << '<link rel="stylesheet" type="text/css" href="css/prism.css" />'
|
145
|
-
#
|
133
|
+
#
|
146
134
|
# @return [Array<String>] the CSS styles to add to each file; default: +[]+
|
147
135
|
attr_accessor :css_styles
|
148
|
-
|
136
|
+
|
149
137
|
# @example
|
150
138
|
# # +gsub!()+ (and other mutable methods) must be used
|
151
139
|
# # as the return value must be +true+ or +false+.
|
152
|
-
# #
|
140
|
+
# #
|
153
141
|
# # @param line [String] the current line being processed from the current file
|
154
|
-
# #
|
142
|
+
# #
|
155
143
|
# # @return [true,false] whether there was a change
|
156
144
|
# task.custom_gsub = Proc.new do |line|
|
157
145
|
# has_change = false
|
158
|
-
#
|
146
|
+
#
|
159
147
|
# has_change = !line.gsub!('dev','prod').nil?() || has_change
|
160
148
|
# # More changes...
|
161
|
-
#
|
149
|
+
#
|
162
150
|
# return has_change
|
163
151
|
# end
|
164
|
-
#
|
152
|
+
#
|
165
153
|
# @return [Proc,nil] the custom Proc ( +respond_to?(:call)+ ) to call to gsub! each line for each file
|
166
154
|
attr_accessor :custom_gsub
|
167
|
-
|
155
|
+
|
168
156
|
# @example
|
169
157
|
# task.custom_gsubs = [
|
170
158
|
# ['dev','prod'],
|
171
159
|
# [/href="#[^"]*"/,'href="#contents"']
|
172
160
|
# ]
|
173
|
-
#
|
161
|
+
#
|
174
162
|
# # Internal code:
|
175
163
|
# # ---
|
176
164
|
# # @custom_gsubs.each do |custom_gsub|
|
177
165
|
# # line.gsub!(custom_gsub[0],custom_gsub[1])
|
178
166
|
# # end
|
179
|
-
#
|
167
|
+
#
|
180
168
|
# @return [Array<[Regexp,String]>] the custom args to use in gsub on each line for each file
|
181
169
|
attr_accessor :custom_gsubs
|
182
|
-
|
170
|
+
|
183
171
|
# @example
|
184
172
|
# task.deps = :yard
|
185
173
|
# # or...
|
186
174
|
# task.deps = [:clobber,:yard]
|
187
|
-
#
|
175
|
+
#
|
188
176
|
# @return [Array<Symbol>,Symbol] the custom dependencies for this task; default: +[]+
|
189
177
|
attr_accessor :deps
|
190
|
-
|
178
|
+
|
191
179
|
# @return [String] the description of this task (customizable)
|
192
180
|
attr_accessor :description
|
193
|
-
|
181
|
+
|
194
182
|
# @return [String] the directory of generated YARDoc files; default: +doc+
|
195
183
|
attr_accessor :doc_dir
|
196
|
-
|
184
|
+
|
197
185
|
# @return [true,false] whether to run a dry run (no writing to the files); default: +false+
|
198
186
|
attr_accessor :dry_run
|
199
|
-
|
187
|
+
|
200
188
|
# @example
|
201
189
|
# task.arg_names = [:dev]
|
202
|
-
#
|
190
|
+
#
|
203
191
|
# # @param task [self]
|
204
192
|
# # @param args [Rake::TaskArguments] the args specified by {arg_names}
|
205
193
|
# # @param file [String] the current file being processed
|
206
194
|
# task.during = Proc.new do |task,args,file|
|
207
195
|
# puts args.dev
|
208
196
|
# end
|
209
|
-
#
|
197
|
+
#
|
210
198
|
# @return [Proc,nil] the Proc to call ( +respond_to?(:call)+ ) at the beginning of processing
|
211
199
|
# each file or +nil+; default: +nil+
|
212
200
|
attr_accessor :during
|
213
|
-
|
201
|
+
|
214
202
|
# @return [Set<String>] the case-sensitive code languages to not fix; default: +Set[ 'ruby' ]+
|
215
|
-
#
|
203
|
+
#
|
216
204
|
# @see fix_code_langs
|
217
205
|
attr_accessor :exclude_code_langs
|
218
|
-
|
206
|
+
|
219
207
|
# @return [true,false] whether to fix anchor links; default: +true+
|
220
208
|
attr_accessor :fix_anchor_links
|
221
|
-
|
209
|
+
|
222
210
|
# If +true+, +language-+ will be added to code classes, except for {exclude_code_langs}.
|
223
|
-
#
|
211
|
+
#
|
224
212
|
# For example, +code class="ruby"+ will be changed to +code class="language-ruby"+.
|
225
|
-
#
|
213
|
+
#
|
226
214
|
# @return [true,false] whether to fix code languages; default: +false+
|
227
215
|
attr_accessor :fix_code_langs
|
228
|
-
|
216
|
+
|
229
217
|
# If +true+, local file links (if the local file exists), will be changed to +file.{filename}.html+.
|
230
|
-
#
|
218
|
+
#
|
231
219
|
# This is useful for +README.md+, +LICENSE.txt+, etc.
|
232
|
-
#
|
220
|
+
#
|
233
221
|
# @return [true,false] whether to fix local file links; default: +true+
|
234
222
|
attr_accessor :fix_file_links
|
235
|
-
|
223
|
+
|
236
224
|
# This is an internal flag meant to be changed internally.
|
237
|
-
#
|
225
|
+
#
|
238
226
|
# @return [true,false] whether {CSS_COMMENT} has been seen/added; default: +false+
|
239
|
-
#
|
227
|
+
#
|
240
228
|
# @see add_css_styles!
|
241
229
|
attr_accessor :has_css_comment
|
242
|
-
|
230
|
+
|
243
231
|
# This is an internal flag meant to be changed internally.
|
244
|
-
#
|
232
|
+
#
|
245
233
|
# @return [true,false] whether {JS_COMMENT} has been seen/added; default: +false+
|
246
|
-
#
|
234
|
+
#
|
247
235
|
# @see add_js_scripts!
|
248
236
|
attr_accessor :has_js_comment
|
249
|
-
|
237
|
+
|
250
238
|
# @example
|
251
239
|
# task.js_scripts << '<script src="js/prism.js"></script>'
|
252
|
-
#
|
240
|
+
#
|
253
241
|
# @return [Array<String>] the JS scripts to add to each file; default: +[]+
|
254
242
|
attr_accessor :js_scripts
|
255
|
-
|
243
|
+
|
256
244
|
# @return [Array<String>] the (GFM) Markdown files to fix; default: +['file.README.html','index.html']+
|
257
245
|
attr_accessor :md_files
|
258
|
-
|
246
|
+
|
259
247
|
# @return [String] the name of this task (customizable); default: +yard_gfm_fix+
|
260
248
|
attr_accessor :name
|
261
|
-
|
249
|
+
|
262
250
|
# @return [true,false] whether to output each change to stdout; default: +true+
|
263
251
|
attr_accessor :verbose
|
264
|
-
|
252
|
+
|
265
253
|
alias_method :dry_run?,:dry_run
|
266
254
|
alias_method :fix_anchor_links?,:fix_anchor_links
|
267
255
|
alias_method :fix_code_langs?,:fix_code_langs
|
@@ -269,9 +257,11 @@ module YardGhurt
|
|
269
257
|
alias_method :has_css_comment?,:has_css_comment
|
270
258
|
alias_method :has_js_comment?,:has_js_comment
|
271
259
|
alias_method :verbose?,:verbose
|
272
|
-
|
260
|
+
|
273
261
|
# @param name [Symbol] the name of this task to use on the command line with +rake+
|
274
262
|
def initialize(name=:yard_gfm_fix)
|
263
|
+
super()
|
264
|
+
|
275
265
|
@after = nil
|
276
266
|
@anchor_db = {}
|
277
267
|
@arg_names = []
|
@@ -292,125 +282,137 @@ module YardGhurt
|
|
292
282
|
@md_files = ['file.README.html','index.html']
|
293
283
|
@name = name
|
294
284
|
@verbose = true
|
295
|
-
|
296
|
-
yield self if block_given?
|
297
|
-
define
|
285
|
+
|
286
|
+
yield self if block_given?
|
287
|
+
define
|
298
288
|
end
|
299
|
-
|
289
|
+
|
300
290
|
# Reset certain instance vars per file.
|
301
|
-
def reset_per_file
|
302
|
-
@anchor_links = AnchorLinks.new
|
291
|
+
def reset_per_file
|
292
|
+
@anchor_links = AnchorLinks.new
|
303
293
|
@has_css_comment = false
|
304
294
|
@has_js_comment = false
|
305
295
|
@has_verbose_anchor_links = false
|
306
296
|
end
|
307
|
-
|
297
|
+
|
308
298
|
# Define the Rake task and description using the instance variables.
|
309
|
-
def define
|
299
|
+
def define
|
310
300
|
desc @description
|
311
301
|
task @name,Array(@arg_names) => Array(@deps) do |task,args|
|
312
302
|
env_dryrun = ENV['dryrun']
|
313
|
-
|
314
|
-
if !env_dryrun.nil?
|
303
|
+
|
304
|
+
if !env_dryrun.nil? && !(env_dryrun = env_dryrun.to_s.strip).empty?
|
315
305
|
@dry_run = Util.to_bool(env_dryrun)
|
316
306
|
end
|
317
|
-
|
307
|
+
|
318
308
|
@before.call(self,args) if @before.respond_to?(:call)
|
319
|
-
|
309
|
+
|
320
310
|
@md_files.each do |md_file|
|
321
|
-
reset_per_file
|
311
|
+
reset_per_file
|
322
312
|
build_anchor_links_db(md_file)
|
323
|
-
|
313
|
+
|
324
314
|
@during.call(self,args,md_file) if @during.respond_to?(:call)
|
325
|
-
|
315
|
+
|
326
316
|
fix_md_file(md_file)
|
327
317
|
end
|
328
|
-
|
318
|
+
|
329
319
|
@after.call(self,args) if @after.respond_to?(:call)
|
330
320
|
end
|
331
|
-
|
321
|
+
|
332
322
|
return self
|
333
323
|
end
|
334
|
-
|
324
|
+
|
335
325
|
# Convert each HTML header tag in +md_file+ to a GFM & YARDoc anchor link
|
336
326
|
# and build a database using them.
|
337
|
-
#
|
327
|
+
#
|
338
328
|
# @param md_file [String] the file (no dir) to build the anchor links database with,
|
339
329
|
# will be joined to {doc_dir}
|
340
|
-
#
|
330
|
+
#
|
341
331
|
# @see AnchorLinks#<<
|
342
332
|
# @see AnchorLinks#merge_anchor_ids!
|
343
333
|
def build_anchor_links_db(md_file)
|
344
334
|
filename = File.join(@doc_dir,md_file)
|
345
|
-
|
335
|
+
|
346
336
|
return unless File.exist?(filename)
|
347
|
-
|
337
|
+
|
348
338
|
File.open(filename,'r') do |file|
|
349
339
|
file.each_line do |line|
|
350
|
-
|
351
|
-
|
340
|
+
# +<h3 id="..."+ or +<h3...+
|
341
|
+
# - +yard_id+ was added for YARD v0.9.25+.
|
342
|
+
match = line.match(/<\s*h\d+.*?id\s*=\s*["'](?<yard_id>[^"']+)["']|<\s*h\d+[\s>]/ui)
|
343
|
+
|
344
|
+
next unless match
|
345
|
+
|
346
|
+
yard_id = nil
|
347
|
+
caps = match.named_captures
|
348
|
+
|
349
|
+
if caps.key?('yard_id')
|
350
|
+
yard_id = caps['yard_id'].to_s.strip
|
351
|
+
yard_id = nil if yard_id.empty?
|
352
|
+
end
|
353
|
+
|
352
354
|
line.gsub!(/<[^>]+>/,'') # Remove tags: <...>
|
353
|
-
line.strip!
|
354
|
-
|
355
|
-
next if line.empty?
|
356
|
-
|
357
|
-
@anchor_links
|
355
|
+
line.strip!
|
356
|
+
|
357
|
+
next if line.empty?
|
358
|
+
|
359
|
+
@anchor_links.add_anchor(line,yard_id: yard_id)
|
358
360
|
end
|
359
361
|
end
|
360
|
-
|
362
|
+
|
361
363
|
@anchor_links.merge_anchor_ids!(@anchor_db)
|
362
364
|
end
|
363
|
-
|
365
|
+
|
364
366
|
# Fix (find & replace) text in +md_file+. Calls all +add_*+ & +gsub_*+ methods.
|
365
|
-
#
|
367
|
+
#
|
366
368
|
# @param md_file [String] the file (no dir) to fix, will be joined to {doc_dir}
|
367
369
|
def fix_md_file(md_file)
|
368
370
|
filename = File.join(@doc_dir,md_file)
|
369
|
-
|
371
|
+
|
370
372
|
puts "[#{filename}]:"
|
371
|
-
|
373
|
+
|
372
374
|
if !File.exist?(filename)
|
373
375
|
puts '! File does not exist'
|
374
|
-
|
376
|
+
|
375
377
|
return
|
376
378
|
end
|
377
|
-
|
379
|
+
|
378
380
|
changes = 0
|
379
381
|
lines = []
|
380
|
-
|
382
|
+
|
381
383
|
File.open(filename,'r') do |file|
|
382
384
|
file.each_line do |line|
|
383
|
-
if line.strip
|
385
|
+
if line.strip.empty?
|
384
386
|
lines << line
|
385
|
-
|
387
|
+
|
386
388
|
next
|
387
389
|
end
|
388
|
-
|
390
|
+
|
389
391
|
has_change = false
|
390
|
-
|
392
|
+
|
391
393
|
# Standard
|
392
394
|
has_change = add_css_styles!(line) || has_change
|
393
395
|
has_change = add_js_scripts!(line) || has_change
|
394
396
|
has_change = gsub_anchor_links!(line) || has_change
|
395
397
|
has_change = gsub_code_langs!(line) || has_change
|
396
398
|
has_change = gsub_local_file_links!(line) || has_change
|
397
|
-
|
399
|
+
|
398
400
|
# Custom
|
399
401
|
has_change = gsub_customs!(line) || has_change
|
400
402
|
has_change = gsub_custom!(line) || has_change
|
401
|
-
|
403
|
+
|
402
404
|
if has_change
|
403
405
|
puts "+ #{line}" if @verbose
|
404
|
-
|
406
|
+
|
405
407
|
changes += 1
|
406
408
|
end
|
407
|
-
|
409
|
+
|
408
410
|
lines << line
|
409
411
|
end
|
410
412
|
end
|
411
|
-
|
413
|
+
|
412
414
|
print '= '
|
413
|
-
|
415
|
+
|
414
416
|
if changes > 0
|
415
417
|
if @dry_run
|
416
418
|
puts 'Nothing written (dry run)'
|
@@ -418,195 +420,195 @@ module YardGhurt
|
|
418
420
|
File.open(filename,'w') do |file|
|
419
421
|
file.puts lines
|
420
422
|
end
|
421
|
-
|
423
|
+
|
422
424
|
puts "#{changes} changes written"
|
423
425
|
end
|
424
426
|
else
|
425
427
|
puts 'Nothing written (up-to-date)'
|
426
428
|
end
|
427
429
|
end
|
428
|
-
|
430
|
+
|
429
431
|
# Add {CSS_COMMENT} & {css_styles} to +line+ if it is +</head>+,
|
430
432
|
# unless {CSS_COMMENT} has already been found or {has_css_comment}.
|
431
|
-
#
|
433
|
+
#
|
432
434
|
# @param line [String] the line from the file to check if +</head>+
|
433
435
|
def add_css_styles!(line)
|
434
|
-
return false if @has_css_comment || @css_styles.empty?
|
435
|
-
|
436
|
-
if line.strip
|
436
|
+
return false if @has_css_comment || @css_styles.empty?
|
437
|
+
|
438
|
+
if line.strip == CSS_COMMENT
|
437
439
|
@has_css_comment = true
|
438
|
-
|
440
|
+
|
439
441
|
return false
|
440
442
|
end
|
441
|
-
|
442
|
-
return false unless line =~
|
443
|
-
|
444
|
-
line.slice!(0,line.length
|
443
|
+
|
444
|
+
return false unless line =~ %r{^\s*</head>\s*$}i
|
445
|
+
|
446
|
+
line.slice!(0,line.length)
|
445
447
|
line << " #{CSS_COMMENT}"
|
446
|
-
|
448
|
+
|
447
449
|
@css_styles.each do |css_style|
|
448
450
|
line << "\n #{css_style}"
|
449
451
|
end
|
450
|
-
|
452
|
+
|
451
453
|
line << "\n\n </head>"
|
452
|
-
|
454
|
+
|
453
455
|
@has_css_comment = true
|
454
|
-
|
456
|
+
|
455
457
|
return true
|
456
458
|
end
|
457
|
-
|
459
|
+
|
458
460
|
# Add {JS_COMMENT} & {js_scripts} to +line+ if it is +</body>+,
|
459
461
|
# unless {JS_COMMENT} has already been found or {has_js_comment}.
|
460
|
-
#
|
462
|
+
#
|
461
463
|
# @param line [String] the line from the file to check if +</body>+
|
462
464
|
def add_js_scripts!(line)
|
463
|
-
return false if @has_js_comment || @js_scripts.empty?
|
464
|
-
|
465
|
-
if line.strip
|
465
|
+
return false if @has_js_comment || @js_scripts.empty?
|
466
|
+
|
467
|
+
if line.strip == JS_COMMENT
|
466
468
|
@has_js_comment = true
|
467
|
-
|
469
|
+
|
468
470
|
return false
|
469
471
|
end
|
470
|
-
|
471
|
-
return false unless line =~
|
472
|
-
|
473
|
-
line.slice!(0,line.length
|
472
|
+
|
473
|
+
return false unless line =~ %r{^\s*</body>\s*$}i
|
474
|
+
|
475
|
+
line.slice!(0,line.length)
|
474
476
|
line << "\n #{JS_COMMENT}"
|
475
|
-
|
477
|
+
|
476
478
|
@js_scripts.each do |js_script|
|
477
479
|
line << "\n #{js_script}"
|
478
480
|
end
|
479
|
-
|
481
|
+
|
480
482
|
line << "\n\n </body>"
|
481
|
-
|
483
|
+
|
482
484
|
@has_js_comment = true
|
483
|
-
|
485
|
+
|
484
486
|
return true
|
485
487
|
end
|
486
|
-
|
488
|
+
|
487
489
|
# Replace GFM anchor links with their equivalent YARDoc anchor links,
|
488
490
|
# using {build_anchor_links_db} & {anchor_db}, if {fix_anchor_links}.
|
489
|
-
#
|
491
|
+
#
|
490
492
|
# @param line [String] the line from the file to fix
|
491
493
|
def gsub_anchor_links!(line)
|
492
494
|
return false unless @fix_anchor_links
|
493
|
-
|
495
|
+
|
494
496
|
has_change = false
|
495
|
-
|
496
|
-
|
497
|
-
line.gsub!(
|
498
|
-
link =
|
499
|
-
|
500
|
-
if link.empty?
|
497
|
+
|
498
|
+
# +href="#..."+ or +href='#...'+
|
499
|
+
line.gsub!(/href\s*=\s*["']\s*#\s*(?<link>[^"']+)["']/ui) do |href|
|
500
|
+
link = Regexp.last_match[:link].to_s.strip # Same as +$~[:link]+.
|
501
|
+
|
502
|
+
if link.empty? || @anchor_links.yard_anchor_id?(link)
|
501
503
|
href
|
502
504
|
else
|
503
505
|
yard_link = @anchor_links[link]
|
504
|
-
|
505
|
-
if yard_link.nil?
|
506
|
+
|
507
|
+
if yard_link.nil?
|
506
508
|
# Either the GFM link is wrong [check with @anchor_links.to_github_anchor_id()]
|
507
509
|
# or the internal code is broken [check with @anchor_links.to_s()]
|
508
510
|
puts "! YARDoc anchor link for GFM anchor link [#{link}] does not exist"
|
509
|
-
|
511
|
+
|
510
512
|
if !@has_verbose_anchor_links
|
511
513
|
if @verbose
|
512
514
|
puts ' GFM anchor link in the Markdown file is wrong?'
|
513
515
|
puts ' Please check the generated links:'
|
514
|
-
puts %Q( #{@anchor_links.to_s
|
516
|
+
puts %Q( #{@anchor_links.to_s.strip.gsub("\n","\n ")})
|
515
517
|
else
|
516
518
|
puts " Turn on #{self.class}.verbose for more info"
|
517
519
|
end
|
518
|
-
|
520
|
+
|
519
521
|
@has_verbose_anchor_links = true
|
520
522
|
end
|
521
|
-
|
523
|
+
|
522
524
|
href
|
523
525
|
else
|
524
526
|
has_change = true
|
525
|
-
|
526
|
-
%Q(
|
527
|
+
|
528
|
+
%Q(href="##{yard_link}")
|
527
529
|
end
|
528
530
|
end
|
529
531
|
end
|
530
|
-
|
532
|
+
|
531
533
|
return has_change
|
532
534
|
end
|
533
|
-
|
535
|
+
|
534
536
|
# Add +language-+ to code class languages and down case them,
|
535
537
|
# if {fix_code_langs} and the language is not in {exclude_code_langs}.
|
536
|
-
#
|
538
|
+
#
|
537
539
|
# @param line [String] the line from the file to fix
|
538
540
|
def gsub_code_langs!(line)
|
539
541
|
return false unless @fix_code_langs
|
540
|
-
|
542
|
+
|
541
543
|
has_change = false
|
542
544
|
tag = 'code class="'
|
543
|
-
|
545
|
+
|
544
546
|
line.gsub!(Regexp.new(Regexp.quote(tag) + '[^"]*"')) do |code_class|
|
545
|
-
lang = code_class[tag.length..-2].strip
|
546
|
-
|
547
|
-
if lang.empty?
|
547
|
+
lang = code_class[tag.length..-2].strip
|
548
|
+
|
549
|
+
if lang.empty? || lang =~ /^language\-/ || @exclude_code_langs.include?(lang)
|
548
550
|
code_class
|
549
551
|
else
|
550
552
|
has_change = true
|
551
|
-
|
552
|
-
%Q(#{tag}language-#{lang.downcase
|
553
|
+
|
554
|
+
%Q(#{tag}language-#{lang.downcase}")
|
553
555
|
end
|
554
556
|
end
|
555
|
-
|
557
|
+
|
556
558
|
return has_change
|
557
559
|
end
|
558
|
-
|
560
|
+
|
559
561
|
# Call the custom Proc {custom_gsub} (if it responds to +:call+) on +line+,
|
560
|
-
#
|
562
|
+
#
|
561
563
|
# @param line [String] the line from the file to fix
|
562
564
|
def gsub_custom!(line)
|
563
565
|
return false unless @custom_gsub.respond_to?(:call)
|
564
566
|
return @custom_gsub.call(line)
|
565
567
|
end
|
566
|
-
|
568
|
+
|
567
569
|
# Call +gsub!()+ on +line+ with each {custom_gsubs},
|
568
570
|
# which is an Array of pairs of arguments:
|
569
571
|
# task.custom_gsubs = [
|
570
572
|
# ['dev','prod'],
|
571
573
|
# [/href="#[^"]*"/,'href="#contents"']
|
572
574
|
# ]
|
573
|
-
#
|
575
|
+
#
|
574
576
|
# @param line [String] the line from the file to fix
|
575
577
|
def gsub_customs!(line)
|
576
|
-
return false if @custom_gsubs.empty?
|
577
|
-
|
578
|
+
return false if @custom_gsubs.empty?
|
579
|
+
|
578
580
|
has_change = false
|
579
|
-
|
581
|
+
|
580
582
|
@custom_gsubs.each do |custom_gsub|
|
581
|
-
has_change = !line.gsub!(custom_gsub[0],custom_gsub[1]).nil?
|
583
|
+
has_change = !line.gsub!(custom_gsub[0],custom_gsub[1]).nil? || has_change
|
582
584
|
end
|
583
|
-
|
585
|
+
|
584
586
|
return has_change
|
585
587
|
end
|
586
|
-
|
588
|
+
|
587
589
|
# Replace local file links (that exist) to be +file.{filename}.html+,
|
588
590
|
# if {fix_file_links}.
|
589
|
-
#
|
591
|
+
#
|
590
592
|
# @param line [String] the line from the file to fix
|
591
593
|
def gsub_local_file_links!(line)
|
592
594
|
return false unless @fix_file_links
|
593
|
-
|
595
|
+
|
594
596
|
has_change = false
|
595
597
|
tag = 'href="'
|
596
|
-
|
598
|
+
|
597
599
|
line.gsub!(Regexp.new(Regexp.quote(tag) + '[^#][^"]*"')) do |href|
|
598
|
-
link = href[tag.length..-2].strip
|
599
|
-
|
600
|
-
if link.empty?
|
600
|
+
link = href[tag.length..-2].strip
|
601
|
+
|
602
|
+
if link.empty? || !File.exist?(link)
|
601
603
|
href
|
602
604
|
else
|
603
605
|
link = File.basename(link,'.*')
|
604
606
|
has_change = true
|
605
|
-
|
607
|
+
|
606
608
|
%Q(#{tag}file.#{link}.html")
|
607
609
|
end
|
608
610
|
end
|
609
|
-
|
611
|
+
|
610
612
|
return has_change
|
611
613
|
end
|
612
614
|
end
|