-
-
Save Voultapher/ec1840178c186827b52df3afeb308802 to your computer and use it in GitHub Desktop.
Syntax highlight markdown change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
#include <string> | |
#include <algorithm> | |
#include <numeric> | |
using buffer_t = std::vector<char>; | |
using lines_t = std::vector<std::string>; | |
buffer_t read_file(const char* filepath) | |
{ | |
std::ifstream file(filepath); | |
if (file.fail()) | |
{ | |
std::cout << "File not found: " << filepath << '\n'; | |
return {}; | |
} | |
file.seekg(0, std::ios::end); // seek to the end | |
int filesize = file.tellg(); // get the file size | |
file.seekg(0, std::ios::beg); | |
filesize -= file.tellg(); // reduce filesize by any header bytes | |
buffer_t buffer; | |
buffer.resize(filesize); | |
file.read(buffer.data(), filesize); | |
file.close(); | |
return buffer; | |
} | |
void write_file(const char* filepath, const std::string& data) | |
{ | |
static std::ofstream file; | |
file.open(filepath); | |
if (file.fail()) | |
{ | |
std::cout << "File not found: " << filepath << '\n'; | |
return; | |
} | |
file << data; | |
file.close(); | |
} | |
lines_t convert_to_lines(const buffer_t& buffer) | |
{ | |
lines_t lines; | |
std::string line; | |
line.reserve(100); | |
std::for_each( | |
std::cbegin(buffer), std::cend(buffer), | |
[&line, &lines](const auto element) -> void | |
{ | |
if (element != '\n') | |
line += element; | |
else | |
{ | |
lines.push_back(line + element); | |
line = ""; | |
} | |
} | |
); | |
return lines; | |
} | |
std::string convert_to_string(const lines_t& lines, size_t size) | |
{ | |
std::string res; | |
res.reserve(size); | |
std::accumulate( | |
std::cbegin(lines), std::cend(lines), | |
false, | |
[&res](const bool is_code, const auto& line) -> bool | |
{ | |
if (line.size() >= 4 && line.substr(0, 4) == " ") | |
{ | |
if (!is_code) | |
res += "```cpp\n"; | |
res += line.substr(4); | |
return true; | |
} | |
else if (is_code && line == "\n") | |
{ | |
res += line; | |
return true; | |
} | |
else if (is_code) | |
res += "```\n\n"; | |
res += line; | |
return false; | |
} | |
); | |
return res; | |
} | |
void update_syntax() | |
{ | |
const char* filepath = "E://Desktop//C--//C++//zz_Else//CppCoreGuidelines//CppCoreGuidelines.md"; | |
auto buffer = read_file(filepath); | |
auto lines = convert_to_lines(buffer); | |
auto s = convert_to_string(lines, buffer.size()); | |
write_file(filepath, s); | |
} | |
int main() | |
{ | |
update_syntax(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment