Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
884b2d1
[Added] mowl for logging
se-spiess Jan 26, 2017
5df7e2e
Merge branch 'master' of https://github.com/Drogglbecher/wiki
se-spiess Feb 2, 2017
a04851d
[Improved] error handling
se-spiess Feb 4, 2017
4e26ad1
[Improved] error handling
se-spiess Feb 4, 2017
f537493
[Improved] error handling
se-spiess Feb 4, 2017
f9f7682
[Added] mowl for logging
se-spiess Jan 26, 2017
161c2a2
[Improved] unittesting for test coverage
se-spiess Feb 13, 2017
fb83a78
Merge branch 'master' of https://github.com/Drogglbecher/wiki
se-spiess Feb 13, 2017
d6448d3
[Changed] processing as lib, moved test files to `tests` folder
Drogglbecher Feb 13, 2017
d7e54c7
[Changed] test examples to `tests` folder and renamed lib to `wikilib`
Drogglbecher Feb 13, 2017
b55a70a
Merge branch 'master' of https://github.com/rust-leipzig/wiki
se-spiess Feb 16, 2017
e055547
Merge remote-tracking branch 'upstream/master'
se-spiess Mar 6, 2017
9680954
[Added] HTML file creation in output directory
se-spiess Mar 6, 2017
48c1bb5
[Removed] test for process.rs
se-spiess Mar 6, 2017
96eddf4
[Fixed] Unittest for generated HTML files
se-spiess Mar 7, 2017
ecbd1b5
Merge branch 'master' into master
saschagrunert Mar 9, 2017
f832504
Update lib.rs
saschagrunert Mar 9, 2017
d787ac2
Update lib.rs
saschagrunert Mar 9, 2017
57fba32
Update main.rs
saschagrunert Mar 9, 2017
5b77c45
Merge remote-tracking branch 'upstream/master'
se-spiess Mar 9, 2017
fbc8fcc
Merge remote-tracking branch 'origin/master'
se-spiess Mar 9, 2017
f8f22c9
[Changed] preserve markdown substructure for HTML output
se-spiess Mar 9, 2017
8814946
[Improved] replaced to_str-unwraps with error codes
se-spiess Mar 11, 2017
aa3d1eb
Merge remote-tracking branch 'upstream/master'
se-spiess Mar 16, 2017
4be9d36
[Added] test for errors
se-spiess Mar 16, 2017
f3cac29
[Changed] error handling to error_chain crate
Drogglbecher Mar 19, 2017
cb802f6
[Changed] test files for error_chain integration
Drogglbecher Mar 19, 2017
f699da5
Merge remote-tracking branch 'upstream/master'
Drogglbecher Mar 19, 2017
42b9908
[Removed] error_chain macro
Drogglbecher Mar 19, 2017
bbb0b35
Merge remote-tracking branch 'upstream/master'
Drogglbecher Apr 24, 2017
4714f9c
[Improved] test coverage
Drogglbecher May 3, 2017
fa1eb11
[Removed] 'static for rust 1.17 behaviour
Drogglbecher May 3, 2017
a19fbc1
[Changed] optimized test/lib.rs test coverage
Drogglbecher May 3, 2017
7e2c53e
[Added] html snippets
Drogglbecher May 18, 2017
f9d04f7
Merge remote-tracking branch 'upstream/master'
Drogglbecher May 21, 2017
e89ef33
[Added] default index.html with substructure
Drogglbecher May 21, 2017
356b704
[Added] index.template.html
Drogglbecher May 21, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/html/index.template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Wiki</h1>

<h2>Structure</h2>
56 changes: 41 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ use std::str;
#[derive(Default)]
/// Global processing structure
pub struct Wiki {
/// A collection of paths for the processing
paths: Vec<PathBuf>,
/// A collection of input_paths for the processing
input_paths: Vec<PathBuf>,
/// The html output paths
output_paths: Vec<PathBuf>,
}

