Skip to content

Commit 07c4313

Browse files
committed
Generate meta tags for each type of page based on content
1 parent 944ed08 commit 07c4313

File tree

7 files changed

+79
-51
lines changed

7 files changed

+79
-51
lines changed

Rakefile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ RDoc::Task.new do |doc|
2525
doc.title = "rdoc #{RDoc::VERSION} Documentation"
2626
doc.rdoc_dir = '_site' # for github pages
2727
doc.rdoc_files = FileList.new %w[lib/**/*.rb *.rdoc doc/rdoc/markup_reference.rb] - PARSER_FILES
28-
doc.meta_tags = {
29-
"description" => "Documentation for RDoc, the Ruby documentation generator",
30-
"keywords" => "rdoc,ruby,documentation,generator"
31-
}
3228
end
3329

3430
task "coverage" do

lib/rdoc/generator/darkfish.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,4 +783,18 @@ def template_for file, page = true, klass = ERB
783783
template
784784
end
785785

786+
# Returns an excerpt of the content for usage in meta description tags
787+
def excerpt(content)
788+
text = content.is_a?(RDoc::Comment) ? content.text : content
789+
790+
# Try to select the text up to the second paragraph, the first paragraph or the first 150 characters
791+
excerpt_end = text.index(".")
792+
excerpt_end = if excerpt_end
793+
text.index(".", excerpt_end + 1) || excerpt_end
794+
else
795+
150
796+
end
797+
798+
text[0...excerpt_end].gsub(/\n/, ' ').squeeze(" ")
799+
end
786800
end

lib/rdoc/generator/template/darkfish/_head.rhtml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,26 @@
22

33
<title><%= h @title %></title>
44

5-
<%- @options.meta_tags.each do |name, content| -%>
6-
<meta name="<%= h name %>" content="<%= h content %>">
5+
<%- if defined?(klass) -%>
6+
<meta name="keywords" content="ruby,<%= h "#{klass.type},#{klass.full_name}" %>">
7+
8+
<%- if klass.comment.empty? -%>
9+
<meta name="description" content="Documentation for the <%= h "#{klass.full_name} #{klass.type}" %>">
10+
<%- else -%>
11+
<meta name="description" content="<%= h "#{klass.type} #{klass.full_name}: #{excerpt(klass.comment)}" %>">
12+
<%- end -%>
13+
<%- elsif defined?(file) -%>
14+
<meta name="keywords" content="ruby,documentation,<%= h file.page_name %>">
15+
<meta name="description" content="<%= h "#{file.page_name}: #{excerpt(file.comment)}" %>">
16+
<%- elsif @title -%>
17+
<meta name="keywords" content="ruby,documentation,<%= h @title %>">
18+
19+
<%- if @options.main_page and
20+
main_page = @files.find { |f| f.full_name == @options.main_page } then %>
21+
<meta name="description" content="<%= h "#{@title}: #{excerpt(main_page.comment)}" %>">
22+
<%- else -%>
23+
<meta name="description" content="Documentation for <%= h @title %>">
24+
<%- end -%>
725
<%- end -%>
826

927
<script type="text/javascript">

lib/rdoc/options.rb

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,6 @@ class RDoc::Options
344344
# Indicates if files of test suites should be skipped
345345
attr_accessor :skip_tests
346346

347-
##
348-
# Meta tags to be included in the HTML header, such as keywords, description,
349-
# author and others. This option is a hash where the keys are the meta tag
350-
# name and the values are the content of the meta tag.
351-
attr_accessor :meta_tags
352-
353347
def initialize loaded_options = nil # :nodoc:
354348
init_ivars
355349
override loaded_options if loaded_options
@@ -398,7 +392,6 @@ def init_ivars # :nodoc:
398392
@encoding = Encoding::UTF_8
399393
@charset = @encoding.name
400394
@skip_tests = true
401-
@meta_tags = {}
402395
end
403396

404397
def init_with map # :nodoc:
@@ -423,7 +416,6 @@ def init_with map # :nodoc:
423416
@title = map['title']
424417
@visibility = map['visibility']
425418
@webcvs = map['webcvs']
426-
@meta_tags = map['meta_tags']
427419

428420
@rdoc_include = sanitize_path map['rdoc_include']
429421
@static_path = sanitize_path map['static_path']
@@ -456,7 +448,6 @@ def override map # :nodoc:
456448
@title = map['title'] if map.has_key?('title')
457449
@visibility = map['visibility'] if map.has_key?('visibility')
458450
@webcvs = map['webcvs'] if map.has_key?('webcvs')
459-
@meta_tags = map['meta_tags'] if map.has_key?('meta_tags')
460451

461452
if map.has_key?('rdoc_include')
462453
@rdoc_include = sanitize_path map['rdoc_include']
@@ -484,8 +475,7 @@ def == other # :nodoc:
484475
@template == other.template and
485476
@title == other.title and
486477
@visibility == other.visibility and
487-
@webcvs == other.webcvs and
488-
@meta_tags == other.meta_tags
478+
@webcvs == other.webcvs
489479
end
490480

