Browse Source

fix templater

master
alistair 3 years ago
parent
commit
8324aa187e
  1. 14
      main.cpp
  2. 45
      templater.hpp
  3. 2
      util.h

14
main.cpp

@ -91,7 +91,7 @@ class builder { @@ -91,7 +91,7 @@ class builder {
mmd::markdown_parser parser;
std::map<std::string, std::string> default_templates;
std::map<std::string, std::string> properties;
templater templr;
templater *templr;
const std::set<std::string> apply_templates_exts {"md","html", "txt", "markdown", "xml", "atom", "rss"};
@ -271,7 +271,7 @@ class builder { @@ -271,7 +271,7 @@ class builder {
}
if (!article_properties.count("notemplating")) {
templr.run_substitution_plugins(text, article_properties);
templr->run_substitution_plugins(text, article_properties);
}
std::string default_template_name = "none";
@ -319,7 +319,7 @@ class builder { @@ -319,7 +319,7 @@ class builder {
}
/* write body into template */
templr.run_substitution_plugins(new_page, article_properties);
templr->run_substitution_plugins(new_page, article_properties);
article_properties["body"] = new_page;
notify_file_write(target);
@ -414,7 +414,7 @@ class builder { @@ -414,7 +414,7 @@ class builder {
properties["css"] = css;
properties["current_file"] = "default_html_template";
templr.run_substitution_plugins(html_template, properties);
templr->run_substitution_plugins(html_template, properties);
default_templates["html"]= html_template;
default_templates["css"] = css;
@ -427,13 +427,13 @@ class builder { @@ -427,13 +427,13 @@ class builder {
builder(std::map<std::string, std::string> m) : properties(m)
{
templr = templater({new variable_transclude_plugin{}, new file_index_plugin{{SITE_CONFIG_FNAME, "index.md", "html.template"}}, new ifdef_plugin{}, new ifndef_plugin{}, new file_transclude_plugin{}, new mmd_snippet_transclude_plugin{}},
templr = new templater({new variable_transclude_plugin{}, new file_index_plugin{{SITE_CONFIG_FNAME, "index.md", "html.template"}}, new ifdef_plugin{}, new ifndef_plugin{}, new file_transclude_plugin{}, new mmd_snippet_transclude_plugin{}},
{new rss_feed_plugin{}, new microblog_plugin{}});
add_default_templates();
}
~builder() {}
~builder() {delete templr;}
bool get_confirmation(std::string message) {
if (settings.no_interactive) {
@ -481,7 +481,7 @@ class builder { @@ -481,7 +481,7 @@ class builder {
if (job_type::TEMPLATE & e.second.type) {
// reapply stg1 and stg2 templates
std::string page = e.second.properties.at("body");
templr.run_substitution_plugins(page,
templr->run_substitution_plugins(page,
e.second.properties, compile_jobs);
e.second.properties["body"] = page;
}

45
templater.hpp

@ -41,7 +41,8 @@ class substitution_plugin { @@ -41,7 +41,8 @@ class substitution_plugin {
std::vector<std::string> get_arguments(const std::string &invocation, int numargs);
/* Must be globally unique: invocation name in the template */
inline static const std::string hook_name = "invalid";
virtual std::string hook_name() = 0;
/* Must return lengh of replaced text */
virtual int perform_substitution(int start, int end, const std::string &invocation,
@ -115,7 +116,7 @@ std::vector<std::string> substitution_plugin::get_arguments(const std::string &i @@ -115,7 +116,7 @@ std::vector<std::string> substitution_plugin::get_arguments(const std::string &i
class file_transclude_plugin : public substitution_plugin {
public:
inline static const std::string hook_name = "include";
std::string hook_name() override {return "include";};
int perform_substitution(int start, int end, const std::string &invocation,
std::string &file_text,
@ -148,7 +149,7 @@ class file_transclude_plugin : public substitution_plugin { @@ -148,7 +149,7 @@ class file_transclude_plugin : public substitution_plugin {
class mmd_snippet_transclude_plugin : public substitution_plugin {
public:
inline static const std::string hook_name = "md";
std::string hook_name() override {return "md";};
int perform_substitution(int start, int end, const std::string &invocation,
std::string &file_text,
@ -171,7 +172,7 @@ class mmd_snippet_transclude_plugin : public substitution_plugin { @@ -171,7 +172,7 @@ class mmd_snippet_transclude_plugin : public substitution_plugin {
class variable_transclude_plugin final : public substitution_plugin {
public:
inline static const std::string hook_name = "";
std::string hook_name() override {return "";};
int perform_substitution(int start, int end, const std::string &invocation,
std::string &file_text,
@ -244,7 +245,7 @@ class ifdef_plugin : public substitution_plugin { @@ -244,7 +245,7 @@ class ifdef_plugin : public substitution_plugin {
public:
inline static const std::string hook_name = "ifdef";
std::string hook_name() override {return "ifdef";};
int perform_substitution(int start, int end, const std::string &invocation,
std::string &file_text,
@ -271,7 +272,7 @@ class ifdef_plugin : public substitution_plugin { @@ -271,7 +272,7 @@ class ifdef_plugin : public substitution_plugin {
class comment_plugin : public substitution_plugin {
public:
inline static const std::string hook_name = "#";
std::string hook_name() override {return "#";};
int perform_substitution(int start, int end, const std::string &invocation,
std::string &file_text,
@ -290,7 +291,7 @@ class comment_plugin : public substitution_plugin { @@ -290,7 +291,7 @@ class comment_plugin : public substitution_plugin {
class ifndef_plugin : public ifdef_plugin {
public:
inline static const std::string hook_name = "ifndef";
std::string hook_name() override {return "ifndef";};
int perform_substitution(int start, int end, const std::string &invocation,
std::string &file_text,
@ -422,7 +423,7 @@ class file_index_plugin : public substitution_plugin { @@ -422,7 +423,7 @@ class file_index_plugin : public substitution_plugin {
public:
inline static const std::string hook_name = "postlist";
std::string hook_name() override {return "postlist";};
int perform_substitution(int start, int end, const std::string &invocation,
std::string &file_text,
@ -720,7 +721,7 @@ std::multimap<time_t, blog_item> get_sorted_post_list(const std::string &cs_dire @@ -720,7 +721,7 @@ std::multimap<time_t, blog_item> get_sorted_post_list(const std::string &cs_dire
class rss_feed_plugin : public s2_substitution_plugin {
public:
inline static const std::string hook_name = "feed";
std::string hook_name() override {return "feed";};
int perform_substitution(int start, int end, const std::string &invocation,
@ -778,7 +779,7 @@ class rss_feed_plugin : public s2_substitution_plugin { @@ -778,7 +779,7 @@ class rss_feed_plugin : public s2_substitution_plugin {
class microblog_plugin : public s2_substitution_plugin {
public:
inline static const std::string hook_name = "microblog";
std::string hook_name() override {return "microblog";};
int perform_substitution(int start, int end, const std::string &invocation,
@ -839,6 +840,7 @@ class templater { @@ -839,6 +840,7 @@ class templater {
public:
templater() {
/*
substitution_commands[variable_transclude_plugin::hook_name] = new variable_transclude_plugin {};
substitution_commands[file_index_plugin::hook_name] = new file_index_plugin{};
substitution_commands[ifdef_plugin::hook_name] = new ifdef_plugin{};
@ -848,20 +850,27 @@ class templater { @@ -848,20 +850,27 @@ class templater {
s2_substitution_commands[rss_feed_plugin::hook_name] = new rss_feed_plugin{};
s2_substitution_commands[microblog_plugin::hook_name] = new microblog_plugin{};
*/
}
templater(std::vector<substitution_plugin *> s1, std::vector<s2_substitution_plugin *> s2) {
for (auto s : s1) {
substitution_commands.insert({s->hook_name, s});
substitution_commands.insert({s->hook_name(), s});
}
for (auto s : s2) {
s2_substitution_commands.insert({s->hook_name, s});
s2_substitution_commands.insert({s->hook_name(), s});
}
for (auto s : substitution_commands) {
spdlog::info("{}: thing", s.first);
}
}
struct done_subtitution_options {
int num;
bool recurse = true;
bool recurse = false;
};
done_subtitution_options
@ -871,7 +880,6 @@ class templater { @@ -871,7 +880,6 @@ class templater {
std::optional<const std::map<fs::path, blog_item>> pages)
{
spdlog::info("invoc: {} at {}", invocation, properties.at("current_file"));
assert(substitution_commands.size() > 0);
std::string command_name = invocation.substr(0, invocation.find(":"));
@ -928,6 +936,7 @@ class templater { @@ -928,6 +936,7 @@ class templater {
}
int search_from = next + TEMPLATE_CODE_START.length();
int loops = 0;
while (next_start < end) {
// we found a nested tag
@ -967,11 +976,15 @@ class templater { @@ -967,11 +976,15 @@ class templater {
end += TEMPLATE_CODE_END.length();
auto subst = do_substitution(next, end, invocation, text, properties, pages);
next += subst.num;
if (!subst.num) {
// unsuccesful
next += TEMPLATE_CODE_START.length();
// spdlog::info("Substitution failed, {} in {}", invocation, properties.at("current_file"));
} else if (true || properties.count("notemplating") || !subst.recurse) {
// next = end;
// break;
// spdlog::info("Substitution failed, {} in {}", nvocation, properties.at("current_file"));
} else if (properties.count("notemplating") || !subst.recurse) {
// do not recurse into substituted content
next += subst.num;
}

2
util.h

@ -17,6 +17,8 @@ namespace fs = std::filesystem; @@ -17,6 +17,8 @@ namespace fs = std::filesystem;
#ifndef UTIL_H
#define UTIL_H
#define STUPID_BREAKPOINT do {*(int *)0 = 1} while (0);
std::string
file_ext(std::string path);

Loading…
Cancel
Save