impl Wiki {
Expand All @@ -50,10 +52,10 @@ impl Wiki {
}

/// Reads all markdown files recursively from a given directory.
/// Clears the current available paths
/// Clears the current available input_paths
pub fn read_from_directory(&mut self, directory: &str) -> Result<()> {
/// Remove all paths
self.paths.clear();
/// Remove all input_paths
self.input_paths.clear();

/// Gather new content
let md_path = PathBuf::from(&directory).join("**").join("*.md");
Expand All @@ -63,31 +65,31 @@ impl Wiki {

/// Use the current working directory as a fallback
for entry in glob(md_path.to_str().unwrap_or("."))? {
self.paths.push(entry?);
self.input_paths.push(entry?);
}

Ok(())
}

/// Print absolute path of all added md files
pub fn list_current_paths(&self) {
pub fn list_current_input_paths(&self) {
info!("Found the following markdown files:");
for file in &self.paths {
for file in &self.input_paths {
println!(" - {:?}", file);
}
}

/// Read the content of all files and convert it to HTML
pub fn read_content_from_current_paths(&self, input_root_dir: &str,
pub fn read_content_from_current_paths(&mut self, input_root_dir: &str,
output_directory: &str) -> Result<()> {
// Check whether output_directory exists, if not -> create
if !Path::new(output_directory).exists() {
info!("Creating directory for HMTL output: '{}'.", output_directory);
fs::create_dir(output_directory)?;
}

// Iterate over all available paths
for file in &self.paths {
// Iterate over all available input_paths
for file in &self.input_paths {
info!("Parsing file: {}", file.display());

// Open the file and read its content
Expand Down Expand Up @@ -129,10 +131,11 @@ impl Wiki {
}

// Creating the ouput HTML file
let mut of = File::create(
PathBuf::from(&output_directory)
.join(output_path))?;
of.write(to_html(&buffer).as_bytes())?;
let output_file_path = PathBuf::from(&output_directory)
.join(output_path);
let mut output_file = File::create(output_file_path.to_owned())?;
output_file.write(to_html(&buffer).as_bytes())?;
self.output_paths.push(output_path.to_path_buf());
},
None => bail!("Can not stringfy file path"),
}
Expand All @@ -141,6 +144,29 @@ impl Wiki {
Ok(())
}

/// Creates an index.html with simple tree structure view when no index.md was seen
pub fn create_index_tree(&self, output_directory: &str) -> Result<()> {
let index_path = Path::new(output_directory).join("index.html");
if !index_path.exists() {
info!("Creating index.html at {}",
index_path.to_str().ok_or_else(|| "Unable to stringify index path.")?);
let mut index_file = File::create(index_path)?;
let mut index_str = String::from(include_str!("html/index.template.html"));
for output_path in &self.output_paths {
index_str.push_str(format!("<li><a href=\"{}\">{}</a></li>\n",
output_path.to_str()
.ok_or_else(|| "Unable to stringify output path.")?,
output_path.file_name()
.ok_or_else(|| "Unable to extract file name for path")?
.to_str().ok_or_else(|| "Unable to stringify output path.")?)
.as_str());
}
index_file.write_all(index_str.as_bytes())?;
}

Ok(())
}

/// Create an HTTP server serving the generated files
pub fn serve(&self, output_directory: &str) -> Result<()> {
// Create a default listening address
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ fn run() -> Result<()> {

wiki.init_logging(log_level)?;
wiki.read_from_directory(input_directory)?;
wiki.list_current_paths();
wiki.list_current_input_paths();
wiki.read_content_from_current_paths(input_directory, output_directory)?;
wiki.create_index_tree(output_directory)?;

if enable_httpd {
wiki.serve(output_directory)?;
Expand Down
4 changes: 3 additions & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ fn test_read_from_directory() {
assert!(Path::new(path).exists());
}
println!("The following paths were found:");
wiki.list_current_paths();
wiki.list_current_input_paths();
assert!(wiki.create_index_tree("html").is_ok());
assert!(Path::new("html").join("index.html").exists());
}

#[test]
Expand Down