Skip to content

Commit 04ff2f8

Browse files
committed
Syntax highlighting with highlightjs
Also remove padding of first header in README, and remove some trailing whitespaces in style.scss
1 parent 8b0e943 commit 04ff2f8

File tree

6 files changed

+44
-18
lines changed

6 files changed

+44
-18
lines changed

src/web/crate_details.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ pub fn crate_details_handler(req: &mut Request) -> IronResult<Response> {
178178
.and_then(|details| {
179179
Page::new(details)
180180
.set_true("show_package_navigation")
181+
.set_true("javascript_highlightjs")
181182
.set_true("package_navigation_crate_tab")
182183
.to_resp("crate_details")
183184
})

src/web/source.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,28 +217,31 @@ pub fn source_browser_handler(req: &mut Request) -> IronResult<Response> {
217217
};
218218

219219

220-
let content = if let Some(file) = file {
220+
let (content, is_rust_source) = if let Some(file) = file {
221221
// serve the file with DatabaseFileHandler if file isn't text and not empty
222222
if !file.mime.starts_with("text") && !file.is_empty() {
223223
return Ok(file.serve());
224224
} else if file.mime.starts_with("text") && !file.is_empty() {
225-
String::from_utf8(file.content).ok()
225+
(String::from_utf8(file.content).ok(), file.path.ends_with(".rs"))
226226
} else {
227-
None
227+
(None, false)
228228
}
229229
} else {
230-
None
230+
(None, false)
231231
};
232232

233233
let list = FileList::from_path(&conn, &name, &version, &req_path);
234234

235235
let page = Page::new(list)
236236
.set_bool("show_parent_link", !req_path.is_empty())
237+
.set_true("javascript_highlightjs")
237238
.set_true("show_package_navigation")
238239
.set_true("package_source_tab");
239240

240241
if let Some(content) = content {
241-
page.set("file_content", &content).to_resp("source")
242+
page.set("file_content", &content)
243+
.set_bool("file_content_rust_source", is_rust_source)
244+
.to_resp("source")
242245
} else {
243246
page.to_resp("source")
244247
}

templates/footer.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
{{#if varsb.javascript_highlightjs}}<script type="text/javascript" charset="utf-8">hljs.initHighlighting();</script>{{/if}}
12
</body>
23
</html>

templates/header.hbs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
<link rel="stylesheet" href="/rustdoc/rustdoc-20160526-1.10.0-nightly-97e3a2401.css" type="text/css" media="all" />
1111
<link rel="stylesheet" href="/rustdoc/main-20160526-1.10.0-nightly-97e3a2401.css" type="text/css" media="all" />
1212
<link rel="stylesheet" href="/style.css" type="text/css" media="all" />
13+
{{#if varsb.javascript_highlightjs}}
14+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/styles/github.min.css" type="text/css" media="all" />
15+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/highlight.min.js" type="text/javascript" charset="utf-8"></script>
16+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/languages/rust.min.js" type="text/javascript" charset="utf-8"></script>
17+
{{/if}}
1318
<title>{{#if title}}{{title}} - {{/if}}{{#if content.metadata.name}}{{content.metadata.name}} {{content.metadata.version}} - {{/if}}Cratesfyi</title>
1419
</head>
1520
<body>

templates/source.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</div>
2929
{{#if ../varss.file_content}}
3030
<div class="pure-u-19-24">
31-
<pre>{{ ../varss.file_content }}</pre>
31+
<pre><code{{#if ../varsb.file_content_rust_source}} class="rust"{{/if}}>{{ ../varss.file_content }}</code></pre>
3232
</div>
3333
{{/if}}
3434
</div>

templates/style.scss

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ strong {
4141
font-weight: 500;
4242
}
4343

44-
pre {
45-
background-color: $color-background-code;
46-
padding: 14px;
47-
}
48-
4944
.pure-button-normal {
5045
background-color: #fff;
5146
box-sizing: border-box !important;
@@ -149,7 +144,7 @@ div.recent-releases-container {
149144
display: block;
150145
border-bottom: 1px solid $color-border;
151146
padding: .4em 1em;
152-
147+
153148
@media #{$media-lg} {
154149
padding: .4em 0;
155150
margin: 0 1em;
@@ -168,7 +163,7 @@ div.recent-releases-container {
168163
.description {
169164
font-family: $font-family-serif;
170165
font-weight: normal;
171-
@media #{$media-sm} {
166+
@media #{$media-sm} {
172167
font-size: 1em;
173168
white-space: nowrap;
174169
overflow: hidden;
@@ -177,7 +172,7 @@ div.recent-releases-container {
177172
}
178173

179174
.description:hover {
180-
@media #{$media-sm} {
175+
@media #{$media-sm} {
181176
overflow: visible;
182177
white-space: normal;
183178
}
@@ -304,8 +299,29 @@ div.package-page-container {
304299
}
305300
}
306301

307-
div.package-details p {
308-
font-family: $font-family-serif;
302+
div.package-details {
303+
p {
304+
font-family: $font-family-serif;
305+
}
306+
307+
h1:first-child,
308+
h2:first-child,
309+
h3:first-child,
310+
h4:first-child,
311+
h5:first-child,
312+
h6:first-child {
313+
margin-top: 0;
314+
}
315+
}
316+
317+
pre {
318+
background-color: inherit;
319+
margin: 0;
320+
padding: 0;
321+
322+
code {
323+
white-space: pre;
324+
}
309325
}
310326
}
311327

@@ -326,7 +342,7 @@ div.cratesfyi-package-container {
326342
margin: 0;
327343
padding: 0 0 20px 16px;
328344

329-
@media #{$media-sm} {
345+
@media #{$media-sm} {
330346
white-space: nowrap;
331347
overflow: hidden;
332348
text-overflow: ellipsis;
@@ -388,6 +404,6 @@ div.search-page-search-form {
388404
max-width: 300px;
389405
padding: .4em 1em;
390406
}
391-
407+
392408

393409
}

0 commit comments

Comments
 (0)