Skip to content

Commit c346b95

Browse files
committed
Auto merge of #3933 - Turbo87:markdown-no-readme, r=hi-rustin
markdown: Remove `README` references This module can be used for any kind of markdown file, not just `README.md` files :)
2 parents b070d14 + ad5eb11 commit c346b95

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

src/admin/render_readmes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
use std::{io::Read, path::Path, sync::Arc, thread};
88

99
use chrono::{TimeZone, Utc};
10-
use cio_markdown::readme_to_html;
10+
use cio_markdown::text_to_html;
1111
use clap::Clap;
1212
use diesel::{dsl::any, prelude::*};
1313
use flate2::read::GzDecoder;
@@ -212,7 +212,7 @@ fn get_readme(
212212
krate_name, version.num, manifest.package.readme?
213213
);
214214
let contents = find_file_by_path(&mut entries, Path::new(&path), version, krate_name);
215-
readme_to_html(
215+
text_to_html(
216216
&contents,
217217
manifest
218218
.package

src/markdown/lib.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Render README files to HTML.
1+
//! Render Markdown files to HTML.
22
33
use ammonia::{Builder, UrlRelative, UrlRelativeEvaluate};
44
use comrak::nodes::{AstNode, NodeValue};
@@ -15,7 +15,7 @@ struct MarkdownRenderer<'a> {
1515
impl<'a> MarkdownRenderer<'a> {
1616
/// Creates a new renderer instance.
1717
///
18-
/// Per `readme_to_html`, `base_url` is the base URL prepended to any
18+
/// Per `text_to_html`, `base_url` is the base URL prepended to any
1919
/// relative links in the input document. See that function for more detail.
2020
fn new(base_url: Option<&'a str>, base_dir: &'a str) -> MarkdownRenderer<'a> {
2121
let allowed_classes = hashmap(&[(
@@ -124,7 +124,7 @@ fn canon_base_url(mut base_url: String) -> String {
124124
base_url
125125
}
126126

127-
/// Sanitize relative URLs in README files.
127+
/// Sanitize relative URLs in Markdown files.
128128
struct SanitizeUrl {
129129
base_url: Option<String>,
130130
base_dir: String,
@@ -217,18 +217,18 @@ impl UrlRelativeEvaluate for SanitizeUrl {
217217
}
218218

219219
/// Renders Markdown text to sanitized HTML with a given `base_url`.
220-
/// See `readme_to_html` for the interpretation of `base_url`.
220+
/// See `text_to_html` for the interpretation of `base_url`.
221221
fn markdown_to_html(text: &str, base_url: Option<&str>, base_dir: &str) -> String {
222222
let renderer = MarkdownRenderer::new(base_url, base_dir);
223223
renderer.to_html(text)
224224
}
225225

226-
/// Any readme with a filename ending in one of these extensions will be rendered as Markdown.
227-
/// Note we also render a readme as Markdown if _no_ extension is on the filename.
226+
/// Any file with a filename ending in one of these extensions will be rendered as Markdown.
227+
/// Note we also render a file as Markdown if _no_ extension is on the filename.
228228
static MARKDOWN_EXTENSIONS: [&str; 7] =
229229
["md", "markdown", "mdown", "mdwn", "mkd", "mkdn", "mkdown"];
230230

231-
/// Renders a readme to sanitized HTML. An appropriate rendering method is chosen depending
231+
/// Renders a text file to sanitized HTML. An appropriate rendering method is chosen depending
232232
/// on the extension of the supplied `filename`.
233233
///
234234
/// The returned text will not contain any harmful HTML tag or attribute (such as iframe,
@@ -242,22 +242,22 @@ static MARKDOWN_EXTENSIONS: [&str; 7] =
242242
/// # Examples
243243
///
244244
/// ```
245-
/// use cio_markdown::readme_to_html;
245+
/// use cio_markdown::text_to_html;
246246
///
247247
/// let text = "[Rust](https://rust-lang.org/) is an awesome *systems programming* language!";
248-
/// let rendered = readme_to_html(text, "README.md", None);
248+
/// let rendered = text_to_html(text, "README.md", None);
249249
/// ```
250-
pub fn readme_to_html(text: &str, readme_path: &str, base_url: Option<&str>) -> String {
251-
let readme_path = Path::new(readme_path);
252-
let readme_dir = readme_path.parent().and_then(|p| p.to_str()).unwrap_or("");
250+
pub fn text_to_html(text: &str, path: &str, base_url: Option<&str>) -> String {
251+
let path = Path::new(path);
252+
let base_dir = path.parent().and_then(|p| p.to_str()).unwrap_or("");
253253

254-
if readme_path.extension().is_none() {
255-
return markdown_to_html(text, base_url, readme_dir);
254+
if path.extension().is_none() {
255+
return markdown_to_html(text, base_url, base_dir);
256256
}
257257

258-
if let Some(ext) = readme_path.extension().and_then(|ext| ext.to_str()) {
258+
if let Some(ext) = path.extension().and_then(|ext| ext.to_str()) {
259259
if MARKDOWN_EXTENSIONS.contains(&ext.to_lowercase().as_str()) {
260-
return markdown_to_html(text, base_url, readme_dir);
260+
return markdown_to_html(text, base_url, base_dir);
261261
}
262262
}
263263

@@ -454,10 +454,10 @@ mod tests {
454454

455455
#[test]
456456
fn absolute_links_dont_get_resolved() {
457-
let readme_text =
457+
let text =
458458
"[![Crates.io](https://img.shields.io/crates/v/clap.svg)](https://crates.io/crates/clap)";
459459
let repository = "https://github.com/kbknapp/clap-rs/";
460-
let result = markdown_to_html(readme_text, Some(repository), "");
460+
let result = markdown_to_html(text, Some(repository), "");
461461

462462
assert_eq!(
463463
result,
@@ -466,7 +466,7 @@ mod tests {
466466
}
467467

468468
#[test]
469-
fn readme_to_html_renders_markdown() {
469+
fn text_to_html_renders_markdown() {
470470
for f in &[
471471
"README",
472472
"readme.md",
@@ -476,30 +476,30 @@ mod tests {
476476
"s1/s2/readme.md",
477477
] {
478478
assert_eq!(
479-
readme_to_html("*lobster*", f, None),
479+
text_to_html("*lobster*", f, None),
480480
"<p><em>lobster</em></p>\n"
481481
);
482482
}
483483

484484
assert_eq!(
485-
readme_to_html("*[lobster](docs/lobster)*", "readme.md", Some("https://github.com/rust-lang/test")),
485+
text_to_html("*[lobster](docs/lobster)*", "readme.md", Some("https://github.com/rust-lang/test")),
486486
"<p><em><a href=\"https://github.com/rust-lang/test/blob/HEAD/docs/lobster\" rel=\"nofollow noopener noreferrer\">lobster</a></em></p>\n"
487487
);
488488
assert_eq!(
489-
readme_to_html("*[lobster](docs/lobster)*", "s/readme.md", Some("https://github.com/rust-lang/test")),
489+
text_to_html("*[lobster](docs/lobster)*", "s/readme.md", Some("https://github.com/rust-lang/test")),
490490
"<p><em><a href=\"https://github.com/rust-lang/test/blob/HEAD/s/docs/lobster\" rel=\"nofollow noopener noreferrer\">lobster</a></em></p>\n"
491491
);
492492
assert_eq!(
493-
readme_to_html("*[lobster](docs/lobster)*", "s1/s2/readme.md", Some("https://github.com/rust-lang/test")),
493+
text_to_html("*[lobster](docs/lobster)*", "s1/s2/readme.md", Some("https://github.com/rust-lang/test")),
494494
"<p><em><a href=\"https://github.com/rust-lang/test/blob/HEAD/s1/s2/docs/lobster\" rel=\"nofollow noopener noreferrer\">lobster</a></em></p>\n"
495495
);
496496
}
497497

498498
#[test]
499-
fn readme_to_html_renders_other_things() {
499+
fn text_to_html_renders_other_things() {
500500
for f in &["readme.exe", "readem.org", "blah.adoc"] {
501501
assert_eq!(
502-
readme_to_html("<script>lobster</script>\n\nis my friend\n", f, None),
502+
text_to_html("<script>lobster</script>\n\nis my friend\n", f, None),
503503
"&lt;script&gt;lobster&lt;/script&gt;<br>\n<br>\nis my friend<br>\n"
504504
);
505505
}

src/render.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Render README files to HTML.
22
3-
use cio_markdown::readme_to_html;
3+
use cio_markdown::text_to_html;
44
use swirl::PerformError;
55

66
use crate::background_jobs::Environment;
@@ -18,7 +18,7 @@ pub fn render_and_upload_readme(
1818
use crate::schema::*;
1919
use diesel::prelude::*;
2020

21-
let rendered = readme_to_html(&text, &readme_path, base_url.as_deref());
21+
let rendered = text_to_html(&text, &readme_path, base_url.as_deref());
2222

2323
conn.transaction(|| {
2424
Version::record_readme_rendering(version_id, conn)?;

0 commit comments

Comments
 (0)