Skip to content

Commit a6d0571

Browse files
Sijasdogruyol
authored andcommittedApr 25, 2018
Add --canonical-base-url cli option to docs generator (#5990)
* Add <link rel="canonical"> to the latest API docs * Revert "Add <link rel="canonical"> to the latest API docs" This reverts commit fc5068e. * Add --canonical-base-url cli option to docs generator * Generate crystal docs with --canonical-base-url set * Refactor HeadTemplate to DRY-it up a lil’
1 parent 382d07e commit a6d0571

File tree

7 files changed

+29
-18
lines changed

7 files changed

+29
-18
lines changed
 

Diff for: ‎Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ compiler_spec: $(O)/compiler_spec ## Run compiler specs
9292

9393
.PHONY: docs
9494
docs: ## Generate standard library documentation
95-
$(BUILD_PATH) ./bin/crystal docs src/docs_main.cr
95+
$(BUILD_PATH) ./bin/crystal docs -b https://crystal-lang.org/api/latest src/docs_main.cr
9696

9797
.PHONY: crystal
9898
crystal: $(O)/crystal ## Build the compiler

Diff for: ‎src/compiler/crystal/command/docs.cr

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class Crystal::Command
77
private def docs
88
output_directory = File.join(".", "docs")
9+
canonical_base_url = nil
910

1011
OptionParser.parse(options) do |opts|
1112
opts.banner = <<-'BANNER'
@@ -20,6 +21,10 @@ class Crystal::Command
2021
output_directory = value
2122
end
2223

24+
opts.on("--canonical-base-url=URL", "-b URL", "Set the canonical base url") do |value|
25+
canonical_base_url = value
26+
end
27+
2328
opts.on("-h", "--help", "Show this message") do
2429
puts opts
2530
exit
@@ -41,6 +46,6 @@ class Crystal::Command
4146
compiler.wants_doc = true
4247
result = compiler.top_level_semantic sources
4348

44-
Doc::Generator.new(result.program, included_dirs, output_directory).run
49+
Doc::Generator.new(result.program, included_dirs, output_directory, canonical_base_url).run
4550
end
4651
end

Diff for: ‎src/compiler/crystal/tools/doc/generator.cr

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Crystal::Doc::Generator
2828
},
2929
}
3030

31-
def initialize(@program : Program, @included_dirs : Array(String), @output_dir : String)
31+
def initialize(@program : Program, @included_dirs : Array(String), @output_dir : String, @canonical_base_url : String?)
3232
@base_dir = Dir.current.chomp
3333
@types = {} of Crystal::Type => Doc::Type
3434
@repo_name = ""
@@ -74,7 +74,7 @@ class Crystal::Doc::Generator
7474
body = ""
7575
end
7676

77-
File.write File.join(@output_dir, "index.html"), MainTemplate.new(body, types, repository_name)
77+
File.write File.join(@output_dir, "index.html"), MainTemplate.new(body, types, repository_name, @canonical_base_url)
7878

7979
main_index = Main.new(raw_body, Type.new(self, @program), repository_name)
8080
File.write File.join(@output_dir, "index.json"), main_index
@@ -97,7 +97,7 @@ class Crystal::Doc::Generator
9797
filename = File.join(dir, "#{type.name}.html")
9898
end
9999

100-
File.write filename, TypeTemplate.new(type, all_types)
100+
File.write filename, TypeTemplate.new(type, all_types, @canonical_base_url)
101101

102102
next if type.program?
103103

Diff for: ‎src/compiler/crystal/tools/doc/html/_head.html

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
<meta charset="utf-8" />
1+
<meta charset="utf-8">
22
<meta http-equiv="X-UA-Compatible" content="IE=edge">
33
<meta name="generator" content="Crystal Docs <%= Crystal::VERSION %>">
44

5-
<link href="<%= base_path %>css/style.css" rel="stylesheet" type="text/css" />
5+
<%- if base_url = canonical_base_url -%>
6+
<%- if path = type.try(&.path) -%>
7+
<%- base_url = File.join(base_url, path) -%>
8+
<%- end -%>
9+
<link rel="canonical" href="<%= base_url %>">
10+
<% end -%>
11+
<%- base_path = type.try(&.path_to("")) -%>
12+
13+
<link href="<%= base_path %>css/style.css" rel="stylesheet" type="text/css">
14+
615
<script type="text/javascript" src="<%= base_path %>js/doc.js"></script>
16+
<script type="text/javascript">
17+
CrystalDoc.base_path = "<%= base_path %>";
18+
</script>

Diff for: ‎src/compiler/crystal/tools/doc/html/main.html

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<%= HeadTemplate.new("") %>
4+
<%= HeadTemplate.new(nil, canonical_base_url) %>
55
<meta id="repository-name" content="<%= repository_name %>">
66
<title>README - <%= repository_name %></title>
7-
<script type="text/javascript">
8-
CrystalDoc.base_path = "";
9-
</script>
107
</head>
118
<body>
129

Diff for: ‎src/compiler/crystal/tools/doc/html/type.html

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<%= HeadTemplate.new(type.path_to "") %>
4+
<%= HeadTemplate.new(type, canonical_base_url) %>
55
<meta id="repository-name" content="<%= type.repository_name %>">
66
<title><%= type.full_name %> - <%= type.repository_name %></title>
7-
<script type="text/javascript">
8-
CrystalDoc.base_path = "<%= type.path_to "" %>";
9-
</script>
107
</head>
118
<body>
129

Diff for: ‎src/compiler/crystal/tools/doc/templates.cr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require "ecr/macros"
22

33
module Crystal::Doc
4-
record TypeTemplate, type : Type, types : Array(Type) do
4+
record TypeTemplate, type : Type, types : Array(Type), canonical_base_url : String? do
55
ECR.def_to_s "#{__DIR__}/html/type.html"
66
end
77

@@ -25,11 +25,11 @@ module Crystal::Doc
2525
ECR.def_to_s "#{__DIR__}/html/_other_types.html"
2626
end
2727

28-
record MainTemplate, body : String, types : Array(Type), repository_name : String do
28+
record MainTemplate, body : String, types : Array(Type), repository_name : String, canonical_base_url : String? do
2929
ECR.def_to_s "#{__DIR__}/html/main.html"
3030
end
3131

32-
record HeadTemplate, base_path : String do
32+
record HeadTemplate, type : Type?, canonical_base_url : String? do
3333
ECR.def_to_s "#{__DIR__}/html/_head.html"
3434
end
3535

0 commit comments

Comments
 (0)
Please sign in to comment.