Skip to content

Commit

Permalink
Substitute variable in template filename
Browse files Browse the repository at this point in the history
  • Loading branch information
houmain committed Jul 25, 2024
1 parent d68bfd2 commit 42e5056
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ The following table contains a list of all definitions, with the item each affec
| data | sprite | key, value | Adds a user defined data entry to a sprite. |
| **description** | - | filename | Adds an additional location where the output description should be written. When the _filename_ is a sequence e.g. `"slice{0-}.plist"` then each _slice_ is output to a separate file. |
| template | description | filename | Sets the template which should be used for generating the output description. |
| set | - | key, value | Sets a variable value, which can be accessed in different places using `{{key}}`. See a list of existing [variables](#variables). |
| set | - | key, value | Sets a variable value, which can be accessed in strings and templates using `{{key}}`. See a list of existing [variables](#variables). |
| group | - | - | Can be used for opening a new scope, to limit for example the effect of a tag. |

<a id="definition-reference-end"></a>
Expand Down
30 changes: 30 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,36 @@ void replace_variables(std::string& expression,
}
}

void replace_variables(std::string& expression, const VariantMap& variables) {
const auto replace_variable = [&](std::string_view variable) {
if (auto it = variables.find(variable); it != variables.end())
return variant_to_string(it->second);
error("unknown id '", variable, "'");
};
return replace_variables(expression, replace_variable);
}

void replace_variables(std::filesystem::path& path, const VariantMap& variables) {
auto string = path_to_utf8(path);
replace_variables(string, variables);
path = utf8_to_path(string);
}

std::string variant_to_string(const Variant& variant) {
auto string = std::string();
std::visit([&](const auto& value) {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string>) {
string = value;
}
else {
using namespace std;
string = to_string(value);
}
}, variant);
return string;
}

std::string make_identifier(std::string string) {
for (auto& c : string)
if (!is_alpha(c) && !is_digit(c))
Expand Down
8 changes: 8 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
#include <utility>
#include <optional>
#include <cassert>
#include <variant>
#include <map>

extern Scheduler scheduler;

namespace spright {

using Variant = std::variant<bool, real, std::string>;
using VariantMap = std::map<std::string, Variant, std::less<>>;

struct LStringView : std::string_view {
LStringView(std::string_view s) : std::string_view(s) { }
LStringView(const char* s) : std::string_view(s) { }
Expand Down Expand Up @@ -123,6 +128,9 @@ std::string remove_directory(std::string filename, int keep_n = 0);
bool has_supported_extension(std::string_view filename);
void replace_variables(std::string& expression,
const std::function<std::string(std::string_view)>& replace_function);
void replace_variables(std::string& expression, const VariantMap& variables);
void replace_variables(std::filesystem::path& path, const VariantMap& variables);
std::string variant_to_string(const Variant& variant);
std::string make_identifier(std::string string);

inline int floor(int v, int q) { return (v / q) * q; };
Expand Down
4 changes: 0 additions & 4 deletions src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include "settings.h"
#include "FilenameSequence.h"
#include <memory>
#include <map>
#include <variant>

namespace spright {

Expand All @@ -14,8 +12,6 @@ using SheetPtr = std::shared_ptr<const struct Sheet>;
using OutputPtr = std::shared_ptr<const struct Output>;
using MapVectorPtr = std::shared_ptr<const std::vector<ImagePtr>>;
using StringMap = std::map<std::string, std::string, std::less<>>;
using Variant = std::variant<bool, real, std::string>;
using VariantMap = std::map<std::string, Variant, std::less<>>;

enum class AnchorX { left, center, right };
enum class AnchorY { top, middle, bottom };
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int main(int argc, const char* argv[]) try {
}

if (settings.mode != Mode::autocomplete) {
complete_description_definitions(settings, descriptions);
complete_description_definitions(settings, descriptions, variables);

output_descriptions(settings, descriptions,
inputs, sprites, slices, textures, variables);
Expand Down
3 changes: 2 additions & 1 deletion src/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ void evaluate_expressions(const Settings& settings,

void complete_description_definitions(
const Settings& settings,
std::vector<Description>& descriptions);
std::vector<Description>& descriptions,
const VariantMap& variables);

std::string dump_description(
const std::string& template_source,
Expand Down
25 changes: 8 additions & 17 deletions src/output_description.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,6 @@ namespace {
return env;
}

std::string variant_to_string(const Variant& variant) {
auto string = std::string();
std::visit([&](const auto& value) {
using T = std::decay_t<decltype(value)>;
if constexpr (std::is_same_v<T, std::string>) {
string = value;
}
else {
using namespace std;
string = to_string(value);
}
}, variant);
return string;
}

void output_description(std::ostream& os,
const std::filesystem::path& template_filename,
const nlohmann::json& json) {
Expand Down Expand Up @@ -361,8 +346,10 @@ void evaluate_expressions(
}
}

void complete_description_definitions(const Settings& settings,
std::vector<Description>& descriptions) {
void complete_description_definitions(const Settings& settings,
std::vector<Description>& descriptions,
const VariantMap& variables) {

// ignore description in definition
if (settings.mode == Mode::describe ||
settings.mode == Mode::describe_input)
Expand All @@ -382,6 +369,10 @@ void complete_description_definitions(const Settings& settings,
if (description.filename.string() != "stdout")
description.filename = settings.output_path / description.filename;

// replace variables
for (auto& description : descriptions)
replace_variables(description.template_filename, variables);

// also lookup templates in sub-directory
for (auto& description : descriptions) {
auto& template_filename = description.template_filename;
Expand Down

0 comments on commit 42e5056

Please sign in to comment.