13
13
require "rails_guides/markdown"
14
14
require "rails_guides/helpers"
15
15
require "rails_guides/epub"
16
+ require "debug"
16
17
17
18
module RailsGuides
18
19
class Generator
19
20
GUIDES_RE = /\. (?:erb|md)\z /
20
21
21
- def initialize ( edge :, version :, all :, only :, epub :, language :, direction : nil )
22
+ def initialize ( edge :, version :, all :, only :, epub :, language :, direction : nil , lint : )
22
23
@edge = edge
23
24
@version = version
24
25
@all = all
25
26
@only = only
26
27
@epub = epub
27
28
@language = language
28
29
@direction = direction || "ltr"
30
+ @lint = lint
31
+ @warnings = [ ]
29
32
30
33
if @epub
31
34
register_special_mime_types
32
35
end
33
36
34
37
initialize_dirs
35
- create_output_dir_if_needed
38
+ create_output_dir_if_needed if ! dry_run?
36
39
initialize_markdown_renderer
37
40
end
38
41
39
42
def generate
40
43
generate_guides
41
- process_scss
42
- copy_assets
43
- generate_epub if @epub
44
+
45
+ if @lint && @warnings . any?
46
+ puts "#{ @warnings . join ( "\n " ) } "
47
+ exit 1
48
+ end
49
+
50
+ if !dry_run?
51
+ process_scss
52
+ copy_assets
53
+ generate_epub if @epub
54
+ end
44
55
end
45
56
46
57
private
58
+ def dry_run?
59
+ [ @lint ] . any?
60
+ end
61
+
47
62
def register_special_mime_types
48
63
Mime ::Type . register_alias ( "application/xml" , :opf , %w( opf ) )
49
64
Mime ::Type . register_alias ( "application/xml" , :ncx , %w( ncx ) )
@@ -168,12 +183,15 @@ def generate_guide(guide, output_file)
168
183
epub : @epub
169
184
) . render ( body )
170
185
171
- warn_about_broken_links ( result )
186
+ broken = warn_about_broken_links ( result )
187
+ if broken . any?
188
+ @warnings << "[WARN] BROKEN LINK(s): #{ guide } : #{ broken . join ( ", " ) } "
189
+ end
172
190
end
173
191
174
192
File . open ( output_path , "w" ) do |f |
175
193
f . write ( result )
176
- end
194
+ end if ! dry_run?
177
195
end
178
196
179
197
def warn_about_broken_links ( html )
@@ -199,13 +217,18 @@ def extract_anchors(html)
199
217
end
200
218
201
219
def check_fragment_identifiers ( html , anchors )
220
+ broken_links = [ ]
221
+
202
222
html . scan ( /<a\s +href="#([^"]+)/ ) . flatten . each do |fragment_identifier |
203
223
next if fragment_identifier == "mainCol" # in layout, jumps to some DIV
204
224
unless anchors . member? ( CGI . unescape ( fragment_identifier ) )
205
225
guess = DidYouMean ::SpellChecker . new ( dictionary : anchors ) . correct ( fragment_identifier ) . first
206
226
puts "*** BROKEN LINK: ##{ fragment_identifier } , perhaps you meant ##{ guess } ."
227
+ broken_links << "##{ fragment_identifier } "
207
228
end
208
229
end
230
+
231
+ broken_links
209
232
end
210
233
end
211
234
end
0 commit comments