aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/template.h7
-rw-r--r--src/main.c15
-rw-r--r--src/template.c14
3 files changed, 22 insertions, 14 deletions
diff --git a/include/template.h b/include/template.h
index e29e6b5..de36293 100644
--- a/include/template.h
+++ b/include/template.h
@@ -1,6 +1,11 @@
#ifndef __TEMPLATE_H
#define __TEMPLATE_H
-void template_initialize(char **base_pre, char **base_post);
+typedef struct {
+ char *pre;
+ char *post;
+} template_t;
+
+template_t *template_create(void);
#endif
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;
}