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)
5555 end
5656 end
5757
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+
5870end
5971
6072class RDoc ::CodeObject
Original file line number Diff line number Diff line change 2525 <%- end -%>
2626<%- end -%>
2727
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+
2833< script type ="text/javascript ">
2934 var rdoc_rel_prefix = "<%= h asset_rel_prefix %> /" ;
3035 var index_rel_prefix = "<%= h rel_prefix %> /" ;
Original file line number Diff line number Diff line change @@ -373,6 +373,11 @@ class RDoc::Options
373373
374374 attr_accessor :file_path_prefix
375375
376+ ##
377+ # The preferred root URL for the documentation
378+
379+ attr_accessor :canonical_root
380+
376381 def initialize ( loaded_options = nil ) # :nodoc:
377382 init_ivars
378383 override loaded_options if loaded_options
@@ -429,6 +434,7 @@ def init_ivars # :nodoc:
429434 @apply_default_exclude = true
430435 @class_module_path_prefix = nil
431436 @file_path_prefix = nil
437+ @canonical_root = nil
432438 end
433439
434440 def init_with ( map ) # :nodoc:
@@ -492,6 +498,7 @@ def override(map) # :nodoc:
492498 @webcvs = map [ 'webcvs' ] if map . has_key? ( 'webcvs' )
493499 @autolink_excluded_words = map [ 'autolink_excluded_words' ] if map . has_key? ( 'autolink_excluded_words' )
494500 @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' )
495502
496503 @warn_missing_rdoc_ref = map [ 'warn_missing_rdoc_ref' ] if map . has_key? ( 'warn_missing_rdoc_ref' )
497504
Original file line number Diff line number Diff line change @@ -514,6 +514,39 @@ def test_meta_tags_for_empty_document
514514 )
515515 end
516516
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+
517550 ##
518551 # Asserts that +filename+ has a link count greater than 1 if hard links to
519552 # @tmpdir are supported.
Original file line number Diff line number Diff line change @@ -89,6 +89,7 @@ def test_to_yaml
8989 'autolink_excluded_words' => [ ] ,
9090 'class_module_path_prefix' => nil ,
9191 'file_path_prefix' => nil ,
92+ 'canonical_root' => nil ,
9293 }
9394
9495 assert_equal expected , coder
You can’t perform that action at this time.
0 commit comments