Skip to content

Commit

Permalink
Added --debug
Browse files Browse the repository at this point in the history
Added a --debug argument so I'll be able to see what's going behind the code and solve problems more easily.
Right now I've added the debug function to several places on the code, but it may vary.
  • Loading branch information
BenTimor committed Aug 7, 2020
1 parent c71080f commit 9262a7e
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 28 deletions.
6 changes: 4 additions & 2 deletions commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ pub trait Command {
command: &String,
parameters: &Vec<String>,
text: &String, spaces: &usize,
blocks: &Vec<Box<PreprocessedObject>>
blocks: &Vec<Box<PreprocessedObject>>,
config: &Config
) -> String;
}

Expand All @@ -39,7 +40,8 @@ pub trait PreprocessedCommand {
parameters: &Vec<String>,
text: &String, spaces: &usize,
blocks: &Vec<Box<PreprocessedObject>>,
preprocessed: Vec<Box<PreprocessedObject>>
preprocessed: Vec<Box<PreprocessedObject>>,
config: &Config
) -> Vec<Box<PreprocessedObject>>;
}

Expand Down
3 changes: 2 additions & 1 deletion commands/delfile.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::commands::Command;
use crate::preprocessing::PreprocessedObject;
use std::fs::remove_file;
use crate::config::Config;

/// Allows you to remove a file
/// @delfile PATH
pub struct DelFile {}

