Skip to content

Commit

Permalink
Merge pull request #10 from zijunhz/multiSettings
Browse files Browse the repository at this point in the history
feat: multi files conversion
  • Loading branch information
zpatronus authored Aug 23, 2022
2 parents 78d5744 + c7bb77e commit 2bb29e3
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.0.0)
project(markdown2html VERSION 1.2.0)
project(markdown2html VERSION 1.2.1)

add_compile_options(-Wall -Wextra -Wpedantic -std=c++1z)

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Code blocks (` ``` ``` `)

Inline code blocks (` `` `)

Convert multiple files

## How to compile

The project uses `CMake` as building tool.
Expand Down
20 changes: 11 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ int main() {
<< " This program comes with ABSOLUTELY NO WARRANTY." << endl
<< " This is free software, and you are welcome to redistribute it" << endl
<< " under certain conditions." << endl;
Settings s;
// Settings s;
// if (s.set() != 0) {
// return 0;
// }
// cout << "Set done" << endl;
// s.convert();
// cout << "DONE. Press enter to exit.";
// fflush(stdin);
// getchar();
MultiSettings s;
if (s.set() != 0) {
return 0;
}
s.beforeBody();
MarkdownSource source(s.inFile);
source.convertTo(s.outFile);
// convert(s.inFile, s.outFile);
s.afterBody();
cout << "DONE. Press enter to exit.";
fflush(stdin);
getchar();
s.convert();
return 0;
}
131 changes: 119 additions & 12 deletions settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with Markdown to HTML Convertor. If not, see <http://www.gnu.org/licenses/>.

