(FEAT): Completed watchdog v2 updates.
This works the way expected, all thats left is the CLI and the command arg parser.
This commit is contained in:
parent
862c6e7c92
commit
408fd5fc2e
22
input.md
22
input.md
@ -1,22 +0,0 @@
|
|||||||
hello `world`
|
|
||||||
|
|
||||||
|
|
||||||
This `is also a code block`
|
|
||||||
|
|
||||||
hi `mom
|
|
||||||
hello`
|
|
||||||
|
|
||||||
hi `mom
|
|
||||||
|
|
||||||
this is too far`
|
|
||||||
|
|
||||||
|
|
||||||
*this is **words***
|
|
||||||
|
|
||||||
## **Hello world**
|
|
||||||
|
|
||||||
### hello *world*
|
|
||||||
|
|
||||||
# ***This is both!***
|
|
||||||
|
|
||||||
###### This is neither
|
|
||||||
@ -10,11 +10,11 @@ CLI::CLI(int argc, char **argv) {
|
|||||||
"Error: No input file provided.\nUsage: <program> <input_file>");
|
"Error: No input file provided.\nUsage: <program> <input_file>");
|
||||||
}
|
}
|
||||||
// sets program info
|
// sets program info
|
||||||
programName = argv[0];
|
program_name = argv[0];
|
||||||
inputFile = argv[1];
|
input_file = argv[1];
|
||||||
|
|
||||||
// checks that file has correct file extension
|
// checks that file has correct file extension
|
||||||
if (!(CLI::IsMarkupFile(inputFile))) {
|
if (!(CLI::IsMarkupFile(input_file))) {
|
||||||
throw std::invalid_argument(
|
throw std::invalid_argument(
|
||||||
"Error: Invalid file extension. Expected a '.md' (Markdown) file.");
|
"Error: Invalid file extension. Expected a '.md' (Markdown) file.");
|
||||||
}
|
}
|
||||||
@ -54,15 +54,16 @@ bool CLI::IsMarkupFile(const std::string &filename) {
|
|||||||
// place to run commands but IDK IF WE SHOULD USE A HASMAP or how we are going
|
// place to run commands but IDK IF WE SHOULD USE A HASMAP or how we are going
|
||||||
// to get each method to run
|
// to get each method to run
|
||||||
void CLI::RunCommands() {
|
void CLI::RunCommands() {
|
||||||
for (int i = 0; i < args.size(); i++) {
|
for (size_t i = 0; i < args.size(); i++) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CLI::PrintHelp() const {
|
void CLI::PrintHelp() const {
|
||||||
std::cout << "Usage:\n"
|
std::cout << "Usage:\n"
|
||||||
<< " " << programName << " <input_file> REQUIRED\n"
|
<< "\t" << program_name << " <input_file>\n"
|
||||||
<< "Flags:\n"
|
<< "Flags:\n"
|
||||||
<< " --o, --output <outputFile>, optional output filename\n"
|
<< "\t-o, --output <output_file>, optional output filename\n"
|
||||||
<< " --w, --watch, enables watchdog\n"
|
<< "\t-w, --watch, enables watchdog\n";
|
||||||
<< " --s, --stop, stops watchdog\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ./parser input_file -w dhuahdakhk -o output
|
||||||
|
|||||||
@ -50,17 +50,16 @@ public:
|
|||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
std::string GetInputFile() const {return inputFile;}
|
std::string GetInputFile() const { return input_file; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns output file
|
* @brief Returns output file
|
||||||
*
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
std::string GetOutputFile() const {return outputFile;}
|
std::string GetOutputFile() const { return output_file; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Ensures the provided filename has a .markup extension
|
* @brief Ensures the provided filename has a .markup extension
|
||||||
*
|
*
|
||||||
@ -71,10 +70,10 @@ private:
|
|||||||
*/
|
*/
|
||||||
static bool IsMarkupFile(const std::string &filename);
|
static bool IsMarkupFile(const std::string &filename);
|
||||||
|
|
||||||
std::string programName;
|
std::string program_name;
|
||||||
std::vector<std::string> args;
|
std::vector<std::string> args;
|
||||||
std::string inputFile;
|
std::string input_file;
|
||||||
std::string outputFile;
|
std::string output_file;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
33
lib/documentConverter.h
Normal file
33
lib/documentConverter.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#ifndef DOCUMENT_CONVERTER_H
|
||||||
|
#define DOCUMENT_CONVERTER_H
|
||||||
|
|
||||||
|
#include "parser.h"
|
||||||
|
#include "watchdog.h"
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
class DocumentConverter {
|
||||||
|
private:
|
||||||
|
Parser parser;
|
||||||
|
Watchdog watchdog;
|
||||||
|
|
||||||
|
public:
|
||||||
|
DocumentConverter(std::string input, std::string output = "")
|
||||||
|
: parser(input, output), watchdog(input) {};
|
||||||
|
|
||||||
|
void Convert() {
|
||||||
|
this->parser.ParseDocument();
|
||||||
|
this->parser.WriteOutput();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConvertWatcher() {
|
||||||
|
// This is a lambda, which can be passed into the start function of
|
||||||
|
// watchdog.
|
||||||
|
std::function<void()> callback = [this]() {
|
||||||
|
this->parser.ParseDocument();
|
||||||
|
this->parser.WriteOutput();
|
||||||
|
};
|
||||||
|
this->watchdog.Start(callback);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -2,6 +2,7 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|||||||
@ -11,7 +11,8 @@ using std::string;
|
|||||||
using std::vector;
|
using std::vector;
|
||||||
|
|
||||||
void Parser::Inspect() {
|
void Parser::Inspect() {
|
||||||
std::cerr << "Parser::Inspect() is not yet implemented." << std::endl;
|
std::cout << this->position << std::endl;
|
||||||
|
std::cout << this->content.size() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Parser::NormalizeInputStream() {
|
void Parser::NormalizeInputStream() {
|
||||||
@ -40,6 +41,9 @@ void Parser::WriteOutput() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Parser::ParseDocument() {
|
void Parser::ParseDocument() {
|
||||||
|
// NOTE:This needs to be set so the parsing can continue
|
||||||
|
this->position = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this->content = this->filesystem.ReadInputFile();
|
this->content = this->filesystem.ReadInputFile();
|
||||||
} catch (const std::runtime_error &e) {
|
} catch (const std::runtime_error &e) {
|
||||||
|
|||||||
@ -6,19 +6,19 @@
|
|||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
|
|
||||||
void Watchdog::Start() {
|
void Watchdog::Start(std::function<void()> callback) {
|
||||||
// checks if file exist
|
// checks if file exist
|
||||||
if (!fs::exists(this->path))
|
if (!fs::exists(this->path))
|
||||||
throw std::runtime_error("File does not exist.");
|
throw std::runtime_error("File does not exist.");
|
||||||
|
|
||||||
// Loop forever and check the file.
|
// Loop forever and check the file.
|
||||||
while (true) {
|
while (true) {
|
||||||
CheckFile();
|
CheckFile(callback);
|
||||||
std::this_thread::sleep_for(this->POLLING_INTERVAL);
|
std::this_thread::sleep_for(this->POLLING_INTERVAL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Watchdog::CheckFile() {
|
void Watchdog::CheckFile(std::function<void()> callback) {
|
||||||
// LONG STORY SHORT:
|
// LONG STORY SHORT:
|
||||||
// When a file is written to, it aquires an OS lock, which means our program
|
// When a file is written to, it aquires an OS lock, which means our program
|
||||||
// cannot access it. So when we request, it fails. So we should basically just
|
// cannot access it. So when we request, it fails. So we should basically just
|
||||||
@ -41,16 +41,20 @@ void Watchdog::CheckFile() {
|
|||||||
this->last_write_time = currentWriteTime;
|
this->last_write_time = currentWriteTime;
|
||||||
|
|
||||||
time_point before = high_resolution_clock::now();
|
time_point before = high_resolution_clock::now();
|
||||||
|
callback();
|
||||||
// DO SOMETHING
|
|
||||||
std::this_thread::sleep_for(this->POLLING_INTERVAL);
|
|
||||||
|
|
||||||
time_point after = high_resolution_clock::now();
|
time_point after = high_resolution_clock::now();
|
||||||
|
|
||||||
duration dur = after - before;
|
duration dur = after - before;
|
||||||
long ms = std::chrono::duration_cast<milliseconds>(dur).count();
|
long ms = std::chrono::duration_cast<milliseconds>(dur).count();
|
||||||
|
long us = std::chrono::duration_cast<microseconds>(dur).count();
|
||||||
|
|
||||||
|
if (ms > 0) {
|
||||||
std::cout << std::endl
|
std::cout << std::endl
|
||||||
<< "Recompiled in \033[36m" << ms << "ms\033[0m" << std::endl;
|
<< "Recompiled in \033[36m" << ms << "ms\033[0m" << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << std::endl
|
||||||
|
<< "Recompiled in \033[36m" << us << "μs\033[0m" << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (const std::exception &ex) {
|
} catch (const std::exception &ex) {
|
||||||
// On last attempt, bubble the error outward
|
// On last attempt, bubble the error outward
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
#include <chrono> //makes timestamps easier
|
#include <chrono> //makes timestamps easier
|
||||||
#include <ctime> //Convert time_t to std::tm for local time
|
#include <ctime> //Convert time_t to std::tm for local time
|
||||||
#include <filesystem> //allow access to files platform independent
|
#include <filesystem> //allow access to files platform independent
|
||||||
|
#include <functional>
|
||||||
#include <iomanip> //Formats std::tim into "YYYY-MM-DD HH-MM-SS"
|
#include <iomanip> //Formats std::tim into "YYYY-MM-DD HH-MM-SS"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -33,10 +34,13 @@ protected:
|
|||||||
*
|
*
|
||||||
* Checks if a file has been modified
|
* Checks if a file has been modified
|
||||||
*
|
*
|
||||||
|
* @param callback Callback function to execute when the watchdog notices a
|
||||||
|
* change.
|
||||||
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
* @author Hayden Hargreaves (hhargreaves2006@gmail.com)
|
* @author Hayden Hargreaves (hhargreaves2006@gmail.com)
|
||||||
*/
|
*/
|
||||||
void CheckFile();
|
void CheckFile(std::function<void()> callback);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Watchdog(const std::string &path) : path(path) {}
|
Watchdog(const std::string &path) : path(path) {}
|
||||||
@ -46,9 +50,12 @@ public:
|
|||||||
*
|
*
|
||||||
* Starts the watchdog to check of a file is modified
|
* Starts the watchdog to check of a file is modified
|
||||||
*
|
*
|
||||||
|
* @param callback Callback function to execute when the watchdog notices a
|
||||||
|
* change.
|
||||||
|
*
|
||||||
* @author Preston Shultz (shultzp1@my.erau.edu)
|
* @author Preston Shultz (shultzp1@my.erau.edu)
|
||||||
*/
|
*/
|
||||||
void Start();
|
void Start(std::function<void()> callback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief watchdog class.
|
* @brief watchdog class.
|
||||||
|
|||||||
10
src/main.cpp
10
src/main.cpp
@ -1,3 +1,4 @@
|
|||||||
|
#include "../lib/documentConverter.h"
|
||||||
#include "../lib/inlineNode.h"
|
#include "../lib/inlineNode.h"
|
||||||
#include "../lib/parser.h"
|
#include "../lib/parser.h"
|
||||||
#include "../lib/structureNode.h"
|
#include "../lib/structureNode.h"
|
||||||
@ -40,7 +41,7 @@ void test_nodes() {
|
|||||||
*/
|
*/
|
||||||
void test_watchdog() {
|
void test_watchdog() {
|
||||||
Watchdog wd("test/input.md");
|
Watchdog wd("test/input.md");
|
||||||
wd.Start();
|
wd.Start(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_input(int argc, char **argv) {
|
void test_input(int argc, char **argv) {
|
||||||
@ -66,4 +67,9 @@ void test_input(int argc, char **argv) {
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) { test_watchdog(); }
|
void test_document_converter() {
|
||||||
|
DocumentConverter dc("test/input.md");
|
||||||
|
dc.Convert();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) { test_document_converter(); }
|
||||||
|
|||||||
45
syntax.md
45
syntax.md
@ -1,45 +0,0 @@
|
|||||||
|
|
||||||
Reference [here](https://www.markdownguide.org/basic-syntax/)
|
|
||||||
|
|
||||||
Headings, h# tags
|
|
||||||
|
|
||||||
|
|
||||||
# Header Level 1 -> <h1> Content </h1>
|
|
||||||
## Header Level 2 -> <h2> Content </h2>
|
|
||||||
### Header Level 3 -> <h3> Content </h3>
|
|
||||||
#### Header Level 4 -> <h4> Content </h4>
|
|
||||||
##### Header Level 5 -> <h5> Content </h5>
|
|
||||||
###### Header Level 6 -> <h6> Content </h6>
|
|
||||||
|
|
||||||
|
|
||||||
Alternate syntax (n number of =/-)
|
|
||||||
|
|
||||||
Header Level 1 -> <h1> Content </h1>
|
|
||||||
================
|
|
||||||
|
|
||||||
|
|
||||||
Header Level 2 -> <h2> Content </h2>
|
|
||||||
----------------
|
|
||||||
|
|
||||||
|
|
||||||
Paragraph tags
|
|
||||||
|
|
||||||
Hello world -> <p> Hello world </p>
|
|
||||||
|
|
||||||
This is also
|
|
||||||
a paragraph -> <p> this is also a paragraph regardless </p>
|
|
||||||
regardless
|
|
||||||
|
|
||||||
However
|
|
||||||
this is a break, because it ends with two spaces -> <p> However <br> this is a break, because it ends with two spaces </p>
|
|
||||||
|
|
||||||
Double returns also
|
|
||||||
|
|
||||||
yields new paragraphs -> <p> Double returns also</p> <p> yields new paragraphs </p>
|
|
||||||
|
|
||||||
|
|
||||||
*italic* -> <em>italic</em>
|
|
||||||
**bold** -> <strong>bold</strong>
|
|
||||||
***italic bold*** -> <strong><em>italic bold</em></strong>
|
|
||||||
|
|
||||||
hello **world** -> [TextClass: hello, BoldClass: world]
|
|
||||||
25
test/input.html
Normal file
25
test/input.html
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Document</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>Reference [here](https://www.markdownguide.org/basic-syntax/)</p>
|
||||||
|
<p>Headings, h# tags</p>
|
||||||
|
<h1>Header Level 1 -> <h1> Content </h1> ## Header Level 2 -> <h2> Content </h2> ### Header Level 3 -> <h3> Content </h3> #### Header Level 4 -> <h4> Content </h4> ##### Header Level 5 -> <h5> Content </h5> ###### Header Level 6 -> <h6> Content </h6></h1>
|
||||||
|
<p>Alternate syntax (n number of =/-)</p>
|
||||||
|
<p>Header Level 1 -> <h1> Content </h1> ================</p>
|
||||||
|
<p>Header Level 2 -> <h2> Content </h2> ----------------</p>
|
||||||
|
<p>Paragraph tags</p>
|
||||||
|
<p>Hello world -> <p> Hello world </p></p>
|
||||||
|
<p>This is also a paragraph -> <p> this is also a paragraph regardless </p> regardless </p>
|
||||||
|
<p>However this is a break, because it ends with two spaces -> <p> However <br> this is a break, because it ends with two spaces </p></p>
|
||||||
|
<p>Double returns also</p>
|
||||||
|
<p>yields new paragraphs -> <p> Double returns also</p> <p> yields new paragraphs </p></p>
|
||||||
|
<p><em>italic</em> -> <em>italic</em> <strong>bold</strong> -> <strong>bold</strong> <strong><em>italic bold</em></strong> -> <strong><em>italic bold</em></strong></p>
|
||||||
|
<p>hello <strong>world</strong> -> [TextClass: hello, BoldClass: world] </p>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -1,17 +1,44 @@
|
|||||||
|
Reference [here](https://www.markdownguide.org/basic-syntax/)
|
||||||
|
|
||||||
|
Headings, h# tags
|
||||||
|
|
||||||
|
|
||||||
# Hello world in an h1 tag
|
# Header Level 1 -> <h1> Content </h1>
|
||||||
|
## Header Level 2 -> <h2> Content </h2>
|
||||||
|
### Header Level 3 -> <h3> Content </h3>
|
||||||
|
#### Header Level 4 -> <h4> Content </h4>
|
||||||
|
##### Header Level 5 -> <h5> Content </h5>
|
||||||
|
###### Header Level 6 -> <h6> Content </h6>
|
||||||
|
|
||||||
|
|
||||||
## This is a h2 tag
|
Alternate syntax (n number of =/-)
|
||||||
|
|
||||||
|
Header Level 1 -> <h1> Content </h1>
|
||||||
|
================
|
||||||
|
|
||||||
|
|
||||||
### h3
|
Header Level 2 -> <h2> Content </h2>
|
||||||
|
----------------
|
||||||
|
|
||||||
|
|
||||||
#### h4
|
Paragraph tags
|
||||||
|
|
||||||
##### h5
|
Hello world -> <p> Hello world </p>
|
||||||
|
|
||||||
###### h6
|
This is also
|
||||||
|
a paragraph -> <p> this is also a paragraph regardless </p>
|
||||||
|
regardless
|
||||||
|
|
||||||
|
However
|
||||||
|
this is a break, because it ends with two spaces -> <p> However <br> this is a break, because it ends with two spaces </p>
|
||||||
|
|
||||||
|
Double returns also
|
||||||
|
|
||||||
|
yields new paragraphs -> <p> Double returns also</p> <p> yields new paragraphs </p>
|
||||||
|
|
||||||
|
|
||||||
|
*italic* -> <em>italic</em>
|
||||||
|
**bold** -> <strong>bold</strong>
|
||||||
|
***italic bold*** -> <strong><em>italic bold</em></strong>
|
||||||
|
|
||||||
|
hello **world** -> [TextClass: hello, BoldClass: world]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user