diff options
Diffstat (limited to 'src/blogc-make')
| -rw-r--r-- | src/blogc-make/ctx.c | 17 | ||||
| -rw-r--r-- | src/blogc-make/ctx.h | 4 | ||||
| -rw-r--r-- | src/blogc-make/exec.c | 16 | ||||
| -rw-r--r-- | src/blogc-make/exec.h | 8 | ||||
| -rw-r--r-- | src/blogc-make/rules.c | 38 | 
5 files changed, 49 insertions, 34 deletions
| diff --git a/src/blogc-make/ctx.c b/src/blogc-make/ctx.c index 74932c9..8e152da 100644 --- a/src/blogc-make/ctx.c +++ b/src/blogc-make/ctx.c @@ -24,7 +24,8 @@  bm_filectx_t* -bm_filectx_new(bm_ctx_t *ctx, const char *filename, struct stat *st) +bm_filectx_new(bm_ctx_t *ctx, const char *filename, const char *slug, +    struct stat *st)  {      if (ctx == NULL || filename == NULL)          return NULL; @@ -35,6 +36,7 @@ bm_filectx_new(bm_ctx_t *ctx, const char *filename, struct stat *st)      bm_filectx_t *rv = bc_malloc(sizeof(bm_filectx_t));      rv->path = f;      rv->short_path = bc_strdup(filename); +    rv->slug = bc_strdup(slug);      if (st == NULL) {          struct stat buf; @@ -93,7 +95,7 @@ bm_filectx_new_r(bc_slist_t *l, bm_ctx_t *ctx, const char *filename)          return l;      } -    l = bc_slist_append(l, bm_filectx_new(ctx, filename, &buf)); +    l = bc_slist_append(l, bm_filectx_new(ctx, filename, NULL, &buf));      free(f);      return l;  } @@ -155,6 +157,7 @@ bm_filectx_free(bm_filectx_t *fctx)          return;      free(fctx->path);      free(fctx->short_path); +    free(fctx->slug);      free(fctx);  } @@ -200,7 +203,7 @@ bm_ctx_new(bm_ctx_t *base, const char *settings_file, const char *argv0,      rv->settings = settings;      char *real_filename = realpath(settings_file, NULL); -    rv->settings_fctx = bm_filectx_new(rv, real_filename, NULL); +    rv->settings_fctx = bm_filectx_new(rv, real_filename, NULL, NULL);      rv->root_dir = realpath(dirname(real_filename), NULL);      free(real_filename); @@ -222,10 +225,10 @@ bm_ctx_new(bm_ctx_t *base, const char *settings_file, const char *argv0,      char *main_template = bc_strdup_printf("%s/%s", template_dir,          bc_trie_lookup(settings->settings, "main_template")); -    rv->main_template_fctx = bm_filectx_new(rv, main_template, NULL); +    rv->main_template_fctx = bm_filectx_new(rv, main_template, NULL, NULL);      free(main_template); -    rv->atom_template_fctx = bm_filectx_new(rv, atom_template, NULL); +    rv->atom_template_fctx = bm_filectx_new(rv, atom_template, NULL, NULL);      free(atom_template);      const char *content_dir = bc_trie_lookup(settings->settings, "content_dir"); @@ -238,7 +241,7 @@ bm_ctx_new(bm_ctx_t *base, const char *settings_file, const char *argv0,              char *f = bc_strdup_printf("%s/%s/%s%s", content_dir, post_prefix,                  settings->posts[i], source_ext);              rv->posts_fctx = bc_slist_append(rv->posts_fctx, -                bm_filectx_new(rv, f, NULL)); +                bm_filectx_new(rv, f, settings->posts[i], NULL));              free(f);          }      } @@ -249,7 +252,7 @@ bm_ctx_new(bm_ctx_t *base, const char *settings_file, const char *argv0,              char *f = bc_strdup_printf("%s/%s%s", content_dir,                  settings->pages[i], source_ext);              rv->pages_fctx = bc_slist_append(rv->pages_fctx, -                bm_filectx_new(rv, f, NULL)); +                bm_filectx_new(rv, f, settings->pages[i], NULL));              free(f);          }      } diff --git a/src/blogc-make/ctx.h b/src/blogc-make/ctx.h index 3f70ead..294e0d3 100644 --- a/src/blogc-make/ctx.h +++ b/src/blogc-make/ctx.h @@ -37,6 +37,7 @@  typedef struct {      char *path;      char *short_path; +    char *slug;      time_t tv_sec;      long tv_nsec;      bool readable; @@ -64,7 +65,8 @@ typedef struct {      bc_slist_t *copy_fctx;  } bm_ctx_t; -bm_filectx_t* bm_filectx_new(bm_ctx_t *ctx, const char *filename, struct stat *st); +bm_filectx_t* bm_filectx_new(bm_ctx_t *ctx, const char *filename, const char *slug, +    struct stat *st);  bc_slist_t* bm_filectx_new_r(bc_slist_t *l, bm_ctx_t *ctx, const char *filename);  bool bm_filectx_changed(bm_filectx_t *ctx, time_t *tv_sec, long *tv_nsec);  void bm_filectx_reload(bm_filectx_t *ctx); diff --git a/src/blogc-make/exec.c b/src/blogc-make/exec.c index a324d47..d246df9 100644 --- a/src/blogc-make/exec.c +++ b/src/blogc-make/exec.c @@ -217,8 +217,8 @@ list_variables(const char *key, const char *value, bc_string_t *str)  char*  bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings, -    bc_trie_t *variables, bool listing, const char *template, -    const char *output, bool dev, bool sources_stdin) +    bc_trie_t *global_variables, bc_trie_t *local_variables, bool listing, +    const char *template, const char *output, bool dev, bool sources_stdin)  {      bc_string_t *rv = bc_string_new(); @@ -245,7 +245,8 @@ bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings,              (bc_trie_foreach_func_t) list_variables, rv);      } -    bc_trie_foreach(variables, (bc_trie_foreach_func_t) list_variables, rv); +    bc_trie_foreach(global_variables, (bc_trie_foreach_func_t) list_variables, rv); +    bc_trie_foreach(local_variables, (bc_trie_foreach_func_t) list_variables, rv);      if (dev) {          bc_string_append(rv, " -D MAKE_ENV_DEV=1 -D MAKE_ENV='dev'"); @@ -276,8 +277,8 @@ bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings,  int -bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *variables, bool listing, -    bm_filectx_t *template, bm_filectx_t *output, bc_slist_t *sources, +bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *global_variables, bc_trie_t *local_variables, +    bool listing, bm_filectx_t *template, bm_filectx_t *output, bc_slist_t *sources,      bool only_first_source)  {      if (ctx == NULL) @@ -290,8 +291,9 @@ bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *variables, bool listing,              break;      } -    char *cmd = bm_exec_build_blogc_cmd(ctx->blogc, ctx->settings, variables, -        listing, template->path, output->path, ctx->dev, input->len > 0); +    char *cmd = bm_exec_build_blogc_cmd(ctx->blogc, ctx->settings, global_variables, +        local_variables, listing, template->path, output->path, ctx->dev, +        input->len > 0);      if (ctx->verbose)          printf("%s\n", cmd); diff --git a/src/blogc-make/exec.h b/src/blogc-make/exec.h index 715c903..85f2a44 100644 --- a/src/blogc-make/exec.h +++ b/src/blogc-make/exec.h @@ -19,10 +19,10 @@ char* bm_exec_find_binary(const char *argv0, const char *bin, const char *env);  int bm_exec_command(const char *cmd, const char *input, char **output,      char **error, bc_error_t **err);  char* bm_exec_build_blogc_cmd(const char *blogc_bin, bm_settings_t *settings, -    bc_trie_t *variables, bool listing, const char *template, -    const char *output, bool dev, bool sources_stdin); -int bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *variables, bool listing, -    bm_filectx_t *template, bm_filectx_t *output, bc_slist_t *sources, +    bc_trie_t *global_variables, bc_trie_t *local_variables, bool listing, +    const char *template, const char *output, bool dev, bool sources_stdin); +int bm_exec_blogc(bm_ctx_t *ctx, bc_trie_t *global_variables, bc_trie_t *local_variables, +    bool listing, bm_filectx_t *template, bm_filectx_t *output, bc_slist_t *sources,      bool only_first_source);  int bm_exec_blogc_runserver(bm_ctx_t *ctx, const char *host, const char *port,      const char *threads); diff --git a/src/blogc-make/rules.c b/src/blogc-make/rules.c index 8a0558b..d8614b6 100644 --- a/src/blogc-make/rules.c +++ b/src/blogc-make/rules.c @@ -52,7 +52,7 @@ index_outputlist(bm_ctx_t *ctx)      char *f = bc_strdup_printf("%s%s%s%s", ctx->short_output_dir,          is_index ? "" : "/", is_index ? "" : index_prefix,          html_ext); -    rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL)); +    rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL));      free(f);      return rv;  } @@ -82,7 +82,7 @@ index_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args)          if (bm_rule_need_rebuild(ctx->posts_fctx, ctx->settings_fctx,                  ctx->main_template_fctx, fctx, false))          { -            rv = bm_exec_blogc(ctx, variables, true, ctx->main_template_fctx, +            rv = bm_exec_blogc(ctx, variables, NULL, true, ctx->main_template_fctx,                  fctx, ctx->posts_fctx, false);              if (rv != 0)                  break; @@ -109,7 +109,7 @@ atom_outputlist(bm_ctx_t *ctx)      const char *atom_ext = bc_trie_lookup(ctx->settings->settings, "atom_ext");      char *f = bc_strdup_printf("%s/%s%s", ctx->short_output_dir,          atom_prefix, atom_ext); -    rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL)); +    rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL));      free(f);      return rv;  } @@ -139,7 +139,7 @@ atom_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args)          if (bm_rule_need_rebuild(ctx->posts_fctx, ctx->settings_fctx, NULL,                  fctx, false))          { -            rv = bm_exec_blogc(ctx, variables, true, ctx->atom_template_fctx, +            rv = bm_exec_blogc(ctx, variables, NULL, true, ctx->atom_template_fctx,                  fctx, ctx->posts_fctx, false);              if (rv != 0)                  break; @@ -167,7 +167,7 @@ atom_tags_outputlist(bm_ctx_t *ctx)      for (size_t i = 0; ctx->settings->tags[i] != NULL; i++) {          char *f = bc_strdup_printf("%s/%s/%s%s", ctx->short_output_dir,              atom_prefix, ctx->settings->tags[i], atom_ext); -        rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL)); +        rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL));          free(f);      }      return rv; @@ -203,7 +203,7 @@ atom_tags_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args)          if (bm_rule_need_rebuild(ctx->posts_fctx, ctx->settings_fctx, NULL,                  fctx, false))          { -            rv = bm_exec_blogc(ctx, variables, true, ctx->atom_template_fctx, +            rv = bm_exec_blogc(ctx, variables, NULL, true, ctx->atom_template_fctx,                  fctx, ctx->posts_fctx, false);              if (rv != 0)                  break; @@ -239,7 +239,7 @@ pagination_outputlist(bm_ctx_t *ctx)      for (size_t i = 0; i < pages; i++) {          char *f = bc_strdup_printf("%s/%s/%d%s", ctx->short_output_dir,              pagination_prefix, i + 1, html_ext); -        rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL)); +        rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL));          free(f);      }      return rv; @@ -271,7 +271,7 @@ pagination_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args)          if (bm_rule_need_rebuild(ctx->posts_fctx, ctx->settings_fctx,                  ctx->main_template_fctx, fctx, false))          { -            rv = bm_exec_blogc(ctx, variables, true, ctx->main_template_fctx, +            rv = bm_exec_blogc(ctx, variables, NULL, true, ctx->main_template_fctx,                  fctx, ctx->posts_fctx, false);              if (rv != 0)                  break; @@ -301,7 +301,7 @@ posts_outputlist(bm_ctx_t *ctx)      for (size_t i = 0; ctx->settings->posts[i] != NULL; i++) {          char *f = bc_strdup_printf("%s/%s/%s%s", ctx->short_output_dir,              post_prefix, ctx->settings->posts[i], html_ext); -        rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL)); +        rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL));          free(f);      }      return rv; @@ -328,14 +328,18 @@ posts_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args)      for (s = ctx->posts_fctx, o = outputs; s != NULL && o != NULL;              s = s->next, o = o->next)      { +        bm_filectx_t *s_fctx = s->data;          bm_filectx_t *o_fctx = o->data;          if (o_fctx == NULL)              continue;          if (bm_rule_need_rebuild(s, ctx->settings_fctx,                  ctx->main_template_fctx, o_fctx, true))          { -            rv = bm_exec_blogc(ctx, variables, false, ctx->main_template_fctx, +            bc_trie_t *local = bc_trie_new(NULL); +            bc_trie_insert(local, "MAKE_SLUG", s_fctx->slug);  // no need to copy +            rv = bm_exec_blogc(ctx, variables, local, false, ctx->main_template_fctx,                  o_fctx, s, true); +            bc_trie_free(local);              if (rv != 0)                  break;          } @@ -362,7 +366,7 @@ tags_outputlist(bm_ctx_t *ctx)      for (size_t i = 0; ctx->settings->tags[i] != NULL; i++) {          char *f = bc_strdup_printf("%s/%s/%s%s", ctx->short_output_dir,              tag_prefix, ctx->settings->tags[i], html_ext); -        rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL)); +        rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL));          free(f);      }      return rv; @@ -399,7 +403,7 @@ tags_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args)          if (bm_rule_need_rebuild(ctx->posts_fctx, ctx->settings_fctx,                  ctx->main_template_fctx, fctx, false))          { -            rv = bm_exec_blogc(ctx, variables, true, ctx->main_template_fctx, +            rv = bm_exec_blogc(ctx, variables, NULL, true, ctx->main_template_fctx,                  fctx, ctx->posts_fctx, false);              if (rv != 0)                  break; @@ -429,7 +433,7 @@ pages_outputlist(bm_ctx_t *ctx)          char *f = bc_strdup_printf("%s%s%s%s", ctx->short_output_dir,              is_index ? "" : "/", is_index ? "" : ctx->settings->pages[i],              html_ext); -        rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL)); +        rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL));          free(f);      }      return rv; @@ -454,14 +458,18 @@ pages_exec(bm_ctx_t *ctx, bc_slist_t *outputs, bc_trie_t *args)      for (s = ctx->pages_fctx, o = outputs; s != NULL && o != NULL;              s = s->next, o = o->next)      { +        bm_filectx_t *s_fctx = s->data;          bm_filectx_t *o_fctx = o->data;          if (o_fctx == NULL)              continue;          if (bm_rule_need_rebuild(s, ctx->settings_fctx,                  ctx->main_template_fctx, o_fctx, true))          { -            rv = bm_exec_blogc(ctx, variables, false, ctx->main_template_fctx, +            bc_trie_t *local = bc_trie_new(NULL); +            bc_trie_insert(local, "MAKE_SLUG", s_fctx->slug); // no need to copy +            rv = bm_exec_blogc(ctx, variables, local, false, ctx->main_template_fctx,                  o_fctx, s, true); +            bc_trie_free(local);              if (rv != 0)                  break;          } @@ -487,7 +495,7 @@ copy_outputlist(bm_ctx_t *ctx)      for (bc_slist_t *s = ctx->copy_fctx; s != NULL; s = s->next) {          char *f = bc_strdup_printf("%s/%s", ctx->short_output_dir,              ((bm_filectx_t*) s->data)->short_path); -        rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL)); +        rv = bc_slist_append(rv, bm_filectx_new(ctx, f, NULL, NULL));          free(f);      }      return rv; | 