#include "settings.h"
#include "Settings.h"
#include <iomanip>
#include <iostream>
#include <string>
Expand All @@ -30,15 +30,51 @@ string getTypeName(FileType t) {
int Settings::set() {
cout << "Input markdown file path: ";
cin >> inFileName;
inFile.open(inFileName);
if (!inFile.is_open()) {
if (inFile != nullptr) {
delete inFile;
}
inFile = new ifstream;
inFile->open(inFileName);
if (!inFile->is_open()) {
cout << "Error opening the markdown file." << endl;
return 1;
}
cout << "Input output file path: ";
cin >> outFileName;
outFile.open(outFileName);
if (!outFile.is_open()) {
if (outFile != nullptr) {
delete outFile;
}
outFile = new ofstream;
outFile->open(outFileName);
if (!outFile->is_open()) {
cout << "Error opening output file." << endl;
return 2;
}
if (outFileName.substr(outFileName.length() - 3, 3) == "vue") {
type = VUE;
}
return 0;
}
int Settings::set(string inFileName, string outFileName, bool addHtml = true, bool addCss = true) {
this->inFileName = inFileName;
this->outFileName = outFileName;
this->addHtml = addHtml;
this->addCss = addCss;
if (inFile != nullptr) {
delete inFile;
}
inFile = new ifstream;
inFile->open(inFileName);
if (!inFile->is_open()) {
cout << "Error opening the markdown file." << endl;
return 1;
}
if (outFile != nullptr) {
delete outFile;
}
outFile = new ofstream;
outFile->open(outFileName);
if (!outFile->is_open()) {
cout << "Error opening output file." << endl;
return 2;
}
Expand All @@ -55,9 +91,14 @@ void Settings::beforeBody() {
if (theLine == "thisShouldBeTheTitleOfYourWebsite") {
theLine = outFileName.substr(0, findChar(outFileName, 0, '.', ""));
}
outFile << theLine << endl;
*outFile << theLine << endl;
}
}
/**
* @brief return the theme's name
*
* @return string
*/
string selectTheme() {
vector<string> themes;
themes.push_back("default");
Expand All @@ -82,25 +123,91 @@ string selectTheme() {
}
return themes[res];
}
void Settings::afterBody() {
void Settings::afterBody(string theme) {
ifstream file("./source/html/" + getTypeName(type) + "End.html");
while (!file.eof()) {
string theLine;
getline(file, theLine);
outFile << theLine << endl;
*outFile << theLine << endl;
}
file.close();
// select style
file.open("./source/css/" + selectTheme() + ".css");
if (theme == "noThemeSelected") {
file.open("./source/css/" + selectTheme() + ".css");
} else {
file.open("./source/css/" + theme + ".css");
}
if (!(file.is_open())) {
cout << "Error opening css file" << endl;
return;
}
outFile << "<style>" << endl;
*outFile << "<style>" << endl;
while (!file.eof()) {
string theLine;
getline(file, theLine);
outFile << theLine << endl;
*outFile << theLine << endl;
}
*outFile << "</style>" << endl;
}
void Settings::convert(string theme) {
beforeBody();
// cout << "Before body done" << endl;
MarkdownSource source(*(inFile));
source.convertTo(*(outFile));
// convert(s.inFile, s.outFile);
afterBody(theme);
}
int MultiSettings::set() {
string s1, s2;
cout << "Input markdown file path(s): ";
fflush(stdin);
getline(cin, s1);
s1 = s1 + " ";
cout << "Input output file path(s); leave blank to set automatically: ";
fflush(stdin);
getline(cin, s2);
s2 = s2 + " ";
int i1 = 0, i2 = 0;
int l1 = s1.length();
int l2 = s2.length();
while (i1 < l1 && s1[i1] == ' ') {
i1++;
}
while (i2 < l2 && s2[i2] == ' ') {
i2++;
}
outFile << "</style>" << endl;
if (isEmpty(s2)) {
while (i1 != -1) {
Settings* settingsPtr = new Settings;
string fileName = fileNameStartFrom(s1, i1);
if (settingsPtr->set(fileName, pureFileName(fileName) + ".html", true, true) != 0) {
return settingsVec.size() + 1;
}
settingsVec.push_back(settingsPtr);
i1 = nextNotBlankPos(s1, findChar(s1, i1, ' ', ""));
}
} else {
while (i1 != -1 && i2 != -1) {
Settings* settingsPtr = new Settings;
string fileName = fileNameStartFrom(s1, i1);
string outFileName = fileNameStartFrom(s2, i2);
if (settingsPtr->set(fileName, outFileName, true, true) != 0) {
return settingsVec.size() + 1;
}
settingsVec.push_back(settingsPtr);
i1 = nextNotBlankPos(s1, findChar(s1, i1, ' ', ""));
i2 = nextNotBlankPos(s2, findChar(s2, i2, ' ', ""));
}
}
theme = selectTheme();
cout << "Set done" << endl;
return 0;
}
void MultiSettings::convert() {
for (auto settingPtr : settingsVec) {
settingPtr->convert(theme);
}
cout << "DONE. Press enter to exit.";
fflush(stdin);
getchar();
}
40 changes: 36 additions & 4 deletions settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

enum FileType {
Expand All @@ -35,10 +36,20 @@ class Settings {
FileType type;

public:
ifstream inFile;
ofstream outFile;
ifstream* inFile;
ofstream* outFile;
Settings()
: inFileName(""), outFileName(""), addHtml(false), addCss(false), type(HTML) {}
: inFileName(""), outFileName(""), addHtml(true), addCss(true), type(HTML), inFile(nullptr), outFile(nullptr) {}
~Settings() {
if (inFile != nullptr) {
inFile->close();
delete inFile;
}
if (outFile != nullptr) {
outFile->close();
delete outFile;
}
}
string getInFileName() const { return inFileName; }
string getOutFileName() const { return outFileName; }
/**
Expand All @@ -47,6 +58,16 @@ class Settings {
* @return int 0 if everything's fine, other if something went wrong
*/
int set();
/**
* @brief get settings from arg
*
* @param inFileName
* @param outFileName
* @param addHtml
* @param addCss
* @return int
*/
int set(string inFileName, string outFileName, bool addHtml, bool addCss);
/**
* @brief lines added before the body, such as <head>, <template>
*
Expand All @@ -56,7 +77,18 @@ class Settings {
* @brief lines added after the body, such as <style>
*
*/
void afterBody();
void afterBody(string theme = "noThemeSelected");
void convert(string theme = "noThemeSelected");
};

class MultiSettings {
protected:
vector<Settings*> settingsVec;
string theme;

public:
int set();
void convert();
};

#endif /* A8B8F23B_68D6_41E6_A207_690B54168564 */
32 changes: 32 additions & 0 deletions stringProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ int findChar(string& s, int startFrom, char c, string exclude) {
}
return -1;
}
int nextNotBlankPos(string& s, int startFrom) {
int i = startFrom, len = s.length();
while (i < len) {
if (s[i] != ' ' && s[i] != '\n') {
return i;
}
i++;
}
return -1;
}
Type startWith(string& s, int startFrom) {
int len = s.length();
if (startFrom == 0) {
Expand Down Expand Up @@ -246,3 +256,25 @@ void processLine(string& l, int startFrom, ofstream& outFile) {
}
outFile << endl;
}
string pureFileName(string& s) {
if (findChar(s, 0, '.', "") == -1) {
return "NotAFileName";
}
int i = s.length() - 1;
while (s[i] != '.') {
i--;
}
return s.substr(0, i);
}
string fileNameStartFrom(string& s, int startFrom) {
int i = startFrom;
int len = s.length();
while (i < len - 1 && s[i] == ' ') {
i++;
}
if (i >= len) {
return "NoMoreFileName";
}
int startPos = i, endPos = findChar(s, startPos, ' ', "");
return s.substr(startPos, endPos - startPos);
}
32 changes: 32 additions & 0 deletions stringProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ int findChar(string& s, int startFrom, char c, string exclude);
* @return false
*/
bool isEmpty(string& s);
/**
* @brief judge whether s consists of only space and \n from startFrom
*
* @param s
* @param startFrom
* @return true
* @return false
*/
bool isEmpty(string& s, int startFrom);
/**
* @brief find the index of the first digit in s starting from startFrom
*
Expand Down Expand Up @@ -95,4 +104,27 @@ void convert(ifstream& inFile, ofstream& outFile);
* @param outFile the output file
*/
void processLine(string& l, int startFrom, ofstream& outFile);
/**
* @brief extract pure file name from a file name. eg. return "index" from "index.md"
*
* @param s
* @return string return "NotAFileName" if s does not contain '.'
*/
string pureFileName(string& s);
/**
* @brief extract file name from s start from index startFrom. e.g. s="1.md 2.md 3.md", startFrom=4, return "2.md"
*
* @param s must end with a space
* @param startFrom
* @return string return "NoMoreFileName" if there's no file name
*/
string fileNameStartFrom(string& s, int startFrom);
/**
* @brief find next none blank char in s starting from s; if s[startFrom] is not blank, return startFrom
*
* @param s
* @param startFrom
* @return int return -1 if s starting from startFrom is empty
*/
int nextNotBlankPos(string& s, int startFrom);
#endif /* DE99A324_78F6_4041_9C09_5DB4135F2509 */

0 comments on commit 2bb29e3

Please sign in to comment.