Skip to content

Commit

Permalink
ver bump
Browse files Browse the repository at this point in the history
  • Loading branch information
amkhrjee committed Aug 28, 2024
1 parent ef3a243 commit 5047a9a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wp"
version = "0.1.0"
version = "0.1.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Todo:
- output in a directory
- progress bar
- helpful errors
- fix {} escape
- fix {} escapeQ
66 changes: 2 additions & 64 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use clap::Parser;
use std::{
fs::File,
io::Write,
path::{Path, PathBuf},
};
use std::path::PathBuf;
use url::Url;

use utils::*;
Expand Down Expand Up @@ -43,18 +39,6 @@ struct Token {
format: FormatType,
}

impl Token {
fn print(&self, source: &Vec<char>) {
println!(
"{:?} => {}",
self.format,
source[self.start..self.start + self.length]
.iter()
.collect::<String>()
)
}
}

fn main() {
let args = Args::parse();
let link = args.link;
Expand Down Expand Up @@ -84,7 +68,7 @@ fn main() {
}
}

fn get_article(url: String) -> Result<(String), String> {
fn get_article(url: String) -> Result<String, String> {
let response: serde_json::Value = reqwest::blocking::get(url)
.map_err(|err| format!("Error: Could not fetch article due to {}", err))?
.json()
Expand Down Expand Up @@ -249,49 +233,3 @@ fn parse_text(characters: &Vec<char>) -> Result<Vec<Token>, String> {

Ok(tokens)
}

fn generate_plaintext(tokens: &Vec<Token>, characters: &Vec<char>) -> String {
let mut plaintext = String::new();
let get_text = |token: &Token| {
characters[token.start..token.start + token.length]
.iter()
.collect::<String>()
};
for token in tokens {
match token.format {
FormatType::Title
| FormatType::Bold
| FormatType::Italic
| FormatType::PlainWord
| FormatType::Subtitle
| FormatType::Subsubtitle
| FormatType::WikiLink
| FormatType::BulletBold
| FormatType::BulletItalic
| FormatType::InlineQuote => plaintext.push_str(&get_text(token)),

FormatType::Space => plaintext.push(' '),
FormatType::NewLine => plaintext.push('\n'),
}
}
plaintext.trim().to_string()
}

fn output_to_stdout(plaintext_string: &str) {
println!("{}", plaintext_string);
}

fn save_to_disk(plaintext_string: &str, article_title: &str) {
let path = Path::new(article_title);

let mut file = match File::create(&path) {
Err(why) => panic!("Error: Couldn't create {}: {}", path.display(), why),
Ok(file) => file,
};

// Write the `LOREM_IPSUM` string to `file`, returns `io::Result<()>`
match file.write_all(plaintext_string.as_bytes()) {
Err(why) => panic!("Error: Couldn't write to {}: {}", path.display(), why),
Ok(_) => println!("Saved to {}", path.display()),
}
}
47 changes: 47 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::{fs::File, io::Write, path::Path};

use crate::{FormatType, Token};

pub fn advance(text: &Vec<char>, current: &mut usize) -> char {
Expand Down Expand Up @@ -38,3 +40,48 @@ pub fn add_space(tokens: &mut Vec<Token>, current: usize) {
format: FormatType::Space,
})
}

pub fn generate_plaintext(tokens: &Vec<Token>, characters: &Vec<char>) -> String {
let mut plaintext = String::new();
let get_text = |token: &Token| {
characters[token.start..token.start + token.length]
.iter()
.collect::<String>()
};
for token in tokens {
match token.format {
FormatType::Title
| FormatType::Bold
| FormatType::Italic
| FormatType::PlainWord
| FormatType::Subtitle
| FormatType::Subsubtitle
| FormatType::WikiLink
| FormatType::BulletBold
| FormatType::BulletItalic
| FormatType::InlineQuote => plaintext.push_str(&get_text(token)),

FormatType::Space => plaintext.push(' '),
FormatType::NewLine => plaintext.push('\n'),
}
}
plaintext.trim().to_string()
}

pub fn output_to_stdout(plaintext_string: &str) {
println!("{}", plaintext_string);
}

pub fn save_to_disk(plaintext_string: &str, article_title: &str) {
let path = Path::new(article_title);

let mut file = match File::create(&path) {
Err(why) => panic!("Error: Couldn't create {}: {}", path.display(), why),
Ok(file) => file,
};

match file.write_all(plaintext_string.as_bytes()) {
Err(why) => panic!("Error: Couldn't write to {}: {}", path.display(), why),
Ok(_) => println!("Saved to {}", path.display()),
}
}

0 comments on commit 5047a9a

Please sign in to comment.