yard_ghurt 1.2.0 → 1.2.2

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.
data/bin/yard_ghurt CHANGED
@@ -4,26 +4,13 @@
4
4
 
5
5
  #--
6
6
  # This file is part of YardGhurt.
7
- # Copyright (c) 2020 Jonathan Bradley Whited (@esotericpig)
8
- #
9
- # YardGhurt is free software: you can redistribute it and/or modify
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/>.
7
+ # Copyright (c) 2020 Bradley Whited
8
+ #
9
+ # SPDX-License-Identifier: LGPL-3.0-or-later
21
10
  #++
22
11
 
23
-
24
12
  require 'yard_ghurt'
25
13
 
26
-
27
14
  # @since 1.2.0
28
- yg = YardGhurt::App.new()
29
- yg.run()
15
+ yg = YardGhurt::App.new
16
+ yg.run
@@ -1,275 +1,228 @@
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-2020 Jonathan Bradley Whited (@esotericpig)
8
- #
9
- # YardGhurt is free software: you can redistribute it and/or modify
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 Bradley Whited
7
+ #
8
+ # SPDX-License-Identifier: LGPL-3.0-or-later
21
9
  #++
22
10
 
23
-
24
11
  require 'set'
25
12
  require 'uri'
26
13
 
27
14
  module YardGhurt
28
- ###
29
15
  # A "database" of anchor links specific to GitHub Flavored Markdown (GFM) & YARDoc.
30
- #
16
+ #
31
17
  # You can use this by itself to view what anchor IDs would be generated:
32
- # al = YardGhurt::AnchorLinks.new()
33
- #
34
- # puts al.to_github_anchor_id('This is a test!')
35
- # puts al.to_yard_anchor_id('This is a test!')
36
- #
37
- # # Output:
38
- # # ---
39
- # # this-is-a-test
40
- # # This_is_a_test_
41
- #
18
+ # al = YardGhurt::AnchorLinks.new
19
+ #
20
+ # puts al.to_github_anchor_id('This is a test!') #=> this-is-a-test
21
+ # puts al.to_yard_anchor_id('This is a test!') #=> This_is_a_test_
22
+ #
42
23
  # Be aware that YARDoc depends on a common number that will be incremented for all duplicates,
43
- # while GFM's number is only local to each specific duplicate:
44
- # al = YardGhurt::AnchorLinks.new()
45
- # name = 'This is a test!'
46
- #
47
- # puts al.to_yard_anchor_id(name) # This_is_a_test_
48
- # puts al.to_yard_anchor_id(name) # This_is_a_test_
49
- #
50
- # puts al.to_github_anchor_id(name) # this-is-a-test
51
- # puts al.to_github_anchor_id(name) # this-is-a-test
52
- #
53
- # al << name # Officially add it to the database
54
- #
55
- # # Instead of being 0 & 0, will be 0 & 1 (incremented),
56
- # # even without being added to the database
57
- # puts al.to_yard_anchor_id(name) # This_is_a_test_0
58
- # puts al.to_yard_anchor_id(name) # This_is_a_test_1
59
- #
60
- # puts al.to_github_anchor_id(name) # this-is-a-test-1
61
- # puts al.to_github_anchor_id(name) # this-is-a-test-1
62
- #
63
- # name = 'This is another test!'
64
- # al << name # Officially add it to the database
65
- #
66
- # # Instead of being 0 & 1, will be 2 & 3 (global increment),
67
- # # even without being added to the database
68
- # puts al.to_yard_anchor_id(name) # This_is_another_test_2
69
- # puts al.to_yard_anchor_id(name) # This_is_another_test_3
70
- #
71
- # puts al.to_github_anchor_id(name) # this-is-another-test-1
72
- # puts al.to_github_anchor_id(name) # this-is-another-test-1
73
- #
74
- # @author Jonathan Bradley Whited (@esotericpig)
75
- # @since 1.0.0
76
- #
24
+ # while GFM's number is only local to each duplicate.
25
+ #
26
+ # @since 1.0.0
27
+ #
77
28
  # @see GFMFixTask
78
- ###
79
29
  class AnchorLinks
80
30
  # @return [Hash] the GFM-style anchor IDs pointing to their YARDoc ID equivalents that have been added
81
31
  attr_reader :anchor_ids
82
-
32
+
83
33
  # @return [Set] the YARDoc anchor IDs that have been added
84
34
  attr_accessor :yard_anchor_ids
85
-
35
+
86
36
  # @return [Integer] the next YARDoc number to use if there is a duplicate anchor ID
87
37
  attr_accessor :yard_dup_num
