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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*
* msg
* Copyright (C) 2025 Raghuram Subramani <raghus2247@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <config.h>
#include <copy.h>
#include <engine.h>
#include <errno.h>
#include <filehandler.h>
#include <ftw.h>
#include <lexer.h>
#include <libgen.h>
#include <list.h>
#include <mkdio.h>
#include <msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <template.h>
#include <time.h>
#include <util.h>
#define LOG_THRESHOLD 6
extern msg_t *msg;
void
handle_file(const char *path)
{
char *inpath;
char *outpath;
asprintf(&inpath, "%s/%s", msg->base_directory, path);
char *dot = strrchr(inpath, '.');
if (dot && strcmp(dot, ".md") == 0) {
asprintf(&outpath,
"%s/%.*s.html",
msg->output_directory,
(int) strlen(path) - 3,
path);
} else {
asprintf(&outpath, "%s/%s", msg->output_directory, path);
}
char *temp_outpath = strdup(outpath);
char *directory = dirname(temp_outpath);
char *next = calloc(strlen(directory) + 1, sizeof(char));
strcpy(next, "");
char *token;
for (token = strtok(directory, "/"); token != NULL;
token = strtok(NULL, "/")) {
if (strcmp(next, "") != 0) {
strcat(next, "/");
}
strcat(next, token);
mkdir(next, 0700);
}
free(next);
free(temp_outpath);
FILE *in = fopen(inpath, "r");
if (in == NULL) {
printf("Failed to open %s\n", inpath);
return;
}
FILE *out = fopen(outpath, "w");
if (out == NULL) {
printf("Failed to open %s\n", outpath);
fclose(in);
return;
}
unsigned int size = fsize(in);
char *buffer = fcontent(in, size);
if (dot && strcmp(dot, ".md") == 0) {
engine_t engine = { .config = NULL, .content_headers = NULL };
char *p = strstr(buffer, "---");
if (p != NULL) {
char *config;
asprintf(&config, "%.*s\n", (int) (p - buffer), buffer);
engine.config = config_parse(config);
free(config);
char *tempbuffer = strdup(p);
free(buffer);
asprintf(&buffer, "%s", tempbuffer + strlen("---"));
free(tempbuffer);
}
mkd_flag_t *flags = mkd_flags();
mkd_set_flag_num(flags, MKD_FENCEDCODE);
MMIOT *doc = mkd_string(buffer, strlen(buffer), flags);
free(flags);
template_write(&engine, out, doc, true);
if (engine.config != NULL)
config_delete(engine.config);
} else if (strlen(buffer) != 0) {
engine_t *engine = engine_ingest(&buffer, false);
template_write(engine, out, buffer, false);
engine_delete(engine);
}
free(buffer);
fclose(in);
fclose(out);
free(inpath);
free(outpath);
}
int
run(bool log)
{
if (log) {
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("Generation started at: %s", asctime(timeinfo));
}
struct stat sb;
if (stat(msg->base_directory, &sb) != 0 || !S_ISDIR(sb.st_mode)) {
printf("%s does not exist.\n", msg->base_directory);
return EXIT_FAILURE;
}
char *config_path;
asprintf(&config_path, "%s/%s", msg->base_directory, CONFIG_FILE);
config_t *config = config_fetch_and_parse(config_path);
free(config_path);
if (config == NULL)
return EXIT_FAILURE;
msg->config = config;
template_initialize();
int err = mkdir(msg->output_directory, 0700);
if (err != 0 && errno != EEXIST) {
perror("mkdir");
return EXIT_FAILURE;
}
list_t *static_ = unwrap(list_find_corresponding_value_from_ptr_wrapper(
config->keys, config->array_values, "static"));
if (static_ == NULL) {
printf("Could not find resources in config.cfg\n");
return EXIT_FAILURE;
}
for (size_t i = 0; i < static_->size; i++) {
ptr_wrapper_t *value = list_get(static_, i);
char *path = NULL;
asprintf(&path, "%s/%s", msg->base_directory, (char *) value->ptr);
struct stat path_stat;
stat(path, &path_stat);
/* TODO: Error handling */
if (S_ISREG(path_stat.st_mode))
copy_recursively(path, NULL, FTW_F, NULL);
else if (S_ISDIR(path_stat.st_mode))
nftw(path, copy_recursively, 64, FTW_PHYS | FTW_ACTIONRETVAL);
free(path);
}
list_t *resources = unwrap(list_find_corresponding_value_from_ptr_wrapper(
config->keys, config->array_values, "resources"));
if (resources == NULL) {
printf("Could not find resources in config.cfg\n");
return EXIT_FAILURE;
}
for (size_t i = 0; i < resources->size; i++) {
ptr_wrapper_t *value = list_get(resources, i);
char *path = value->ptr;
if (log) {
if (i < LOG_THRESHOLD || msg->verbose)
printf("\tProcessing %s\n", path);
else if (i == LOG_THRESHOLD && !msg->verbose)
printf("\t...\n");
}
handle_file(path);
}
template_clean();
config_delete(config);
return EXIT_SUCCESS;
}
|