Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for meta tags #1091

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ RDoc::Task.new do |doc|
doc.title = "rdoc #{RDoc::VERSION} Documentation"
doc.rdoc_dir = '_site' # for github pages
doc.rdoc_files = FileList.new %w[lib/**/*.rb *.rdoc doc/rdoc/markup_reference.rb] - PARSER_FILES
doc.meta_tags = {
"description" => "Documentation for RDoc, the Ruby documentation generator",
"keywords" => "rdoc,ruby,documentation,generator"
}
end

task "coverage" do
Expand Down
4 changes: 4 additions & 0 deletions lib/rdoc/generator/template/darkfish/_head.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

<%- @options.meta_tags.each do |name, content| -%>
<meta name="<%= h name %>" content="<%= h content %>">
<%- end -%>

<script type="text/javascript">
var rdoc_rel_prefix = "<%= h asset_rel_prefix %>/";
var index_rel_prefix = "<%= h rel_prefix %>/";
Expand Down
21 changes: 20 additions & 1 deletion lib/rdoc/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,12 @@ class RDoc::Options
# Indicates if files of test suites should be skipped
attr_accessor :skip_tests

##
# Meta tags to be included in the HTML header, such as keywords, description,
# author and others. This option is a hash where the keys are the meta tag
# name and the values are the content of the meta tag.
attr_accessor :meta_tags

def initialize loaded_options = nil # :nodoc:
init_ivars
override loaded_options if loaded_options
Expand Down Expand Up @@ -392,6 +398,7 @@ def init_ivars # :nodoc:
@encoding = Encoding::UTF_8
@charset = @encoding.name
@skip_tests = true
@meta_tags = {}
end

def init_with map # :nodoc:
Expand All @@ -416,6 +423,7 @@ def init_with map # :nodoc:
@title = map['title']
@visibility = map['visibility']
@webcvs = map['webcvs']
@meta_tags = map['meta_tags']

@rdoc_include = sanitize_path map['rdoc_include']
@static_path = sanitize_path map['static_path']
Expand Down Expand Up @@ -448,6 +456,7 @@ def override map # :nodoc:
@title = map['title'] if map.has_key?('title')
@visibility = map['visibility'] if map.has_key?('visibility')
@webcvs = map['webcvs'] if map.has_key?('webcvs')
@meta_tags = map['meta_tags'] if map.has_key?('meta_tags')

if map.has_key?('rdoc_include')
@rdoc_include = sanitize_path map['rdoc_include']
Expand Down Expand Up @@ -475,7 +484,8 @@ def == other # :nodoc:
@template == other.template and
@title == other.title and
@visibility == other.visibility and
@webcvs == other.webcvs
@webcvs == other.webcvs and
@meta_tags == other.meta_tags
end

##
Expand Down Expand Up @@ -790,6 +800,15 @@ def parse argv
@skip_tests = false
end

opt.separator nil

opt.on("-mTAGS", "--meta-tags=TAGS",
nobu marked this conversation as resolved.
Show resolved Hide resolved
"Meta tags to be included in the HTML head") do |value|
@meta_tags = JSON.parse(value)
rescue JSON::ParserError, TypeError
raise OptionParser::InvalidArgument, "Invalid value for --meta-tags. Should be a JSON string"
end

opt.separator nil

opt.on("--extension=NEW=OLD", "-E",
Expand Down
21 changes: 15 additions & 6 deletions lib/rdoc/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
require_relative '../rdoc'
require 'rake'
require 'rake/tasklib'
require "json"

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

attr_accessor :external

##
# Meta tags to be included in the HTML header, such as keywords, description,
# author and others. This option is a hash where the keys are the meta tag
# name and the values are the content of the meta tag.
attr_accessor :meta_tags

##
# Create an RDoc task with the given name. See the RDoc::Task class overview
# for documentation.
Expand Down Expand Up @@ -201,6 +208,7 @@ def defaults
@template = nil
@generator = nil
@options = []
@meta_tags = nil
end

##
Expand Down Expand Up @@ -271,12 +279,13 @@ def define

def option_list
result = @options.dup
result << "-o" << @rdoc_dir
result << "--main" << main if main
result << "--markup" << markup if markup
result << "--title" << title if title
result << "-T" << template if template
result << '-f' << generator if generator
result << "-o" << @rdoc_dir
result << "--main" << main if main
result << "--markup" << markup if markup
result << "--title" << title if title
result << "-T" << template if template
result << '-f' << generator if generator
result << '--meta-tags' << meta_tags.to_json if meta_tags
result
end

Expand Down
16 changes: 16 additions & 0 deletions test/rdoc/test_rdoc_generator_darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,22 @@ def test_title_escape
assert_main_title(File.binread('index.html'), title)
end

def test_meta_tags
@options.meta_tags = {
"keywords" => "ruby,programming,software",
"description" => "RDoc is an awesome documentation generator for Ruby!"
}
@g.generate

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

assert_match("<meta name=\"keywords\" content=\"ruby,programming,software\">", content)
assert_match(
"<meta name=\"description\" content=\"RDoc is an awesome documentation generator for Ruby!\">",
content
)
Comment on lines +334 to +338
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert_match("<meta name=\"keywords\" content=\"ruby,programming,software\">", content)
assert_match(
"<meta name=\"description\" content=\"RDoc is an awesome documentation generator for Ruby!\">",
content
)
assert_include(content, '<meta name="keywords" content="ruby,programming,software">')
assert_include(
content,
'<meta name="description" content="RDoc is an awesome documentation generator for Ruby!">'
)

end

##
# Asserts that +filename+ has a link count greater than 1 if hard links to
# @tmpdir are supported.
Expand Down
1 change: 1 addition & 0 deletions test/rdoc/test_rdoc_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def test_to_yaml
'visibility' => :protected,
'webcvs' => nil,
'skip_tests' => true,
'meta_tags' => {},
}

assert_equal expected, coder
Expand Down