88
-
89
- def initialize()
90
- reset()
38
+
39
+ def initialize
40
+ super
41
+ reset
91
42
  end
92
-
43
+
93
44
  # Reset the database back to its fresh, pristine self,
94
45
  # including common numbers for duplicates.
95
- def reset()
46
+ def reset
96
47
  @anchor_ids = {}
97
- @yard_anchor_ids = Set.new()
48
+ @yard_anchor_ids = Set.new
98
49
  @yard_dup_num = 0
99
50
  end
100
-
51
+
101
52
  # (see #add_anchor)
102
53
  def <<(name)
103
54
  return add_anchor(name)
104
55
  end
105
-
56
+
106
57
  # (see #store_anchor)
107
58
  def []=(github_anchor_id,yard_anchor_id)
108
- return store_anchor(github_anchor_id,yard_anchor_id)
59
+ store_anchor(github_anchor_id,yard_anchor_id)
109
60
  end
110
-
61
+
111
62
  # Convert +name+ (header text) to a GFM and YARDoc anchor ID and add the IDs to the database.
112
- #
63
+ #
64
+ # @note +yard_id:+ was added in v1.2.1 for YARD v0.9.25+.
65
+ #
113
66
  # @param name [String] the name (header text) to convert to anchor IDs and add to the database
114
- #
115
67
  # @return [self]
116
- def add_anchor(name)
117
- store_anchor(to_github_anchor_id(name),to_yard_anchor_id(name))
118
-
68
+ def add_anchor(name,yard_id: nil)
69
+ yard_id = to_yard_anchor_id(name) if yard_id.nil?
70
+
71
+ store_anchor(to_github_anchor_id(name),yard_id)
72
+
119
73
  return self
120
74
  end
121
-
75
+
122
76
  # Escape +str+ for the web (e.g., "%20" for a space).
123
- #
77
+ #
124
78
  # Mainly used for non-English languages.
125
- #
79
+ #
126
80
  # @param str [String] the string to escape
127
- #
81
+ #
128
82
  # @return [String] the escaped string
129
- #
130
- # @since 1.2.0
83
+ #
84
+ # @since 1.2.0
131
85
  def self.escape(str)
132
- # URI.escape()/encode() is obsolete
86
+ # URI.escape()/encode() is obsolete.
133
87
  return URI.encode_www_form_component(str)
134
88
  end
135
-
89
+
136
90
  # Merge +anchor_ids+ with {anchor_ids} and {yard_anchor_ids}.
137
- #
91
+ #
138
92
  # @param anchor_ids [Hash] the anchor IDs (of GFM anchor IDs to YARDoc anchor IDs) to merge
139
93
  def merge_anchor_ids!(anchor_ids)
140
94
  @anchor_ids.merge!(anchor_ids)
141
95
  @yard_anchor_ids.merge(anchor_ids.values)
142
-
96
+
143
97
  return @anchor_ids
144
98
  end
145
-
99
+
146
100
  # Store & associate key +github_anchor_id+ with value +yard_anchor_id+,
147
101
  # and add +yard_anchor_id+ to the YARDoc anchor IDs.
148
- #
102
+ #
149
103
  # @param github_anchor_id [String] the GitHub anchor ID; the key
150
104
  # @param yard_anchor_id [String] the YARDoc anchor ID; the value
151
- #
105
+ #
152
106
  # @return [String] the +yard_anchor_id+ added
153
107
  def store_anchor(github_anchor_id,yard_anchor_id)
154
108
  @anchor_ids[github_anchor_id] = yard_anchor_id
155
109
  @yard_anchor_ids << yard_anchor_id
156
-
110
+
157
111
  return yard_anchor_id
158
112
  end
159
-
113
+
160
114
  def anchor_ids=(anchor_ids)
161
115
  @anchor_ids = anchor_ids
162
116
  @yard_anchor_ids.merge(anchor_ids.values)
163
-
164
- return @anchor_ids
165
117
  end
166
-
118
+
167
119
  # (see #anchor_id)
168
120
  def [](github_anchor_id)
169
121
  return anchor_id(github_anchor_id)
170
122
  end
171
-
123
+
172
124
  # Get the YARDoc anchor ID associated with this +github_anchor_id+.
173
- #
125
+ #
174
126
  # @param github_anchor_id [String] the GitHub anchor ID key to look up
175
- #
127
+ #
176
128
  # @return [String,nil] the YARDoc anchor ID associated with +github_anchor_id+
177
129
  def anchor_id(github_anchor_id)
178
130
  return @anchor_ids[github_anchor_id]
179
131
  end
