File tree Expand file tree Collapse file tree 5 files changed +58
-0
lines changed Expand file tree Collapse file tree 5 files changed +58
-0
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,18 @@ def cvs_url(url, full_path)
55
55
end
56
56
end
57
57
58
+ ##
59
+ # The preferred URL for this object.
60
+
61
+ def canonical_url
62
+ options = @store . options
63
+ if path
64
+ File . join ( options . canonical_root , path . to_s )
65
+ else
66
+ options . canonical_root
67
+ end
68
+ end
69
+
58
70
end
59
71
60
72
class RDoc ::CodeObject
Original file line number Diff line number Diff line change 25
25
<%- end -%>
26
26
<%- end -%>
27
27
28
+ <%- if canonical_url = @options . canonical_root -%>
29
+ <% canonical_url = current . canonical_url if defined? ( current ) %>
30
+ < link rel ="canonical " href ="<%= canonical_url %> ">
31
+ <%- end -%>
32
+
28
33
< script type ="text/javascript ">
29
34
var rdoc_rel_prefix = "<%= h asset_rel_prefix %> /" ;
30
35
var index_rel_prefix = "<%= h rel_prefix %> /" ;
Original file line number Diff line number Diff line change @@ -373,6 +373,11 @@ class RDoc::Options
373
373
374
374
attr_accessor :file_path_prefix
375
375
376
+ ##
377
+ # The preferred root URL for the documentation
378
+
379
+ attr_accessor :canonical_root
380
+
376
381
def initialize ( loaded_options = nil ) # :nodoc:
377
382
init_ivars
378
383
override loaded_options if loaded_options
@@ -429,6 +434,7 @@ def init_ivars # :nodoc:
429
434
@apply_default_exclude = true
430
435
@class_module_path_prefix = nil
431
436
@file_path_prefix = nil
437
+ @canonical_root = nil
432
438
end
433
439
434
440
def init_with ( map ) # :nodoc:
@@ -492,6 +498,7 @@ def override(map) # :nodoc:
492
498
@webcvs = map [ 'webcvs' ] if map . has_key? ( 'webcvs' )
493
499
@autolink_excluded_words = map [ 'autolink_excluded_words' ] if map . has_key? ( 'autolink_excluded_words' )
494
500
@apply_default_exclude = map [ 'apply_default_exclude' ] if map . has_key? ( 'apply_default_exclude' )
501
+ @canonical_root = map [ 'canonical_root' ] if map . has_key? ( 'canonical_root' )
495
502
496
503
@warn_missing_rdoc_ref = map [ 'warn_missing_rdoc_ref' ] if map . has_key? ( 'warn_missing_rdoc_ref' )
497
504
Original file line number Diff line number Diff line change @@ -514,6 +514,39 @@ def test_meta_tags_for_empty_document
514
514
)
515
515
end
516
516
517
+ def test_canonical_url_for_index
518
+ @store . options . canonical_root = @options . canonical_root = "https://docs.ruby-lang.org/en/master/"
519
+ @g . generate
520
+
521
+ content = File . binread ( "index.html" )
522
+
523
+ assert_include ( content , '<link rel="canonical" href="https://docs.ruby-lang.org/en/master/">' )
524
+ end
525
+
526
+ def test_canonical_url_for_classes
527
+ top_level = @store . add_file ( "file.rb" )
528
+ top_level . add_class ( @klass . class , @klass . name )
529
+ inner = @klass . add_class ( RDoc ::NormalClass , "Inner" )
530
+
531
+ @store . options . canonical_root = @options . canonical_root = "https://docs.ruby-lang.org/en/master/"
532
+ @g . generate
533
+
534
+ content = File . binread ( "Klass/Inner.html" )
535
+
536
+ assert_include ( content , '<link rel="canonical" href="https://docs.ruby-lang.org/en/master/Klass/Inner.html">' )
537
+ end
538
+
539
+ def test_canonical_url_for_rdoc_files
540
+ top_level = @store . add_file ( "CONTRIBUTING.rdoc" , parser : RDoc ::Parser ::Simple )
541
+
542
+ @store . options . canonical_root = @options . canonical_root = "https://docs.ruby-lang.org/en/master/"
543
+ @g . generate
544
+
545
+ content = File . binread ( "CONTRIBUTING_rdoc.html" )
546
+
547
+ assert_include ( content , '<link rel="canonical" href="https://docs.ruby-lang.org/en/master/CONTRIBUTING_rdoc.html">' )
548
+ end
549
+
517
550
##
518
551
# Asserts that +filename+ has a link count greater than 1 if hard links to
519
552
# @tmpdir are supported.
Original file line number Diff line number Diff line change @@ -89,6 +89,7 @@ def test_to_yaml
89
89
'autolink_excluded_words' => [ ] ,
90
90
'class_module_path_prefix' => nil ,
91
91
'file_path_prefix' => nil ,
92
+ 'canonical_root' => nil ,
92
93
}
93
94
94
95
assert_equal expected , coder
You can’t perform that action at this time.
0 commit comments