Skip to content

Commit

Permalink
Rollup merge of rust-lang#37050 - frewsxcv:librustdoc, r=alexcrichton
Browse files Browse the repository at this point in the history
librustdoc refactoring and cleanup.
  • Loading branch information
alexcrichton committed Oct 12, 2016
2 parents 4e65489 + e4f066f commit 5ac7f4f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 38 deletions.
85 changes: 50 additions & 35 deletions src/librustdoc/externalfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,79 @@
use std::fs::File;
use std::io::prelude::*;
use std::io;
use std::path::{PathBuf, Path};
use std::path::Path;
use std::str;

#[derive(Clone)]
pub struct ExternalHtml{
/// Content that will be included inline in the <head> section of a
/// rendered Markdown file or generated documentation
pub in_header: String,
/// Content that will be included inline between <body> and the content of
/// a rendered Markdown file or generated documentation
pub before_content: String,
/// Content that will be included inline between the content and </body> of
/// a rendered Markdown file or generated documentation
pub after_content: String
}

impl ExternalHtml {
pub fn load(in_header: &[String], before_content: &[String], after_content: &[String])
-> Option<ExternalHtml> {
match (load_external_files(in_header),
load_external_files(before_content),
load_external_files(after_content)) {
(Some(ih), Some(bc), Some(ac)) => Some(ExternalHtml {
in_header: ih,
before_content: bc,
after_content: ac
}),
_ => None
}
load_external_files(in_header)
.and_then(|ih|
load_external_files(before_content)
.map(|bc| (ih, bc))
)
.and_then(|(ih, bc)|
load_external_files(after_content)
.map(|ac| (ih, bc, ac))
)
.map(|(ih, bc, ac)|
ExternalHtml {
in_header: ih,
before_content: bc,
after_content: ac,
}
)
}
}

pub fn load_string(input: &Path) -> io::Result<Option<String>> {
let mut f = File::open(input)?;
let mut d = Vec::new();
f.read_to_end(&mut d)?;
Ok(str::from_utf8(&d).map(|s| s.to_string()).ok())
pub enum LoadStringError {
ReadFail,
BadUtf8,
}

macro_rules! load_or_return {
($input: expr, $cant_read: expr, $not_utf8: expr) => {
{
let input = PathBuf::from(&$input[..]);
match ::externalfiles::load_string(&input) {
Err(e) => {
let _ = writeln!(&mut io::stderr(),
"error reading `{}`: {}", input.display(), e);
return $cant_read;
}
Ok(None) => {
let _ = writeln!(&mut io::stderr(),
"error reading `{}`: not UTF-8", input.display());
return $not_utf8;
}
Ok(Some(s)) => s
}
pub fn load_string<P: AsRef<Path>>(file_path: P) -> Result<String, LoadStringError> {
let file_path = file_path.as_ref();
let mut contents = vec![];
let result = File::open(file_path)
.and_then(|mut f| f.read_to_end(&mut contents));
if let Err(e) = result {
let _ = writeln!(&mut io::stderr(),
"error reading `{}`: {}",
file_path.display(), e);
return Err(LoadStringError::ReadFail);
}
match str::from_utf8(&contents) {
Ok(s) => Ok(s.to_string()),
Err(_) => {
let _ = writeln!(&mut io::stderr(),
"error reading `{}`: not UTF-8",
file_path.display());
Err(LoadStringError::BadUtf8)
}
}
}

pub fn load_external_files(names: &[String]) -> Option<String> {
fn load_external_files(names: &[String]) -> Option<String> {
let mut out = String::new();
for name in names {
out.push_str(&*load_or_return!(&name, None, None));
let s = match load_string(name) {
Ok(s) => s,
Err(_) => return None,
};
out.push_str(&s);
out.push('\n');
}
Some(out)
Expand Down
14 changes: 11 additions & 3 deletions src/librustdoc/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use testing;
use rustc::session::search_paths::SearchPaths;
use rustc::session::config::Externs;

use externalfiles::ExternalHtml;
use externalfiles::{ExternalHtml, LoadStringError, load_string};

use html::render::reset_ids;
use html::escape::Escape;
Expand Down Expand Up @@ -58,7 +58,11 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
css.push_str(&s)
}

let input_str = load_or_return!(input, 1, 2);
let input_str = match load_string(input) {
Ok(s) => s,
Err(LoadStringError::ReadFail) => return 1,
Err(LoadStringError::BadUtf8) => return 2,
};
let playground = matches.opt_str("markdown-playground-url");
if playground.is_some() {
markdown::PLAYGROUND_KRATE.with(|s| { *s.borrow_mut() = Some(None); });
Expand Down Expand Up @@ -144,7 +148,11 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
/// Run any tests/code examples in the markdown file `input`.
pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
mut test_args: Vec<String>) -> isize {
let input_str = load_or_return!(input, 1, 2);
let input_str = match load_string(input) {
Ok(s) => s,
Err(LoadStringError::ReadFail) => return 1,
Err(LoadStringError::BadUtf8) => return 2,
};

let mut opts = TestOptions::default();
opts.no_crate_inject = true;
Expand Down

0 comments on commit 5ac7f4f

Please sign in to comment.