Skip to content

Commit

Permalink
Improved variable naming a lot!
Browse files Browse the repository at this point in the history
Reduced code duplication through very cool dedicated validation functions

Changed folder validation to use a poggers filesystem status

delete testing files for the win
  • Loading branch information
gusmccallum committed Oct 14, 2021
1 parent 806de87 commit 1210d7d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 42 deletions.
4 changes: 4 additions & 0 deletions gasHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

static void processText(std::string fileName, int fileType, std::string = "", std::string = "", std::string = "./dist");
static void newFolder(std::string = "./dist");
bool isText(std::string inLine);
bool isMarkDown(std::string inLine);
bool isFolder(std::string inLine);
bool isJson(std::string inLine);
std::string makeHeader1(std::string line);
std::string hzRule(std::string line);
std::string getJsonValue(std::string line);
Expand Down
119 changes: 77 additions & 42 deletions gasMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int main(int argc, char** argv) {
}
if (cliString == "-c" || cliString == "--config"){
std::string input = argv[2];
if (input.find(".json") < input.size()){
if (isJson(input)){
processJsonFormat(input);
}else{
std::cout << "Invalid entry." << std::endl;
Expand All @@ -36,7 +36,7 @@ int main(int argc, char** argv) {
std::cout << "********************************\n" <<
"********************************\n" <<
"**** ****\n" <<
"**** Gus' Awesome Ssg V0.1 ****\n" <<
"**** Gus' Awesome Ssg V0.4 ****\n" <<
"**** ****\n" <<
"********************************\n"
"********************************" << std::endl;
Expand Down Expand Up @@ -65,7 +65,7 @@ int main(int argc, char** argv) {
}

//text file input
if (fileNameString.find(".txt") != std::string::npos) {
if (isText(fileNameString)) {
std::ifstream inFile(fileNameString);
if (!inFile)
{
Expand All @@ -79,7 +79,7 @@ int main(int argc, char** argv) {

}
//md file input
else if (fileNameString.find(".md") != std::string::npos) {
else if (isMarkDown(fileNameString)) {
std::ifstream inFile(fileNameString);
if (!inFile)
{
Expand All @@ -91,15 +91,15 @@ int main(int argc, char** argv) {
processText(fileNameString, 2);
}
}
else {
else if (isFolder(fileNameString)){
//folder input
newFolder();
for (const auto& dirItem : std::filesystem::recursive_directory_iterator(fileNameString)) {
std::string path = dirItem.path().string();
if (path.find(".txt") != std::string::npos) {
if (isText(path)) {
processText(path, 1);
}
else if (path.find(".md") != std::string::npos) {
else if (isMarkDown(path)) {
processText(path, 2);
}
}
Expand All @@ -109,23 +109,23 @@ int main(int argc, char** argv) {
return 0;
}

static void processText(std::string fileName, int fileType, std::string stylesheet, std::string lang, std::string folderOutput) {
static void processText(std::string inFileName, int inFileType, std::string stylesheet, std::string lang, std::string folderOutput) {
std::string outFileName = "";
std::string title = "";
//open input file
std::ifstream inFile(fileName);
std::ifstream inFile(inFileName);
if (!inFile) {
inFile.close();
}
//folder input
if (fileName.find("\\") != std::string::npos) {
outFileName = fileName;
if (inFileName.find("\\") != std::string::npos) {
outFileName = inFileName;
outFileName = outFileName.erase(0, outFileName.find_last_of("\\") + 1);
outFileName = outFileName.substr(0, outFileName.find("."));
}
//txt or md input
else {
outFileName = fileName;
outFileName = inFileName;
outFileName = outFileName.substr(0, outFileName.find("."));
}

Expand All @@ -141,7 +141,7 @@ static void processText(std::string fileName, int fileType, std::string styleshe
if(stylesheet != "")
outFile << "<link rel='stylesheet' href='" << stylesheet <<"'>";
//.txt file title/header
if (fileType == 1) {
if (inFileType == 1) {
//get Title
std::string line1 = "";
std::string line2 = "";
Expand All @@ -159,35 +159,35 @@ static void processText(std::string fileName, int fileType, std::string styleshe
outFile << "</head>" << '\n' << "<body>" << '\n' << "<h1> " << line1 << "</h1>" << '\n';
}
//.md file title
else if (fileType == 2) {
else if (inFileType == 2) {
outFile << " <title> " << title << "</title>" << '\n';
outFile << "</head>" << '\n' << "<body>" << '\n';
}

std::string inLine = "";
std::string firstLine = "";
std::string lastLine = "";
while (std::getline(inFile, inLine)) {
if (fileType == 2) {
while (std::getline(inFile, firstLine)) {
if (inFileType == 2) {
//Check for Header 1 syntax
if ((inLine.substr(0,2).find("# ") != std::string::npos)) {
inLine = makeHeader1(inLine);
if ((firstLine.substr(0,2).find("# ") != std::string::npos)) {
firstLine = makeHeader1(firstLine);
}
//Check for Horizontal rule syntax
if (inLine.find("---") != std::string::npos) {
inLine = hzRule(inLine);
if (firstLine.find("---") != std::string::npos) {
firstLine = hzRule(firstLine);
}
}

if (inLine != "" && lastLine == "") {
outFile << "<p> " << '\n' << inLine << '\n';
if (firstLine != "" && lastLine == "") {
outFile << "<p> " << '\n' << firstLine << '\n';
}
else if (inLine == "" && lastLine != "") {
else if (firstLine == "" && lastLine != "") {
outFile << " </p>" << '\n';
}
else {
outFile << inLine << "\n";
outFile << firstLine << "\n";
}
lastLine = inLine;
lastLine = firstLine;
}
inFile.close();
outFile << " </body>" << '\n' << "</html>";
Expand All @@ -201,16 +201,51 @@ static void processText(std::string fileName, int fileType, std::string styleshe

}

std::string makeHeader1(std::string line) {
std::string newLine = line;
newLine.erase(0, 2);
newLine = "<h1>" + newLine + "</h1>";
return newLine;
std::string makeHeader1(std::string inLine) {
std::string headerLine = inLine;
headerLine.erase(0, 2);
headerLine = "<h1>" + headerLine + "</h1>";
return headerLine;
}

std::string hzRule(std::string inLine) {
inLine.replace(inLine.find("---"), 3, "<hr>");
return inLine;
}

std::string hzRule(std::string line) {
line.replace(line.find("---"), 3, "<hr>");
return line;
bool isText(std::string inLine) {
if (inLine.find(".txt") != std::string::npos) {
return true;
}
else {
return false;
}
}
bool isMarkDown(std::string inLine) {
if (inLine.find(".md") != std::string::npos) {
return true;
}
else {
return false;
}
}
bool isFolder(std::string inLine) {
const std::filesystem::path folderPath = inLine;
if (std::filesystem::is_directory(symlink_status(folderPath))) {
return true;
}
else {
return false;
}
}

bool isJson(std::string inLine) {
if (inLine.find(".json") != std::string::npos) {
return true;
}
else {
return false;
}
}

static void newFolder(std::string folder) {
Expand All @@ -221,15 +256,15 @@ static void newFolder(std::string folder) {
}
}

std::string getJsonValue(std::string line){
line = line.erase(0, line.find(":"));
line = line.erase(0, line.find("\"")+1);
std::string value = line.substr(0, line.find("\""));
std::string getJsonValue(std::string inLine){
inLine = inLine.erase(0, inLine.find(":"));
inLine = inLine.erase(0, inLine.find("\"")+1);
std::string value = inLine.substr(0, inLine.find("\""));
return value;
}

void processJsonFormat(std::string file){
std::ifstream jsonFile(file);
void processJsonFormat(std::string inFile){
std::ifstream jsonFile(inFile);
if (jsonFile){
std::vector<std::string> jsonLines;
std::string temp;
Expand Down Expand Up @@ -280,10 +315,10 @@ void processJsonFormat(std::string file){
newFolder(output);
for (const auto& dirItem : std::filesystem::recursive_directory_iterator(fileNameString)) {
std::string path = dirItem.path().string();
if (path.find(".txt") != std::string::npos) {
if (isText(path)) {
processText(path, 1, stylesheet, lang, output);
}
else if (path.find(".md") != std::string::npos) {
else if (isMarkDown(path)) {
processText(path, 2, stylesheet, lang, output);
}
}
Expand Down

0 comments on commit 1210d7d

Please sign in to comment.