-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
181 lines (165 loc) · 5.31 KB
/
main.cpp
File metadata and controls
181 lines (165 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <filesystem>
#include <fstream>
#include <string>
using namespace std;
namespace fs = std::filesystem;
// Убирает лишние пробелы, либо добавляет один пробел после символа, определяющего строку
// Примеры:
// "* some text" --> "* some text"
// "=>example.com text" --> "=> example.com text"
void formatSpaces(string &buff)
{
if (buff.empty()) return;
int pos = 0; // pos - позиция, где должен стоять пробел после символа, определяющего строку
switch (buff[0]) {
case '#': {
int count = 0;
for (auto &ch: buff) {
if (ch == '#') count++;
else break;
}
pos = count;
break;
}
case '*': {
pos = 1;
break;
}
case '>': {
pos = 1;
break;
}
case '=': {
pos = 2;
break;
}
default: {
return;
}
}
for (int i = pos; i < buff.size();) {
if (buff[i] == buff[i + 1] && buff[i] == ' ') buff.erase(pos, 1);
else break;
}
if (buff[pos] != ' ') {
buff.insert(pos, " ");
return;
}
}
// преобразует .gmi файл в .html
void toHtml(const fs::path &file, const fs::path &out_directory)
{
string filename = file.string();
filename = filename.substr(filename.rfind('\\'));
filename = filename.substr(0, filename.rfind("."));
ofstream html_file(out_directory.string() + filename + ".html");
ifstream original_file(file.string());
html_file << "<html>\n";
string buff;
bool isFirstUnList = true;
bool isPreformat = false;
while (!original_file.eof()) {
getline(original_file, buff);
if (!isFirstUnList && buff[0] != '*') {
html_file << "</ul>\n";
isFirstUnList = true;
}
if (isPreformat && buff[0] != '`') {
html_file << buff << "\n";
continue;
}
formatSpaces(buff);
switch (buff[0]) {
case '#': {
int count = 0;
for (auto &ch: buff) {
if (ch == '#') count++;
else break;
}
html_file << "<h" << count << ">";
html_file << buff.substr(count + 1) << "</h" << count << ">\n";
break;
}
case '*': {
if (isFirstUnList) {
html_file << "<ul>\n";
isFirstUnList = false;
}
html_file << "<li>" << buff.substr(2) << "</li>\n";
break;
}
case '>': {
html_file << "<blockquote>" << buff.substr(2) << "</blockquote>\n";
break;
}
case '=': {
html_file << "<p>\n<a href=\"";
int i;
for (i = 4; i < buff.size(); ++i) {
if (buff[i] == ' ') break;
}
if (i >= buff.size()) i = buff.size() - 1;
html_file << buff.substr(3, i - 3) << "\">";
html_file << buff.substr(i + 1) << "</a>\n</p>\n";
break;
}
case '`': {
if (!isPreformat) {
html_file << "<pre>\n";
isPreformat = true;
} else {
html_file << "</pre>\n";
isPreformat = false;
}
break;
}
default: {
if (buff.empty()) html_file << "<p> </p>\n";
else html_file << "<p>" << buff << "</p>\n";
break;
}
}
}
html_file << "</html";
html_file.close();
original_file.close();
}
// функция для обхода директории
void pass(const fs::path &curr_path, const fs::path &out_directory)
{
for (const auto &file: fs::directory_iterator(curr_path)) {
if (fs::is_directory(file)) {
string curr_folder_name = file.path().string();
curr_folder_name = curr_folder_name.substr(curr_folder_name.rfind('\\'));
fs::create_directory(out_directory.string() + curr_folder_name);
pass(file.path(), out_directory.string() + curr_folder_name);
} else {
string path = file.path().string();
if (path.rfind(".gmi") == string::npos) {
fs::copy(file, out_directory);
} else {
toHtml(file, out_directory);
}
}
}
}
int main()
{
setlocale(LC_ALL, "RUS");
string input_directory, output_directory;
cout << "Enter the full path to the input_directory:";
cin >> input_directory;
cout << "Enter the full path to the output_directory:";
cin >> output_directory;
if(!fs::is_directory(input_directory)) {
cout << "You have entered an incorrect path for input directory";
return 0;
}
if(!fs::is_directory(output_directory)) {
cout << "You have entered an incorrect path for output directory";
return 0;
}
pass(input_directory, output_directory);
return 0;
}