Skip to content

Commit

Permalink
refactoring to improve code maintainability:
Browse files Browse the repository at this point in the history
added clarifying comments to title check

updated regex error name to be more informative

added spacing for code clarity

simplified non-sub-directory checking condition

added line spacing for code clarity
  • Loading branch information
rudychung committed Oct 11, 2022
1 parent 9aa0002 commit 9167d64
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/Files.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <fstream>
#include "Files.h"


Files::Files(std::string inputPathStr, std::string outputPathStr) {
inputPath = std::filesystem::path(inputPathStr);
outputPath = std::filesystem::path(outputPathStr);
Expand All @@ -14,8 +13,7 @@ Files::Files(std::string inputPathStr, std::string outputPathStr) {
for (const std::filesystem::directory_entry& i : std::filesystem::recursive_directory_iterator{ inputPath })
{
// if file is not a directory, add to texts vector
if (!std::filesystem::is_directory(i.path().string()) && i.path().extension().string() == ".txt" ||
!std::filesystem::is_directory(i.path().string()) && i.path().extension().string() == ".md") {
if (std::filesystem::is_directory(i.path()) && (i.path().extension().string() == ".txt" || i.path().extension().string() == ".md")) {
texts.push_back(i.path());
}
}
Expand All @@ -40,6 +38,7 @@ void Files::createFiles() {
std::filesystem::path("./" + outputPath.string() + "/" + i.getHtmlName()).make_preferred()
);
}

// if input path is a directory, generate an index page
if (std::filesystem::is_directory(inputPath)) {
createIndexPage();
Expand Down
8 changes: 6 additions & 2 deletions src/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ Text::Text(std::filesystem::path filePath) {
// get filename and extension from filepath
m_fileName = filePath.filename().string();
m_fileExt = m_fileName.substr(m_fileName.rfind('.'));

// check if text contains title
std::ifstream ifs(m_filePath);
std::string tempString;
int checkCount = 0;
for (int i = 0; i < 3; i++) {
std::getline(ifs, tempString, '\n');
// 1st check, title is present
if (i == 0 && tempString.length() > 0) {
checkCount++;
}
// 2nd and 3rd check, subsequent 2 lines are empty
else if (i > 0 && tempString.length() == 0) {
checkCount++;
}
Expand Down Expand Up @@ -91,9 +94,10 @@ void Text::parseMarkdown(std::string& tempString, std::ostream& ofs, bool& inPar
tempString = std::regex_replace(tempString, italAs, "<i>$2</i>");
tempString = std::regex_replace(tempString, code, "<code>$2</code>");
}
catch (const std::regex_error& e) {
std::cout << "regex error caught: " << e.what() << "\n";
catch (const std::regex_error& error) {
std::cout << "regex error caught: " << error.what() << "\n";
}

if (tempString.find("## ", 0, 3) != std::string::npos) {
ofs << (inParagraph ? "</p>\n" : "") << "<h2>" << tempString.substr(3) << "</h2>" << std::endl;
inParagraph = false;
Expand Down

0 comments on commit 9167d64

Please sign in to comment.