Skip to content

Commit 8e922f6

Browse files
committed
cleaned up stdout significantly, removed unnecessary delay
1 parent 0c19271 commit 8e922f6

File tree

7 files changed

+92
-62
lines changed

7 files changed

+92
-62
lines changed

config.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# Hi! this is a minimal config file for pond
2-
3-
author_name = 'Sahil Upasane'
4-
blog_name = '404salad'
5-
1+
author_name = """
2+
sahil
3+
"""
4+
blog_name = """
5+
404salad
6+
"""

dist/index.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<title>404salad</title>
5+
<title>404salad
6+
</title>
67
<link rel="stylesheet" href="style.css">
78
<script>
89
function filterArticles() {
@@ -28,8 +29,10 @@
2829
</head>
2930
<body class="container">
3031
<br>
31-
<h1>404salad</h1>
32-
<h6>Sahil Upasane</h6>
32+
<h1>404salad
33+
</h1>
34+
<h6>sahil
35+
</h6>
3336
<input type="search" id="search" name="search" placeholder="Type to search..." onkeyup="filterArticles()">
3437
<br>
3538
<br>

src/config.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use serde::Deserialize;
1+
use serde::{Deserialize, Serialize};
22
use std::fmt;
3-
use std::fs::read_to_string;
3+
use std::fs::{read_to_string, File};
44
use std::io::{self, Write};
5+
use std::path::Path;
56

6-
#[derive(Deserialize, Clone)]
7+
#[derive(Deserialize, Clone, Serialize)]
78
pub struct UserConfig {
89
pub author_name: String,
910
pub blog_name: String,
@@ -20,28 +21,49 @@ impl fmt::Display for UserConfig {
2021
}
2122
}
2223

23-
/*
24-
pub fn initial_config () {
25-
print!("enter your name > " );
24+
pub fn initial_config() {
25+
// only run config setup if the config file doesnt exists
26+
if Path::new("config.toml").exists() {
27+
return;
28+
}
29+
30+
// taking user input
31+
print!("enter your name > ");
2632
io::stdout().flush().expect("flush failed");
2733
let mut author_name = String::new();
2834
io::stdin()
2935
.read_line(&mut author_name)
3036
.expect("readline failed");
3137

32-
print!("enter blog name > " );
38+
print!("enter blog name > ");
3339
io::stdout().flush().expect("flush failed");
3440
let mut blog_name = String::new();
3541
io::stdin()
3642
.read_line(&mut blog_name)
3743
.expect("readline failed");
3844

39-
let user = UserConfig{
45+
let user = UserConfig {
4046
author_name,
4147
blog_name,
4248
};
49+
let toml_config: String = match toml::to_string(&user) {
50+
Ok(config_str) => config_str,
51+
Err(why) => panic!("invalid config file due to {why}"),
52+
};
53+
54+
// writing to config file
55+
let config_path = Path::new("config.toml");
56+
57+
let mut config_file = match File::create(&config_path) {
58+
Err(why) => panic!("cant create config file because {}", why),
59+
Ok(file) => file,
60+
};
61+
62+
match config_file.write_all(toml_config.as_bytes()) {
63+
Err(why) => panic!("cant write to config file because {}", why),
64+
Ok(_) => println!("succesfully created config file"),
65+
}
4366
}
44-
*/
4567

4668
pub fn read_config() -> Result<UserConfig, toml::de::Error> {
4769
let config_file_data = match read_to_string("config.toml") {

src/consolidate_into_homepage.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
use super::config;
12
use std::fs;
23
use std::fs::File;
3-
use std::io::{Write};
4-
use super::config;
4+
use std::io::Write;
55

6-
pub fn read_directory_content() -> Vec<String>{
7-
let mut article_names:Vec<String> = vec![];
6+
pub fn read_directory_content() -> Vec<String> {
7+
let mut article_names: Vec<String> = vec![];
88
let paths_result = fs::read_dir("content");
99
// iterating through paths
1010
match paths_result {
@@ -13,10 +13,12 @@ pub fn read_directory_content() -> Vec<String>{
1313
match path_result {
1414
Ok(path) => {
1515
if let Some(path_str) = path.path().to_str() {
16-
article_names.push(path_str
17-
.trim_start_matches("content/")
18-
.trim_end_matches(".md")
19-
.to_string());
16+
article_names.push(
17+
path_str
18+
.trim_start_matches("content/")
19+
.trim_end_matches(".md")
20+
.to_string(),
21+
);
2022
} else {
2123
eprintln!("Error converting path to string");
2224
}
@@ -80,21 +82,23 @@ pub fn create_homepage(user_config: &config::UserConfig) -> std::io::Result<()>
8082
user_config.blog_name, user_config.blog_name, user_config.author_name
8183
));
8284

83-
for article_name in article_names {
84-
document.push_str(&format!(
85-
"<article>
85+
for article_name in article_names {
86+
document.push_str(&format!(
87+
"<article>
8688
<a href=\"articles/{article_name}.html\"> {article_name} </a>
8789
</article>",
8890
article_name = article_name
89-
));
90-
}
91+
));
92+
}
9193

92-
document.push_str("
94+
document.push_str(
95+
"
9396
</section>
9497
</body>
95-
</html>");
98+
</html>",
99+
);
96100

97-
let mut file = File::create(output_path)?;
98-
write!(file, "{}", document)?;
99-
Ok(())
101+
let mut file = File::create(output_path)?;
102+
write!(file, "{}", document)?;
103+
Ok(())
100104
}

src/main.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ pub mod utils;
55

66
use rayon::prelude::*;
77
use std::fs;
8-
use std::thread;
9-
use std::time::Duration;
108

119
use console::Term;
1210

1311
fn main() {
1412
let term = Term::stdout();
15-
term.clear_screen();
16-
term.write_line(
13+
let _ = term.clear_screen();
14+
let _ = term.write_line(
1715
"
1816
██████ ██████ ███ ██ ██████ ███████ ███████ ██████
1917
██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██
@@ -22,38 +20,36 @@ fn main() {
2220
██ ██████ ██ ████ ██████ ███████ ███████ ██████
2321
",
2422
);
25-
thread::sleep(Duration::from_millis(200));
26-
term.write_line(" A simple cli tool to convert markdown to blog");
23+
let _ = term.write_line(" A simple cli tool to convert markdown to blog");
2724

28-
// TODO: only run this function if config toml doesnt exist
29-
//config::initial_config();
25+
config::initial_config();
3026

3127
let user_config = config::read_config().unwrap();
32-
println!("{}", user_config);
28+
//println!("{}", user_config);
3329

3430
// Remove previous content (clean)
3531
let article_dir = fs::read_dir("dist/articles");
3632
utils::delete_dir_contents(article_dir);
3733

3834
let article_names = consolidate_into_homepage::read_directory_content();
39-
println!("{:?}", article_names);
35+
// println!("{:?}", article_names);
4036

4137
// rebuilding all the articles in content directory (parallely)
4238

4339
article_names.par_iter().for_each(|article_name| {
4440
let user_config_for_threads = user_config.clone();
4541
match parse_one_article::markdown_to_styled_html(&article_name, &user_config_for_threads) {
46-
Ok(_) => {
47-
println!("succesful parse")
48-
}
42+
Ok(_) => {}
4943
Err(e) => {
5044
eprintln!("unsuccesful parse {}", e)
5145
}
5246
}
5347
});
48+
println!("generated all blogs");
49+
5450
match consolidate_into_homepage::create_homepage(&user_config) {
5551
Ok(_) => {
56-
println!("succesfully created homepage")
52+
println!("added all blogs to homepage, view in dist/index.html")
5753
}
5854
Err(e) => {
5955
eprintln!("unsuccesful in creating homepage {}", e)

src/parse_one_article.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use super::config;
2+
use pulldown_cmark::{html, Options, Parser};
13
use std::fs::File;
2-
use std::path::Path;
34
use std::io::prelude::*;
4-
use std::io::{Write, BufReader, Error};
5-
use pulldown_cmark::{Parser, Options, html};
6-
use super::config;
5+
use std::io::{BufReader, Error, Write};
6+
use std::path::Path;
77

88
// parse markdown to html
99
fn parse_markdown(input: &str) -> String {
@@ -23,9 +23,10 @@ fn read_markdown<P: AsRef<Path>>(path: P) -> Result<String, Error> {
2323
}
2424

2525
// wrapper for input so that standard html and styles can be injected after converting to html
26-
fn wrap_html(markdown_output: &str, article: &str, user_config: &config::UserConfig) -> String {
26+
fn wrap_html(markdown_output: &str, article: &str, _user_config: &config::UserConfig) -> String {
2727
let mut wrapped_html = String::from(markdown_output);
28-
wrapped_html=format!("
28+
wrapped_html = format!(
29+
"
2930
<!DOCTYPE html>
3031
<html lang=\"en\">
3132
<head>
@@ -38,25 +39,28 @@ fn wrap_html(markdown_output: &str, article: &str, user_config: &config::UserCon
3839
<a href='../index.html'>home</a>
3940
<h1> {article} </h1>
4041
<hr>
41-
") + &wrapped_html +
4242
"
43+
) + &wrapped_html
44+
+ "
4345
</body>
4446
</html>
4547
";
4648
wrapped_html
4749
}
4850

4951
/// convert a single .md file to html
50-
pub fn markdown_to_styled_html(article: &str, user_config: &config::UserConfig) -> std::io::Result<()>{
52+
pub fn markdown_to_styled_html(
53+
article: &str,
54+
user_config: &config::UserConfig,
55+
) -> std::io::Result<()> {
56+
println!("parsing - {article}");
5157
let mut input_path = String::from("content/") + &article.to_owned();
52-
let mut output_path =String::from("dist/articles/")+ &article.to_owned();
58+
let mut output_path = String::from("dist/articles/") + &article.to_owned();
5359
input_path.push_str(".md");
5460
output_path.push_str(".html");
55-
println!("{input_path} => {output_path}");
5661
let html_from_md = parse_markdown(&read_markdown(input_path)?);
5762
let mut file = File::create(output_path)?;
5863
let wrapped_html = wrap_html(&html_from_md, article, user_config);
59-
write!(file,"{wrapped_html}")?;
64+
write!(file, "{wrapped_html}")?;
6065
Ok(())
6166
}
62-

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub fn delete_dir_contents(read_dir_res: Result<ReadDir, Error>) {
1212
} else {
1313
fs::remove_file(path).expect("Failed to remove a file");
1414
}
15-
println!("removed {:?}", entry);
1615
};
1716
}
1817
};
18+
println!("successfully removed previous content");
1919
}

0 commit comments

Comments
 (0)