impl Command for DelFile {
fn run(&self, _command: &String, _parameters: &Vec<String>, text: &String, _spaces: &usize, _blocks: &Vec<Box<PreprocessedObject>>) -> String {
fn run(&self, _command: &String, _parameters: &Vec<String>, text: &String, _spaces: &usize, _blocks: &Vec<Box<PreprocessedObject>>, _config: &Config) -> String {
if remove_file(text).is_err() {
println!("Couldn't delete the file {} with the command @delfile", &text)
}
Expand Down
6 changes: 3 additions & 3 deletions commands/file.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use crate::commands::Command;
use crate::preprocessing::PreprocessedObject;
use crate::processing::full_process;
use crate::config::get_config;
use crate::config::{get_config, Config};
use crate::utils::get_file_content;

/// Allows you to read a file.
/// @file(process_content: bool DEFAULT=false) PATH
pub struct File {}

impl Command for File {
fn run(&self, _command: &String, parameters: &Vec<String>, text: &String, _spaces: &usize, _blocks: &Vec<Box<PreprocessedObject>>) -> String {
fn run(&self, _command: &String, parameters: &Vec<String>, text: &String, _spaces: &usize, _blocks: &Vec<Box<PreprocessedObject>>, config: &Config) -> String {
// Check of the first parameter is "true"
let process: bool = parameters.get(0).unwrap_or(&"false".to_string()).eq("true");
let content: String = get_file_content(text);

if process {
return full_process(&content, &get_config(false));
return full_process(&content, &get_config(false, config.debug));
}

content
Expand Down
6 changes: 3 additions & 3 deletions commands/intofile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ use crate::commands::Command;
use crate::preprocessing::PreprocessedObject;
use crate::utils::{get_blocks_as_content, export_content_into_file};
use crate::processing::full_process;
use crate::config::get_config;
use crate::config::{get_config, Config};

/// Allows you to write into files.
/// @intofile(process_content: bool DEFAULT=false, add_html: bool DEFAULT=false)
/// CONTENT
pub struct IntoFile {}

impl Command for IntoFile {
fn run(&self, _command: &String, parameters: &Vec<String>, text: &String, _spaces: &usize, blocks: &Vec<Box<PreprocessedObject>>) -> String {
fn run(&self, _command: &String, parameters: &Vec<String>, text: &String, _spaces: &usize, blocks: &Vec<Box<PreprocessedObject>>, config: &Config) -> String {
// Check of the first parameter is "true"
let process: bool = parameters.get(0).unwrap_or(&"false".to_string()).eq("true");
let html: bool = parameters.get(1).unwrap_or(&"false".to_string()).eq("true");
let content: String = get_blocks_as_content(&blocks);

if process {
export_content_into_file(&text, &full_process(&content, &get_config(html)));
export_content_into_file(&text, &full_process(&content, &get_config(html, config.debug)));
} else {
export_content_into_file(&text, &content);
}
Expand Down
6 changes: 3 additions & 3 deletions commands/loop_command.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::commands::Command;
use crate::preprocessing::PreprocessedObject;
use crate::processing::full_process;
use crate::config::get_config;
use crate::config::{get_config, Config};
use crate::utils::get_blocks_as_content;

/// Loop allows you to run a statement multiple times
/// @loop TIMES
pub struct LoopCommand {}

impl Command for LoopCommand {
fn run(&self, _command: &String, _parameters: &Vec<String>, text: &String, _spaces: &usize, blocks: &Vec<Box<PreprocessedObject>>) -> String {
fn run(&self, _command: &String, _parameters: &Vec<String>, text: &String, _spaces: &usize, blocks: &Vec<Box<PreprocessedObject>>, config: &Config) -> String {
// The 'text' of the command is the amount of times that the loop has to run
let times: usize = text.parse::<usize>()
.unwrap_or({
Expand All @@ -24,7 +24,7 @@ impl Command for LoopCommand {

// Running the content {times} amount of times
for _ in 0..times {
result.push(full_process(&content, &get_config(false)));
result.push(full_process(&content, &get_config(false, config.debug)));
}

result.join("\n")
Expand Down
3 changes: 2 additions & 1 deletion commands/note.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::commands::Command;
use crate::preprocessing::PreprocessedObject;
use std::ops::Deref;
use crate::config::Config;

/// Note command allows you to create HTML notes
/// @note Optional:NOTE
/// Optional:NOTE
pub struct Note {}

impl Command for Note {
fn run(&self, _command: &String, _parameters: &Vec<String>, text: &String, _spaces: &usize, blocks: &Vec<Box<PreprocessedObject>>) -> String {
fn run(&self, _command: &String, _parameters: &Vec<String>, text: &String, _spaces: &usize, blocks: &Vec<Box<PreprocessedObject>>, _config: &Config) -> String {
let mut note: Vec<String> = Vec::new();
note.push("<!--".to_string());
note.push(text.clone());
Expand Down
6 changes: 3 additions & 3 deletions commands/raw.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::commands::Command;
use crate::preprocessing::PreprocessedObject;
use crate::processing::full_process;
use crate::config::get_config;
use crate::config::{get_config, Config};
use crate::utils::get_blocks_as_content;

/// Allows you to use HTML "as is"
Expand All @@ -11,7 +11,7 @@ use crate::utils::get_blocks_as_content;
pub struct Raw {}

impl Command for Raw {
fn run(&self, _command: &String, parameters: &Vec<String>, text: &String, _spaces: &usize, blocks: &Vec<Box<PreprocessedObject>>) -> String {
fn run(&self, _command: &String, parameters: &Vec<String>, text: &String, _spaces: &usize, blocks: &Vec<Box<PreprocessedObject>>, config: &Config) -> String {
let use_tag = parameters.get(0).unwrap_or(&"true".to_string()).eq("true");
let mut raw: Vec<String> = Vec::new();

Expand All @@ -23,7 +23,7 @@ impl Command for Raw {

// If we use the tag, we have to the open of the tag in the start and the end of the tag in the end
if use_tag {
let processed_tag = full_process(&text, &get_config(false));
let processed_tag = full_process(&text, &get_config(false, config.debug));
let mut splitted_tag = processed_tag.lines();

// If the amount of the lines is smaller than two, skip
Expand Down
3 changes: 2 additions & 1 deletion commands/set.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::commands::PreprocessedCommand;
use crate::preprocessing::PreprocessedObject;
use std::ops::Deref;
use crate::config::Config;

/// Allows you to create "variables" by replacing whatever you choose in the file
/// ^set(from, to)
pub struct Set {}

impl PreprocessedCommand for Set {
fn run(&self, _command: &String, parameters: &Vec<String>, _text: &String, _spaces: &usize, _blocks: &Vec<Box<PreprocessedObject>>, preprocessed: Vec<Box<PreprocessedObject>>) -> Vec<Box<PreprocessedObject>> {
fn run(&self, _command: &String, parameters: &Vec<String>, _text: &String, _spaces: &usize, _blocks: &Vec<Box<PreprocessedObject>>, preprocessed: Vec<Box<PreprocessedObject>>, _config: &Config) -> Vec<Box<PreprocessedObject>> {
let optional_from = parameters.get(0);
if optional_from.is_none() {
println!("You have to enter two parameters for ^set command");
Expand Down
3 changes: 2 additions & 1 deletion commands/straw_note.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::commands::Command;
use crate::preprocessing::PreprocessedObject;
use crate::config::Config;

pub struct StrawNote {}

/// Does nothing. Useful for notes.
impl Command for StrawNote {
fn run(&self, _command: &String, _parameters: &Vec<String>, _text: &String, _spaces: &usize, _blocks: &Vec<Box<PreprocessedObject>>) -> String {
fn run(&self, _command: &String, _parameters: &Vec<String>, _text: &String, _spaces: &usize, _blocks: &Vec<Box<PreprocessedObject>>, _config: &Config) -> String {
"".to_string()
}
}
3 changes: 2 additions & 1 deletion commands/terminal.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::preprocessing::PreprocessedObject;
use std::process::Command as TerminalCommand;
use crate::commands::Command;
use crate::config::Config;

/// Allows you to run terminal commands
pub struct Terminal {}

impl Command for Terminal {
fn run(&self, _command: &String, _parameters: &Vec<String>, text: &String, _spaces: &usize, _blocks: &Vec<Box<PreprocessedObject>>) -> String {
fn run(&self, _command: &String, _parameters: &Vec<String>, text: &String, _spaces: &usize, _blocks: &Vec<Box<PreprocessedObject>>, _config: &Config) -> String {
// Literally copied pasted from several stackoverflow posts
let output_option = TerminalCommand::new("sh").arg("-c").arg(text).output().unwrap();
String::from_utf8_lossy(&output_option.stdout).parse().unwrap()
Expand Down
8 changes: 5 additions & 3 deletions config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ pub struct Config {
pub commands: HashMap<String, Box<dyn Command>>,
pub preprocessed_commands: HashMap<String, Box<dyn PreprocessedCommand>>,
pub short_commands: HashMap<String, String>,
pub add_html: bool
pub add_html: bool,
pub debug: bool
}

pub fn get_config(add_html: bool) -> Config {
pub fn get_config(add_html: bool, debug: bool) -> Config {
let mut config = Config {
commands: HashMap::new(),
preprocessed_commands: HashMap::new(),
short_commands: HashMap::new(),
add_html
add_html,
debug
};

config.short_commands.insert("//".to_string(), "@straw_note".to_string());
Expand Down
17 changes: 13 additions & 4 deletions main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod commands;
use crate::processing::full_process;
use crate::config::{Config, get_config};
use std::env;
use crate::utils::{get_file_content, export_content_into_file, create_directory_if_not_exist, get_argument_parameter};
use crate::utils::{get_file_content, export_content_into_file, create_directory_if_not_exist, get_argument_parameter, has_argument_parameter, debug};
use std::fs::{metadata, read_dir};

mod processing;
Expand All @@ -18,6 +18,12 @@ fn main() {

/* Here comes the main arguments */

// --debug option
let mut to_debug = false;
if has_argument_parameter(&"--debug".to_string(), &mut args) {
to_debug = true;
}

// --target Option
let mut directory= "./".to_string();
// If the argument of the target exist, change the target directory
Expand All @@ -32,7 +38,7 @@ fn main() {
// --XML Option
loop {
if let Option::Some(arg) = get_argument_parameter(&"--XML".to_string(), &mut args) {
compile(&arg, &directory, &get_config(false));
compile(&arg, &directory, &get_config(false, to_debug));
} else {
break;
}
Expand All @@ -42,7 +48,7 @@ fn main() {

// Call to the compile function for each argument
for arg in args {
compile(&arg, &directory, &get_config(true));
compile(&arg, &directory, &get_config(true, to_debug));
}
}

Expand All @@ -58,6 +64,7 @@ fn compile(path: &String, directory: &String, config: &Config) {
let unwrapped_md = md.unwrap();
// If it's a dir, take its content and compile recursively
if unwrapped_md.is_dir() {
debug("main.rs::compile entering into a directory.".to_string(), &config);
let full_directory = format!("{}{}", &directory, &path);
create_directory_if_not_exist(&full_directory);
let paths = read_dir(&path).unwrap();
Expand All @@ -73,9 +80,11 @@ fn compile(path: &String, directory: &String, config: &Config) {
fn compile_file(path: &String, directory: &String, config: &Config) {
// If it's not .sw file, skip
if !path.ends_with(".sw") {
debug(format!("main.rs::compile_file skipping the file {}.", &path), &config);
return;
}

println!("\nStarting to render {}", &path);
// Getting and processing the content of the file. Adding HTML and Doctype.
let content = full_process(
&get_file_content(&path.to_string()),
Expand All @@ -90,5 +99,5 @@ fn compile_file(path: &String, directory: &String, config: &Config) {
// Writing into the new file.
export_content_into_file(&format!("{}{}.html", &directory, &modified_path), &content);

println!("{} compiled into {}.html", &path, &modified_path)
println!("{} rendered into {}.html\n", &path, &modified_path)
}
11 changes: 10 additions & 1 deletion preprocessing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::config::Config;
use std::ops::Deref;
use crate::preprocessing::PreprocessedObject::Block;
use crate::utils::debug;

/// This method is the main preprocessing method.
/// It takes a content of file and converts it into PreprocessedObjects.
Expand All @@ -26,24 +27,28 @@ pub fn preprocess<'a>(content: &String, config: &Config) -> Vec<Box<Preprocessed
// If we're in a block, add a Block object. // Look at the 'current_spaces' variable note.
if block_spaces != 0 {
if current_spaces > block_spaces {
debug(format!("preprocessing.rs::preprocess saving a block | spaces={} | text={}", &current_spaces, &line), &config);
commands.push(Box::new(PreprocessedObject::Block {text: line, spaces: current_spaces}));
continue;
}
else {
debug(format!("preprocessing.rs::preprocess getting out of a block"), &config);
block_spaces = 0;
}
}

// Checking if it's a short command. If it is, then replace it with the long command.
for (short_command, long_command) in config.short_commands.clone() {
if line.starts_with(&short_command) {
debug(format!("preprocessing.rs::preprocess replacing short command {} with {}", &short_command, &long_command), &config);
line = line.replacen(&short_command[..], &format!("{} ", long_command)[..], 1);
break;
}
}

// If it's a command, start a block
if line.starts_with("@") {
debug(format!("preprocessing.rs::preprocess entering into the block of line={}", &line), &config);
block_spaces = current_spaces;
}

Expand All @@ -57,20 +62,23 @@ pub fn preprocess<'a>(content: &String, config: &Config) -> Vec<Box<Preprocessed
}

// We have to add one more command so it'll run through everything
debug("preprocessing.rs::preprocess adding an empty command so it'll add one more iterate".to_string(), &config);
commands.push(Box::new(PreprocessedObject::Command {
command: "@".to_string(),
parms: vec![],
text: "".to_string(),
spaces: 0
}));

debug("preprocessing.rs::preprocess starting the preprocessed commands".to_string(), &config);
let mut i = 0;
let mut blocks: Vec<Box<PreprocessedObject>> = Vec::new();
let mut optional_last_command: Option<Box<PreprocessedObject>> = Option::None{};
let mut temporary_commands: Vec<Box<PreprocessedObject>> = Vec::new();
// The loop is running until it doesn't find any element. It does it in case some command will change the commands vector size.
loop {
if !temporary_commands.is_empty() {
debug("preprocessing.rs::preprocess moving the temporary_commands into the commands".to_string(), &config);
commands = temporary_commands.clone();
temporary_commands = Vec::new();
}
Expand Down Expand Up @@ -101,8 +109,9 @@ pub fn preprocess<'a>(content: &String, config: &Config) -> Vec<Box<Preprocessed
continue;
}

debug(format!("preprocessing.rs::preprocess running the command {}", &command), &config);
temporary_commands = command_object.unwrap()
.run(&command, &parms, &text, &spaces, &mut blocks, commands.clone());
.run(&command, &parms, &text, &spaces, &mut blocks, commands.clone(), &config);
}
optional_last_command = Option::None;
}
Expand Down
Loading

0 comments on commit 9262a7e

Please sign in to comment.