180
-
132
+
181
133
  # Check if +id+ exists in the database of YARDoc anchor IDs
182
- #
134
+ #
183
135
  # @param id [String] the YARDoc anchor ID to check
184
- #
136
+ #
185
137
  # @return [true,false] whether this ID exists in the database of YARDoc anchor IDs
186
138
  def yard_anchor_id?(id)
187
139
  return @yard_anchor_ids.include?(id)
188
140
  end
189
-
141
+
190
142
  # Convert +name+ (header text) to a GitHub anchor ID.
191
- #
143
+ #
192
144
  # If the converted ID already exists in the database,
193
145
  # then the ID will be updated according to GFM rules.
194
- #
146
+ #
195
147
  # @param name [String] the name (header text) to convert
196
- #
148
+ #
197
149
  # @return [String] the converted GitHub anchor ID for this database
198
- #
150
+ #
199
151
  # @see https://gist.github.com/asabaylus/3071099#gistcomment-2834467
200
152
  # @see https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb
201
153
  def to_github_anchor_id(name)
202
- id = name.dup()
203
-
204
- id.strip!()
154
+ id = name.dup
155
+
156
+ id.strip!
205
157
  id.gsub!(/&[^;]+;/,'') # Remove entities: &...;
206
158
  id.gsub!(/[^\p{Word}\- ]/u,'')
207
159
  id.tr!(' ','-')
208
-
160
+
209
161
  if RUBY_VERSION >= '2.4'
210
162
  id.downcase!(:ascii)
211
163
  else
212
- id.downcase!()
164
+ id.downcase!
213
165
  end
214
-
215
- id = self.class.escape(id) # For non-English languages
216
-
166
+
167
+ id = self.class.escape(id) # For non-English languages.
168
+
217
169
  # Duplicates
218
170
  dup_num = 1
219
- orig_id = id.dup()
220
-
171
+ orig_id = id.dup
172
+
221
173
  while @anchor_ids.key?(id)
222
174
  id = "#{orig_id}-#{dup_num}"
223
175
  dup_num += 1
224
176
  end
225
-
177
+
226
178
  return id
227
179
  end
228
-
180
+
229
181
  # Dumps the key-value database of GFM & YARDoc anchor IDs.
230
- #
182
+ #
231
183
  # @return [String] the database of anchor IDs as a String
232
- def to_s()
233
- s = ''.dup()
234
-
235
- @anchor_ids.keys.sort_by{|key| key.to_s()}.each do |key|
184
+ def to_s
185
+ s = ''.dup
186
+
187
+ @anchor_ids.keys.sort_by(&:to_s).each do |key|
236
188
  s << "[#{key}] => '#{@anchor_ids[key]}'\n"
237
189
  end
238
-
190
+
239
191
  return s
240
192
  end
241
-
193
+
242
194
  # Convert +name+ (header text) to a YARDoc anchor ID.
243
- #
195
+ #
244
196
  # If the converted ID already exists in the database,
245
197
  # then the ID will be updated according to YARDoc rules,
246
198
  # which requires incrementing a common number variable.
247
- #
248
- # The logic for this is pulled from +doc/app.js#generateTOC()+,
249
- # which you can generate using +rake yard+ or +yardoc+ on the command line.
250
- #
199
+ #
200
+ # The logic for this is pulled from +doc/app.js#generateTOC()+
201
+ # (or +doc/js/app.js+), which you can generate using +rake yard+
202
+ # or +yardoc+ on the command line.
203
+ #
251
204
  # @note Be aware that this will increment a common number variable
252
205
  # every time you call this with a duplicate.
253
- #
206
+ #
254
207
  # @param name [String] the name (header text) to convert
255
- #
208
+ #
256
209
  # @return [String] the converted YARDoc anchor ID for this database
257
210
  def to_yard_anchor_id(name)
258
- id = name.dup()
259
-
260
- id.strip!()
211
+ id = name.dup
212
+
213
+ id.strip!
261
214
  id.gsub!(/&[^;]+;/,'_') # Replace entities: &...;
262
215
  id.gsub!(/[^a-z0-9-]/i,'_')
263
- id = self.class.escape(id) # For non-English languages
264
-
265
- # Duplicates
266
- orig_id = id.dup()
267
-
216
+ id = self.class.escape(id) # For non-English languages.
217
+
218
+ # Duplicates.
219
+ orig_id = id.dup
220
+
268
221
  while @yard_anchor_ids.include?(id)
269
222
  id = "#{orig_id}#{@yard_dup_num}"
270
223
  @yard_dup_num += 1
271
224
  end
272
-
225
+
273
226
  return id
274
227
  end
275
228
  end