Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP #12

Merged
merged 3 commits into from
Oct 8, 2021
Merged

WIP #12

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions HTMLFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ void HTMLFile::setHtmlBody(vector<string> file){
}

//this function creates HTML Page
void HTMLFile::writeHTML(){
string name = "dist/" + getURL();
void HTMLFile::writeHTML(string path){
string name = "./" + path + "/" + getURL();
ofstream file(name);
file << getHtmlFile() << endl;
file.close();
Expand Down
2 changes: 1 addition & 1 deletion HTMLFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ class HTMLFile
string getURL();
void setHtmlHead(string title);
void setHtmlBody(vector<string> file);
void writeHTML();
void writeHTML(string path);
};

11 changes: 5 additions & 6 deletions MainPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
#include "MainPage.h"

//this function sets up main page and linked websites
void MainPage::setMainPage(string name, vector<string> textFiles, string lang){
setFolderName(name);
void MainPage::setMainPage(string path, vector<string> textFiles, string lang){
setFolderName(path);
for (int i = 0; i < textFiles.size();i++){
HTMLFile newFile;
newFile.openFile(textFiles.at(i), lang);
newFile.setHtmlFile();
string fileName = textFiles.at(i).substr(textFiles.at(i).find(folderName) + 2, textFiles.at(i).size() - 2);
newFile.writeHTML();
newFile.writeHTML(path);
sites.push_back(newFile);
}
setHTMLMainPage();
Expand Down Expand Up @@ -45,9 +45,8 @@ void MainPage::setHTMLMainPage(){
}

//this function writes HTML main page
void MainPage:: writeHTML(){
string name = "dist/";
name += (getFolderName() +".html");
void MainPage:: writeHTML(string path){
string name = "./" + path + "/" + path + ".html";
ofstream file(name);
file << getMainPage() << endl;
file.close();
Expand Down
2 changes: 1 addition & 1 deletion MainPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MainPage{
string getMainPage();
void displayMainPage();
void setHTMLMainPage();
void writeHTML();
void writeHTML(string path);
void setFolderName(string name);
string getFolderName();
};
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"input": "./docs",
"output": "./web",
"future-feature": "should be ignored for now"
}
1 change: 1 addition & 0 deletions config1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions config2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"input": "./docs"
}
4 changes: 4 additions & 0 deletions config3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"input": "./docs",
"output": "./web"
}
138 changes: 104 additions & 34 deletions pgprogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,92 +14,162 @@ using namespace std;
using std::filesystem::directory_iterator;
namespace fs = std::filesystem;

void createOneHTML(string filename, string lang);
void createManyHTML(string folderName, string lang);
void createOneHTML(string filename, string outPath, string lang);
void createManyHTML(string folderName, string outPath, string lang);
string checkArguments(int argc, char** argv);
string checkLanguage(int argc, char** argv);
bool checkIfConfig(int argc, char** argv);
void makeDir(string outputDir);
string readConfig(string fileName);


int main(int argc, char** argv)
{
{

if(argc >= 2){
string language = checkLanguage(argc, argv);
string name = checkArguments(argc, argv);
if (name != "terminate"){
string temp = readConfig(name);
bool configFlag = checkIfConfig(argc, argv);

//if empty config
if (configFlag == true && temp.find("\"") == string::npos) {
name = "terminate";
}

if (name != "terminate") {
if (name == "")
name = argv[1];
size_t folderOrFile = name.find(".txt");
size_t mdFile = name.find(".md");

fs::remove_all("./dist");
fs::current_path("./");
fs::create_directory("dist");

if (folderOrFile > name.size()){
if (mdFile > name.size()){
createManyHTML(name, language);
}else{
createOneHTML(name, language);
}
}else{
createOneHTML(name, language);
string outFolder = "./dist";
//if output folder is specified in config file
if (temp.find("output") != string::npos) {
size_t start = temp.find("output") + 12;
size_t end = temp.find("\"", start - 1);
outFolder = temp.substr(start, (end - start));
}
makeDir(outFolder);
//get input from config file
if (temp.find("input") != string::npos)
{
size_t start = temp.find("input") + 9;
size_t end = temp.find("\"", start + 2);
name = temp.substr(start, (end - start));
}

if (configFlag || name.find("./") != string::npos) {
createManyHTML(name, outFolder, language);
}
else if (name.find(".txt") != string::npos) {
createOneHTML(name, outFolder, language);
}
else if (name.find(".md") != string::npos) {
createOneHTML(name, outFolder, language);
}
}

}else{
else if (configFlag == true) {
cout << "Empty config file. Please try again." << endl;
}
}
else {
cout << "Failed arguments provided";
}
}

//this function creates a single HTML page
void createOneHTML(string filename, string lang){
void createOneHTML(string filename,string outPath, string lang){
HTMLFile newFile;
newFile.openFile(filename, lang);
newFile.setHtmlFile();
newFile.writeHTML();
newFile.writeHTML(outPath);
}

//this function creates multiple HTML page
void createManyHTML(string folderName, string lang){
void createManyHTML(string folderName, string outPath, string lang){
vector<string> fileNames;
for (const auto & file : directory_iterator(folderName))
for (const auto& file : directory_iterator(folderName)) {
fileNames.push_back(file.path().string());
MainPage newPage;
newPage.setMainPage(folderName, fileNames, lang);
newPage.writeHTML();
MainPage newPage;
newPage.setMainPage(outPath, fileNames, lang);
newPage.writeHTML(outPath);
}

}

//this function checks for arguments input
string checkArguments(int argc, char** argv){
string fName = "";

for (int i = 1; i < argc; i++) {
if (string(argv[i]) == "--version" || string(argv[i]) == "-v") {
cout << "Potato Generator - Version 0.1";
fName = "terminate";
break;
}else if (string(argv[i]) == "--help" || string(argv[i]) == "-h"){
}
else if (string(argv[i]) == "--help" || string(argv[i]) == "-h") {
cout << "*Run the program with command: ./pgprogram inputName\n";
cout << "The inputName is a text file name or a folder name\n";
cout << "*To include a input file/folder, include --input or -i before the file/folder name in the arguments.\n";
cout << "*To specify an config file, use --config or -c before the config filename.\n";
cout << "*To see the version of the program, include --version or -v in the arguments.\n";
cout << "*To need help, include --help or -h in the arguments.\n";
fName = "terminate";
break;
}else if (string(argv[i]) == "--input" || string(argv[i]) == "-i"){
fName = argv[(i+1)];
}
else if (string(argv[i]) == "--input" || string(argv[i]) == "-i") {
fName = argv[i+1];
break;
}
else if (string(argv[i]) == "--config" || string(argv[i]) == "-c") {
fName = argv[i+1];
break;
}
}


return fName;
}

//this function checks for language specified
string checkLanguage(int argc, char** argv){
string language = "";
for (int i = 1; i < argc; i++) {
if (string(argv[i]) == "--lang" || string(argv[i]) == "-l"){
language = argv[(i+1)];
break;
}
}
if (string(argv[i]) == "--lang" || string(argv[i]) == "-l") {
language = argv[i+1];
}
}
return language;
}

bool checkIfConfig(int argc, char** argv) {
bool config = false;
for (int i = 1; i < argc; i++) {
if (string(argv[i]) == "--config" || string(argv[i]) == "-c") {
config = true;
}
}
return config;
}

void makeDir(string outputDir) {
fs::remove_all("./dist");
if (outputDir != "./dist") {
outputDir = "./" + outputDir;
fs::create_directory(outputDir);
}
else {
fs::create_directory("./dist");
}
}

string readConfig(string fileName) {
string storeConfig = "";
ifstream configFile;
configFile.open(fileName, ios::in);
if (configFile.is_open()) {
getline(configFile, storeConfig, '}');
configFile.close();
}
return storeConfig;
}