491481
##
@@ -802,15 +792,6 @@ def parse argv
802792

803793
opt.separator nil
804794

805-
opt.on("-mTAGS", "--meta-tags=TAGS",
806-
"Meta tags to be included in the HTML head") do |value|
807-
@meta_tags = JSON.parse(value)
808-
rescue JSON::ParserError, TypeError
809-
raise OptionParser::InvalidArgument, "Invalid value for --meta-tags. Should be a JSON string"
810-
end
811-
812-
opt.separator nil
813-
814795
opt.on("--extension=NEW=OLD", "-E",
815796
"Treat files ending with .new as if they",
816797
"ended with .old. Using '-E cgi=rb' will",

lib/rdoc/task.rb

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
require_relative '../rdoc'
3636
require 'rake'
3737
require 'rake/tasklib'
38-
require "json"
3938

4039
##
4140
# RDoc::Task creates the following rake tasks to generate and clean up RDoc
@@ -152,12 +151,6 @@ class RDoc::Task < Rake::TaskLib
152151

153152
attr_accessor :external
154153

155-
##
156-
# Meta tags to be included in the HTML header, such as keywords, description,
157-
# author and others. This option is a hash where the keys are the meta tag
158-
# name and the values are the content of the meta tag.
159-
attr_accessor :meta_tags
160-
161154
##
162155
# Create an RDoc task with the given name. See the RDoc::Task class overview
163156
# for documentation.
@@ -208,7 +201,6 @@ def defaults
208201
@template = nil
209202
@generator = nil
210203
@options = []
211-
@meta_tags = nil
212204
end
213205

214206
##
@@ -279,13 +271,12 @@ def define
279271

280272
def option_list
281273
result = @options.dup
282-
result << "-o" << @rdoc_dir
283-
result << "--main" << main if main
284-
result << "--markup" << markup if markup
285-
result << "--title" << title if title
286-
result << "-T" << template if template
287-
result << '-f' << generator if generator
288-
result << '--meta-tags' << meta_tags.to_json if meta_tags
274+
result << "-o" << @rdoc_dir
275+
result << "--main" << main if main
276+
result << "--markup" << markup if markup
277+
result << "--title" << title if title
278+
result << "-T" << template if template
279+
result << '-f' << generator if generator
289280
result
290281
end
291282

test/rdoc/test_rdoc_generator_darkfish.rb

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,19 +322,48 @@ def test_title_escape
322322
assert_main_title(File.binread('index.html'), title)
323323
end
324324

325-
def test_meta_tags
326-
@options.meta_tags = {
327-
"keywords" => "ruby,programming,software",
328-
"description" => "RDoc is an awesome documentation generator for Ruby!"
329-
}
325+
def test_meta_tags_for_index
326+
@options.title = "My awesome Ruby project"
330327
@g.generate
331328

332329
content = File.binread("index.html")
333330

334-
assert_match("<meta name=\"keywords\" content=\"ruby,programming,software\">", content)
335-
assert_match(
336-
"<meta name=\"description\" content=\"RDoc is an awesome documentation generator for Ruby!\">",
337-
content
331+
assert_include(content, '<meta name="keywords" content="ruby,documentation,My awesome Ruby project">')
332+
assert_include(content, '<meta name="description" content="Documentation for My awesome Ruby project">')
333+
end
334+
335+
def test_meta_tags_for_classes
336+
top_level = @store.add_file("file.rb")
337+
top_level.add_class(@klass.class, @klass.name)
338+
inner = @klass.add_class(RDoc::NormalClass, "Inner")
339+
inner.add_comment("This is a normal class. It is fully documented.", top_level)
340+
341+
@g.generate
342+
343+
content = File.binread("Klass/Inner.html")
344+
assert_include(content, '<meta name="keywords" content="ruby,class,Klass::Inner">')
345+
assert_include(
346+
content,
347+
'<meta name="description" content="class Klass::Inner: This is a normal class. It is fully documented">',
348+
)
349+
end
350+
351+
def test_meta_tags_for_pages
352+
top_level = @store.add_file("CONTRIBUTING.rdoc", parser: RDoc::Parser::Simple)
353+
top_level.comment = <<~RDOC
354+
= Contributing
355+
356+
Here are the instructions for contributing. Begin by installing Ruby.
357+
RDOC
358+
359+
@g.generate
360+
361+
content = File.binread("CONTRIBUTING_rdoc.html")
362+
assert_include(content, '<meta name="keywords" content="ruby,documentation,CONTRIBUTING">')
363+
assert_include(
364+
content,
365+
"<meta name=\"description\" content=\"CONTRIBUTING: = Contributing Here are the instructions for contributing." \
366+
" Begin by installing Ruby\">",
338367
)
339368
end
340369

test/rdoc/test_rdoc_options.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ def test_to_yaml
8383
'visibility' => :protected,
8484
'webcvs' => nil,
8585
'skip_tests' => true,
86-
'meta_tags' => {},
8786
}
8887

8988
assert_equal expected, coder

0 commit comments

Comments
 (0)