Skip to content

Commit 90c3a7a

Browse files
straight-shootasdogruyol
authored andcommitted
Refactor docs generator HTML scaffold (#5816)
* Rename partial templates with leading underscore * Add _head.html partial to dry up html head * Add _sidebar.html partial, remove style IDs and use flexbox This dries up sidebar code. Element IDs for CSS selectors are replaced with classes to reduce specificity in stylesheets (avoid specificity wars). Flexbox is better than absolute positioning as it allows for more flexible layout and dimensions.
1 parent 9df0315 commit 90c3a7a

12 files changed

+99
-86
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<meta charset="utf-8" />
2+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
3+
<meta name="generator" content="Crystal Docs <%= Crystal::VERSION %>">
4+
5+
<link href="<%= base_path %>css/style.css" rel="stylesheet" type="text/css" />
6+
<script type="text/javascript" src="<%= base_path %>js/doc.js"></script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<div class="sidebar">
2+
<div class="sidebar-header">
3+
<div class="search-box">
4+
<input type="search" class="search-input" placeholder="Search..." spellcheck="false">
5+
</div>
6+
7+
<div class="repository-links">
8+
<a href="<%= current_type.try(&.path_to("")) %>index.html">README</a>
9+
</div>
10+
</div>
11+
12+
<div class="search-results" class="hidden">
13+
<ul class="search-list"></ul>
14+
</div>
15+
16+
<div class="types-list">
17+
<%= ListItemsTemplate.new(types, current_type) %>
18+
</div>
19+
</div>

src/compiler/crystal/tools/doc/html/css/style.css

+50-38
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,28 @@ h2 {
4040
padding-bottom: 5px;
4141
}
4242

43-
#types-list, #main-content {
44-
position: absolute;
45-
top: 0;
46-
bottom: 0;
43+
body {
44+
display: flex;
45+
}
46+
47+
.sidebar, .main-content {
4748
overflow: auto;
4849
}
4950

50-
#types-list {
51-
left: 0;
52-
width: 20%;
51+
.sidebar {
52+
width: 30em;
53+
color: #F8F4FD;
5354
background-color: #2E1052;
5455
padding: 0 0 30px;
5556
box-shadow: inset -3px 0 4px rgba(0,0,0,.35);
57+
line-height: 1.2;
5658
}
5759

58-
#types-list #search-box {
60+
.sidebar .search-box {
5961
padding: 8px 9px;
6062
}
6163

62-
#types-list input {
64+
.sidebar input {
6365
display: block;
6466
box-sizing: border-box;
6567
margin: 0;
@@ -75,82 +77,92 @@ h2 {
7577
transition: box-shadow .12s;
7678
}
7779

78-
#types-list input:focus {
80+
.sidebar input:focus {
7981
box-shadow: 0px 5px 6px rgba(0,0,0,.5);
8082
}
8183

82-
#types-list input::-webkit-input-placeholder { /* Chrome/Opera/Safari */
84+
.sidebar input::-webkit-input-placeholder { /* Chrome/Opera/Safari */
8385
color: #C8C8C8;
8486
font-size: 14px;
8587
text-indent: 2px;
8688
}
8789

88-
#types-list input::-moz-placeholder { /* Firefox 19+ */
90+
.sidebar input::-moz-placeholder { /* Firefox 19+ */
8991
color: #C8C8C8;
9092
font-size: 14px;
9193
text-indent: 2px;
9294
}
9395

94-
#types-list input:-ms-input-placeholder { /* IE 10+ */
96+
.sidebar input:-ms-input-placeholder { /* IE 10+ */
9597
color: #C8C8C8;
9698
font-size: 14px;
9799
text-indent: 2px;
98100
}
99101

100-
#types-list input:-moz-placeholder { /* Firefox 18- */
102+
.sidebar input:-moz-placeholder { /* Firefox 18- */
101103
color: #C8C8C8;
102104
font-size: 14px;
103105
text-indent: 2px;
104106
}
105107

106-
#types-list ul {
108+
.sidebar ul {
107109
margin: 0;
108110
padding: 0;
109111
list-style: none outside;
110112
}
111113

112-
#types-list li {
114+
.sidebar li {
113115
display: block;
114116
position: relative;
115117
}
116118

117-
#types-list li.hide {
119+
.types-list li.hide {
118120
display: none;
119121
}
120122

121-
#types-list a,
122-
.search_result .search-result__title > a {
123-
display: block;
123+
.sidebar a {
124124
text-decoration: none;
125-
color: #F8F4FD;
125+
color: inherit;
126126
transition: color .14s;
127127
}
128128
.types-list a {
129+
display: block;
129130
padding: 5px 15px 5px 30px;
130131
}
131132

132-
#types-list a:focus {
133+
.types-list {
134+
display: block;
135+
}
136+
137+
.sidebar a:focus {
133138
outline: 1px solid #D1B7F1;
134139
}
135140

136-
#types-list .current > a,
137-
#types-list a:hover {
141+
.types-list a {
142+
padding: 5px 15px 5px 30px;
143+
}
144+
145+
.sidebar .current > a,
146+
.sidebar a:hover {
138147
color: #866BA6;
139148
}
140149

141-
#types-list li ul {
150+
.repository-links {
151+
padding: 5px 15px 5px 30px;
152+
}
153+
154+
.types-list li ul {
142155
overflow: hidden;
143156
height: 0;
144157
max-height: 0;
145158
transition: 1s ease-in-out;
146159
}
147160

148-
149-
#types-list li.parent {
161+
.types-list li.parent {
150162
padding-left: 30px;
151163
}
152164

153-
#types-list li.parent::before {
165+
.types-list li.parent::before {
154166
box-sizing: border-box;
155167
content: "▼";
156168
display: block;
@@ -169,23 +181,22 @@ h2 {
169181
}
170182

171183

172-
#types-list li.parent > a {
184+
.types-list li.parent > a {
173185
padding-left: 0;
174186
}
175187

176-
#types-list li.parent.open::before {
188+
.types-list li.parent.open::before {
177189
transform: rotateZ(0);
178190
}
179191

180-
#types-list li.open > ul {
192+
.types-list li.open > ul {
181193
height: auto;
182194
max-height: 1000em;
183195
}
184196

185-
#main-content {
197+
.main-content {
186198
padding: 0 30px 30px 30px;
187-
left: 20%;
188-
right: 0;
199+
width: 100%;
189200
}
190201

191202
.kind {
@@ -487,6 +498,7 @@ span.flag.purple {
487498
}
488499
.search-results .search-result__title > a {
489500
padding: 0;
501+
display: block;
490502
}
491503
.search-result__title > a > .args {
492504
color: #dddddd;
@@ -536,13 +548,13 @@ span.flag.purple {
536548
.js-modal-visible .modal-background {
537549
display: flex;
538550
}
539-
#types-list,
540-
#main-content {
551+
.types-list,
552+
.main-content {
541553
filter: blur(0);
542554
transition: filter 200ms;
543555
}
544-
.js-modal-visible #types-list,
545-
.js-modal-visible #main-content {
556+
.js-modal-visible .types-list,
557+
.js-modal-visible .main-content {
546558
filter: blur(2px);
547559
}
548560
.modal-background {

src/compiler/crystal/tools/doc/html/js/doc.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ document.addEventListener('DOMContentLoaded', function() {
1919
};
2020
}
2121

22-
var repositoryName = document.getElementById('repository-name').getAttribute('content');
23-
var typesList = document.getElementById('types-list');
24-
var searchInput = document.getElementById('search-input');
25-
var parents = document.querySelectorAll('#types-list li.parent');
22+
var repositoryName = document.querySelector('#repository-name').getAttribute('content');
23+
var typesList = document.querySelector('.types-list');
24+
var searchInput = document.querySelector('.search-input');
25+
var parents = document.querySelectorAll('.types-list li.parent');
2626

2727
var setPersistentSearchQuery = function(value){
2828
sessionStorage.setItem(repositoryName + '::search-input:value', value);
@@ -56,7 +56,7 @@ document.addEventListener('DOMContentLoaded', function() {
5656
window.focus();
5757
}
5858

59-
var navigator = new Navigator(document.querySelector('#types-list'), searchInput, document.querySelector(".search-results"), leaveSearchScope);
59+
var navigator = new Navigator(document.querySelector('.types-list'), searchInput, document.querySelector(".search-results"), leaveSearchScope);
6060

6161
CrystalDoc.loadIndex();
6262
var searchTimeout;

src/compiler/crystal/tools/doc/html/main.html

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<meta charset="utf-8" />
4+
<%= HeadTemplate.new("") %>
55
<meta id="repository-name" content="<%= repository_name %>">
6-
<link href="css/style.css" rel="stylesheet" type="text/css" />
7-
<script type="text/javascript" src="js/doc.js"></script>
86
<title>README - <%= repository_name %></title>
97
<script type="text/javascript">
108
CrystalDoc.base_path = "";
119
</script>
1210
</head>
1311
<body>
1412

15-
<div id="types-list">
16-
<div id="search-box">
17-
<input type="search" id="search-input" placeholder="Search..." spellcheck="false">
18-
</div>
13+
<%= SidebarTemplate.new(repository_name, types, nil) %>
1914

20-
<a href="index.html">README</a>
21-
22-
<div class="search-results" class="hidden">
23-
<ul class="search-list"></ul>
24-
</div>
25-
26-
<div class="types-list">
27-
<%= ListItemsTemplate.new(types, nil) %>
28-
</div>
29-
</div>
30-
31-
<div id="main-content">
15+
<div class="main-content">
3216
<%= body %>
3317
</div>
3418
</body>

src/compiler/crystal/tools/doc/html/type.html

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<meta charset="utf-8" />
4+
<%= HeadTemplate.new(type.path_to "") %>
55
<meta id="repository-name" content="<%= type.repository_name %>">
6-
<link href="<%= type.path_to "css/style.css" %>" rel="stylesheet" type="text/css" />
7-
<script type="text/javascript" src="<%= type.path_to "js/doc.js" %>"></script>
86
<title><%= type.full_name %> - <%= type.repository_name %></title>
97
<script type="text/javascript">
108
CrystalDoc.base_path = "<%= type.path_to "" %>";
119
</script>
1210
</head>
1311
<body>
1412

15-
<div id="types-list">
16-
<div id="search-box">
17-
<input type="search" id="search-input" placeholder="Search..." spellcheck="false">
18-
</div>
13+
<%= SidebarTemplate.new(type.repository_name, types, type) %>
1914

20-
<a href="<%= type.path_to("index.html") %>">README</a>
21-
22-
<div class="search-results" class="hidden">
23-
<ul class="search-list"></ul>
24-
</div>
25-
26-
<div class="types-list">
27-
<%= ListItemsTemplate.new(types, type) %>
28-
</div>
29-
</div>
30-
31-
<div id="main-content">
15+
<div class="main-content">
3216
<h1 class="type-name">
3317
<% if type.program? %>
3418
<%= type.full_name %>

src/compiler/crystal/tools/doc/templates.cr

+13-5
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,37 @@ module Crystal::Doc
66
end
77

88
record ListItemsTemplate, types : Array(Type), current_type : Type? do
9-
ECR.def_to_s "#{__DIR__}/html/list_items.html"
9+
ECR.def_to_s "#{__DIR__}/html/_list_items.html"
1010
end
1111

1212
record MethodSummaryTemplate, title : String, methods : Array(Method) | Array(Macro) do
13-
ECR.def_to_s "#{__DIR__}/html/method_summary.html"
13+
ECR.def_to_s "#{__DIR__}/html/_method_summary.html"
1414
end
1515

1616
record MethodDetailTemplate, title : String, methods : Array(Method) | Array(Macro) do
17-
ECR.def_to_s "#{__DIR__}/html/method_detail.html"
17+
ECR.def_to_s "#{__DIR__}/html/_method_detail.html"
1818
end
1919

2020
record MethodsInheritedTemplate, type : Type, ancestor : Type, methods : Array(Method), label : String do
21-
ECR.def_to_s "#{__DIR__}/html/methods_inherited.html"
21+
ECR.def_to_s "#{__DIR__}/html/_methods_inherited.html"
2222
end
2323

2424
record OtherTypesTemplate, title : String, type : Type, other_types : Array(Type) do
25-
ECR.def_to_s "#{__DIR__}/html/other_types.html"
25+
ECR.def_to_s "#{__DIR__}/html/_other_types.html"
2626
end
2727

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

32+
record HeadTemplate, base_path : String do
33+
ECR.def_to_s "#{__DIR__}/html/_head.html"
34+
end
35+
36+
record SidebarTemplate, repository_name : String, types : Array(Type), current_type : Type? do
37+
ECR.def_to_s "#{__DIR__}/html/_sidebar.html"
38+
end
39+
3240
struct JsTypeTemplate
3341
ECR.def_to_s "#{__DIR__}/html/js/doc.js"
3442
end

0 commit comments

Comments
 (0)