aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-06-24 11:34:55 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2025-06-24 11:34:55 +0530
commit5c4124a71f37875656b5f622f90a1ba02a26be44 (patch)
tree6982336e46c4453f2ad6bb87e500c70ec3110421 /src
parent7e4a3ce413eb967d440de3c417f10f8833f1e064 (diff)
(template): introduce template struct
Diffstat (limited to 'src')
-rw-r--r--src/main.c15
-rw-r--r--src/template.c14
2 files changed, 16 insertions, 13 deletions
diff --git a/src/main.c b/src/main.c
index 26f0b38..8e06dd9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -16,8 +16,7 @@
#include "../config.h"
-char *base_pre;
-char *base_post;
+template_t *base_template;
void
handle_file(const char *path)
@@ -71,13 +70,13 @@ handle_file(const char *path)
if (dot && strcmp(dot, ".md") == 0) {
MMIOT *doc = mkd_string(buffer, size, 0);
- fprintf(out, "%s", base_pre);
+ fprintf(out, "%s", base_template->pre);
markdown(doc, out, 0);
- fprintf(out, "%s", base_post);
+ fprintf(out, "%s", base_template->post);
} else {
if (strlen(buffer) != 0)
ingest(&buffer);
- fprintf(out, "%s%s%s", base_pre, buffer, base_post);
+ fprintf(out, "%s%s%s", base_template->pre, buffer, base_template->post);
}
free(buffer);
@@ -101,7 +100,7 @@ main(int argc, char **argv)
return EXIT_FAILURE;
}
- template_initialize(&base_pre, &base_post);
+ base_template = template_create();
int err = mkdir(OUTPUT, 0700);
if (err != 0 && errno != EEXIST) {
@@ -128,8 +127,8 @@ main(int argc, char **argv)
free(filepath);
}
- free(base_pre);
- free(base_post);
+ free(base_template->pre);
+ free(base_template->post);
return EXIT_SUCCESS;
}
diff --git a/src/template.c b/src/template.c
index 95a54d9..e3ce60a 100644
--- a/src/template.c
+++ b/src/template.c
@@ -8,21 +8,25 @@
#include "../config.h"
-void
-template_initialize(char **base_pre, char **base_post)
+template_t *
+template_create(void)
{
+ template_t *template = malloc(sizeof(template_t));
+
FILE *base = fopen(DIRECTORY "/" BASE_TEMPLATE, "r");
unsigned int size = fsize(base);
char *contents = fcontent(base, size);
+ fclose(base);
key_match_t *match = find_next_key(contents);
- asprintf(base_pre, "%.*s", match->offset, contents);
- asprintf(base_post,
+ asprintf(&template->pre, "%.*s", match->offset, contents);
+ asprintf(&template->post,
"%.*s",
size - match->offset - match->length,
contents + match->offset + match->length);
free(contents);
- fclose(base);
+ free(match);
+ return template;
}