1- //! Render README files to HTML.
1+ //! Render Markdown files to HTML.
22
33use ammonia:: { Builder , UrlRelative , UrlRelativeEvaluate } ;
44use comrak:: nodes:: { AstNode , NodeValue } ;
@@ -15,7 +15,7 @@ struct MarkdownRenderer<'a> {
1515impl < ' 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.
128128struct 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`.
221221fn 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.
228228static 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 "[](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 \n is my friend\n " , f, None ) ,
502+ text_to_html ( "<script>lobster</script>\n \n is my friend\n " , f, None ) ,
503503 "<script>lobster</script><br>\n <br>\n is my friend<br>\n "
504504 ) ;
505505 }
0 commit comments