Skip to content

Conversation

@mbu93
Copy link
Contributor

@mbu93 mbu93 commented May 6, 2017

The wiki now automatically creates links to the files in the file-folder of the generated site. If there is no file-folder found it will initially create one and add the neccessary paths(same as the html paths). To add a link files can simply be dropped. In order to do so I extended the Server Responses for a few filetypes.

@coveralls
Copy link

Coverage Status

Coverage decreased (-9.6%) to 31.847% when pulling 2cacc34 on mbu93:master into af0ab2e on rust-leipzig:master.

Copy link
Member

@saschagrunert saschagrunert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for the contribution. We need some rework to this pull request.

src/lib.rs Outdated
/// Gather new content
let md_path = PathBuf::from(&directory).join("**").join("*.md");
if !Path::new(&directory).is_dir() {
if !Path::new(&directory).is_dir () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this change :)

src/lib.rs Outdated
info!("Creating directory for file input: {}.", file_directory);
fs::create_dir(file_directory);
}
for path in self.paths.clone(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This clone can be avoided by iterating over the references: for path in &self.paths

src/lib.rs Outdated
let mut count = 0;

//create the path for the html site or just find it
let page_files_path = format!("{}/{}", file_directory, path.to_str().unwrap()).replace(".md","");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend to use the join based approach here.

src/lib.rs Outdated
//create the path for the html site or just find it
let page_files_path = format!("{}/{}", file_directory, path.to_str().unwrap()).replace(".md","");
if !Path::new(page_files_path.as_str()).exists(){
info!("Creating directory for {}'s files: {}.", path.to_str().unwrap(), page_files_path.as_str());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unwraps can lead into panics, please avoid these by using the display method.

src/lib.rs Outdated
//add the path
let file_path = Path::new(page_files_path.as_str());
//read all the files
let files = file_path.read_dir().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add error handling here as well.

src/lib.rs Outdated
pub fn serve(&self, output_directory: &str) -> Result<()> {
// Create a default listening address
let addr = "localhost:5000";
let addr = "localhost:3000";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend to use a higher ranged port here. The address should be a static string somewhere in the lib, since we have no configuration options.

src/lib.rs Outdated
let output_directory_string = output_directory.to_owned();

//maybe this shoul be stored in a separated file?
let pdf_mime: Mime = "application/pdf".parse().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid "live" unwrapping, this can be done before main in a static string or by using the lazy static crate.

src/lib.rs Outdated
if !path.exists() {
return Ok(Response::with(status::InternalServerError));
return Ok(Response::with((ContentType::html().0,
status::NotFound, html_err)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong indent my dear 🐻

src/lib.rs Outdated
}

let mut f = match File::open(path) {
let mut f = match File::open(path.clone()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can also open a reference here to avoid cloning.

src/lib.rs Outdated
status::NotFound, html_err))),
};

match path.clone().to_str(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can avoid this clone too.

Copy link
Member

@Drogglbecher Drogglbecher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All in all there are many unwraps in waiting to be purged ;)

src/lib.rs Outdated
"\n<a href='http://localhost:3000/files/{}/{}'>{}</a><br>\n",
path.to_str().unwrap().replace(".md", ""),
current_entry.file_name().into_string().unwrap().replace("\n","."),
current_entry.file_name().into_string().unwrap()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can try to replace these 3 unwraps above with ok_or_else

src/lib.rs Outdated

let mut html_file = OpenOptions::new().read(true).write(true).create(true).
open(format!("output/{}", path.to_str().unwrap().
replace(".md", ".html"))).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These unwraps can be handled too

src/lib.rs Outdated
}
//check if there are no files attached
if count == 0 {
info!("No files found so far for {}. Simply add your files to the folders in 'files'.", path.to_str().unwrap())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For better reading some line breaks would be good. In l. 186 too.

src/lib.rs Outdated

Ok(())
}
/// read all the files from current paths
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description not really describes what this function actually does (and to be honest, i have no idea too)

help: Enable integrated HTTP server to serve contents from output directory.
long: www
short: w
- file_directory:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since input and output directory we have now additionally a file directory and as a user I would have no idea wherefore this is and the description is not really helpful.

…mited. Better doc comments and minor fixes.
Copy link
Member

@saschagrunert saschagrunert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now, what do you think @Drogglbecher

@Drogglbecher
Copy link
Member

Yes, thanks. Please merge in the current master and resolve the failing check, after that I will have a look, it's a little bit hard to read it this way. There are some "coding style issues" too but we can resolve them later ;) @mbu93

MfLabrepo@gmail.com added 2 commits June 17, 2017 01:37
Copy link
Member

@Drogglbecher Drogglbecher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do cargo update, I think this should resolve the problems with nightly build ;)

After these changes the rest looks ok so far. Thanks ;)

for entry in files {
let current_entry = match entry {
Ok(entry) => entry,
_ => panic!("Couldn't read the path!"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better return an error with a meaningful description here.


//create the path for the html site or just find it
let page_files_path = file_directory.join(&path.path).to_str().ok_or_else(||
"Couldn't create Path to html file!")?.replace(".md","");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message is a bit weird. It would be also better to break the lines here for better reading.

file_directory.join(&path.path)
             .to_str()
             .ok_or_else(|| "Couldn't create Path to html file!")?
             .replace(".md","");

"Couldn't create Path to html file!")?.replace(".md","");

if !PathBuf::from(page_files_path.as_str()).exists(){
info!("Creating directory for {}'s files: {}.", path.path.to_str().ok_or_else(|| "Path not found!")?,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good when specifying which path can't be found. For this we should rework the error messages for the whole project, there are much things in like this ^^

let files = file_path.read_dir()?;

//now attach the files to the html sites folder
for entry in files {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can get rid of the extra var but write
for entry in file_path.read_dir()? { ...

html_file.read_to_string(&mut buffer);
buffer += &link.as_str();
info!("Creating link to {:?} for {:?}", current_entry.file_name(), path.path.to_str()
.ok_or_else(|| "Entry is corrupted!")?.replace(".md", ".html"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

info!("Creating link to {:?} for {:?}",
      current_entry.file_name(),
      path.path.to_str()
               .ok_or_else(|| "Entry is corrupted!")?
               .replace(".md", ".html"));


else {Ok(Response::with((status::Ok, f)))}
},
_ => panic!("Invalid Path."),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here also return an Error.

@Drogglbecher Drogglbecher merged commit b67e14b into rust-leipzig:master Jun 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants