Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit 09065b6

Browse files
authored
Merge pull request #56 from brycelelbach/callbacks/load
`load`, an Inja callback that loads the JSON data for a refid
2 parents 1f053da + 33a98e6 commit 09065b6

File tree

5 files changed

+50
-35
lines changed

5 files changed

+50
-35
lines changed

include/Doxybook/Generator.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "JsonConverter.hpp"
3+
#include "Doxygen.hpp"
34
#include "Renderer.hpp"
45
#include <string>
56
#include <unordered_set>
@@ -16,15 +17,15 @@ namespace Doxybook2 {
1617
};
1718

1819
explicit Generator(const Config& config,
20+
const Doxygen& doxygen,
1921
const JsonConverter& jsonConverter,
2022
const std::optional<std::string>& templatesPath);
2123

22-
void print(const Doxygen& doxygen, const Filter& filter, const Filter& skip);
23-
void json(const Doxygen& doxygen, const Filter& filter, const Filter& skip);
24-
void manifest(const Doxygen& doxygen);
25-
void printIndex(const Doxygen& doxygen, FolderCategory type, const Filter& filter, const Filter& skip);
26-
void summary(const Doxygen& doxygen,
27-
const std::string& inputFile,
24+
void print(const Filter& filter, const Filter& skip);
25+
void json(const Filter& filter, const Filter& skip);
26+
void manifest();
27+
void printIndex(FolderCategory type, const Filter& filter, const Filter& skip);
28+
void summary(const std::string& inputFile,
2829
const std::string& outputFile,
2930
const std::vector<SummarySection>& sections);
3031

@@ -43,6 +44,7 @@ namespace Doxybook2 {
4344
bool shouldInclude(const Node& node);
4445

4546
const Config& config;
47+
const Doxygen& doxygen;
4648
const JsonConverter& jsonConverter;
4749
Renderer renderer;
4850
};

include/Doxybook/Renderer.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22
#include "Config.hpp"
3+
#include "JsonConverter.hpp"
4+
#include "Doxygen.hpp"
35
#include <memory>
46
#include <nlohmann/json.hpp>
57
#include <optional>
@@ -14,14 +16,16 @@ namespace inja {
1416
namespace Doxybook2 {
1517
class Renderer {
1618
public:
17-
explicit Renderer(const Config& config, const std::optional<std::string>& templatesPath = std::nullopt);
19+
explicit Renderer(const Config& config, const Doxygen& doxygen, const JsonConverter& jsonConverter, const std::optional<std::string>& templatesPath = std::nullopt);
1820
~Renderer();
1921

2022
void render(const std::string& name, const std::string& path, const nlohmann::json& data) const;
2123
std::string render(const std::string& name, const nlohmann::json& data) const;
2224

2325
private:
2426
const Config& config;
27+
const Doxygen& doxygen;
28+
const JsonConverter& jsonConverter;
2529

2630
std::unique_ptr<inja::Environment> env;
2731
std::unordered_map<std::string, std::unique_ptr<inja::Template>> templates;

src/Doxybook/Generator.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <Doxybook/Path.hpp>
66
#include <Doxybook/Renderer.hpp>
77
#include <Doxybook/Utils.hpp>
8+
#include <inja/inja.hpp>
89
#include <filesystem>
910
#include <fstream>
1011

@@ -38,13 +39,14 @@ std::string Doxybook2::Generator::kindToTemplateName(const Kind kind) {
3839
}
3940

4041
Doxybook2::Generator::Generator(const Config& config,
42+
const Doxygen& doxygen,
4143
const JsonConverter& jsonConverter,
4244
const std::optional<std::string>& templatesPath)
43-
: config(config), jsonConverter(jsonConverter), renderer(config, templatesPath) {
45+
: config(config), doxygen(doxygen), jsonConverter(jsonConverter),
46+
renderer(config, doxygen, jsonConverter, templatesPath) {
4447
}
4548

46-
void Doxybook2::Generator::summary(const Doxygen& doxygen,
47-
const std::string& inputFile,
49+
void Doxybook2::Generator::summary(const std::string& inputFile,
4850
const std::string& outputFile,
4951
const std::vector<SummarySection>& sections) {
5052

@@ -163,15 +165,15 @@ void Doxybook2::Generator::jsonRecursively(const Node& parent, const Filter& fil
163165
}
164166
}
165167

166-
void Doxybook2::Generator::print(const Doxygen& doxygen, const Filter& filter, const Filter& skip) {
168+
void Doxybook2::Generator::print(const Filter& filter, const Filter& skip) {
167169
printRecursively(doxygen.getIndex(), filter, skip);
168170
}
169171

170-
void Doxybook2::Generator::json(const Doxygen& doxygen, const Filter& filter, const Filter& skip) {
172+
void Doxybook2::Generator::json(const Filter& filter, const Filter& skip) {
171173
jsonRecursively(doxygen.getIndex(), filter, skip);
172174
}
173175

174-
void Doxybook2::Generator::manifest(const Doxygen& doxygen) {
176+
void Doxybook2::Generator::manifest() {
175177
auto data = manifestRecursively(doxygen.getIndex());
176178
const auto path = Path::join(config.outputDir, "manifest.json");
177179

@@ -207,8 +209,7 @@ nlohmann::json Doxybook2::Generator::manifestRecursively(const Node& node) {
207209
return ret;
208210
}
209211

210-
void Doxybook2::Generator::printIndex(const Doxygen& doxygen,
211-
const FolderCategory type,
212+
void Doxybook2::Generator::printIndex(const FolderCategory type,
212213
const Filter& filter,
213214
const Filter& skip) {
214215
const auto path = typeToIndexName(config, type) + "." + config.fileExt;

src/Doxybook/Renderer.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@ static std::string basename(const std::string& path) {
7575
return str.substr(0, found);
7676
}
7777

78-
Doxybook2::Renderer::Renderer(const Config& config, const std::optional<std::string>& templatesPath)
79-
: config(config), env(std::make_unique<inja::Environment>(
80-
templatesPath.has_value() ? trimPath(*templatesPath) + SEPARATOR : "./")) {
78+
Doxybook2::Renderer::Renderer(const Config& config,
79+
const Doxygen& doxygen,
80+
const JsonConverter& jsonConverter,
81+
const std::optional<std::string>& templatesPath)
82+
: config(config), doxygen(doxygen), jsonConverter(jsonConverter),
83+
env(std::make_unique<inja::Environment>(
84+
templatesPath.has_value() ? trimPath(*templatesPath) + SEPARATOR : "./")) {
8185

8286
env->add_callback("isEmpty", 1, [](inja::Arguments& args) -> bool {
8387
const auto arg = args.at(0)->get<std::string>();
@@ -164,6 +168,10 @@ Doxybook2::Renderer::Renderer(const Config& config, const std::optional<std::str
164168
const auto data = args.at(1)->get<nlohmann::json>();
165169
return this->render(name, data);
166170
});
171+
env->add_callback("load", 1, [&](inja::Arguments& args) -> nlohmann::json {
172+
const auto refid = args.at(0)->get<std::string>();
173+
return jsonConverter.getAsJson(*doxygen.find(refid));
174+
});
167175
env->add_callback("replace", 3, [](inja::Arguments& args) -> nlohmann::json {
168176
auto str = args.at(0)->get<std::string>();
169177
const auto what = args.at(1)->get<std::string>();
@@ -369,3 +377,4 @@ std::string Doxybook2::Renderer::render(const std::string& name, const nlohmann:
369377
}
370378
return ss.str();
371379
}
380+

src/DoxybookCli/main.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ int main(const int argc, char* argv[]) {
147147
templatesPath = args["templates"].as<std::string>();
148148
}
149149

150-
Generator generator(config, jsonConverter, templatesPath);
150+
Generator generator(config, doxygen, jsonConverter, templatesPath);
151151

152152
const auto shouldGenerate = [&](const FolderCategory category) {
153153
return std::find(config.foldersToGenerate.begin(), config.foldersToGenerate.end(), category) !=
@@ -180,11 +180,11 @@ int main(const int argc, char* argv[]) {
180180
Log::i("Rendering...");
181181

182182
if (args["json"]) {
183-
generator.json(doxygen, LANGUAGE_FILTER, {});
184-
generator.json(doxygen, INDEX_FILES_FILTER, {});
185-
generator.json(doxygen, INDEX_PAGES_FILTER, {});
183+
generator.json(LANGUAGE_FILTER, {});
184+
generator.json(INDEX_FILES_FILTER, {});
185+
generator.json(INDEX_PAGES_FILTER, {});
186186

187-
generator.manifest(doxygen);
187+
generator.manifest();
188188
} else {
189189
if (args["summary-input"] && args["summary-output"]) {
190190
std::vector<Generator::SummarySection> sections;
@@ -207,8 +207,7 @@ int main(const int argc, char* argv[]) {
207207
sections.push_back({FolderCategory::EXAMPLES, INDEX_EXAMPLES_FILTER, {}});
208208
}
209209

210-
generator.summary(doxygen,
211-
args["summary-input"].as<std::string>(),
210+
generator.summary(args["summary-input"].as<std::string>(),
212211
args["summary-output"].as<std::string>(),
213212
sections);
214213
}
@@ -227,36 +226,36 @@ int main(const int argc, char* argv[]) {
227226
languageFilder.insert(Kind::MODULE);
228227
}
229228
if (!languageFilder.empty()) {
230-
generator.print(doxygen, languageFilder, {});
229+
generator.print(languageFilder, {});
231230
}
232231

233232
if (shouldGenerate(FolderCategory::FILES)) {
234-
generator.print(doxygen, INDEX_FILES_FILTER, {});
233+
generator.print(INDEX_FILES_FILTER, {});
235234
}
236235
if (shouldGenerate(FolderCategory::PAGES)) {
237-
generator.print(doxygen, INDEX_PAGES_FILTER, {});
236+
generator.print(INDEX_PAGES_FILTER, {});
238237
}
239238
if (shouldGenerate(FolderCategory::EXAMPLES)) {
240-
generator.print(doxygen, INDEX_EXAMPLES_FILTER, {});
239+
generator.print(INDEX_EXAMPLES_FILTER, {});
241240
}
242241

243242
if (shouldGenerate(FolderCategory::CLASSES)) {
244-
generator.printIndex(doxygen, FolderCategory::CLASSES, INDEX_CLASS_FILTER, {});
243+
generator.printIndex(FolderCategory::CLASSES, INDEX_CLASS_FILTER, {});
245244
}
246245
if (shouldGenerate(FolderCategory::NAMESPACES)) {
247-
generator.printIndex(doxygen, FolderCategory::NAMESPACES, INDEX_NAMESPACES_FILTER, {});
246+
generator.printIndex(FolderCategory::NAMESPACES, INDEX_NAMESPACES_FILTER, {});
248247
}
249248
if (shouldGenerate(FolderCategory::MODULES)) {
250-
generator.printIndex(doxygen, FolderCategory::MODULES, INDEX_MODULES_FILTER, {});
249+
generator.printIndex(FolderCategory::MODULES, INDEX_MODULES_FILTER, {});
251250
}
252251
if (shouldGenerate(FolderCategory::FILES)) {
253-
generator.printIndex(doxygen, FolderCategory::FILES, INDEX_FILES_FILTER, {});
252+
generator.printIndex(FolderCategory::FILES, INDEX_FILES_FILTER, {});
254253
}
255254
if (shouldGenerate(FolderCategory::PAGES)) {
256-
generator.printIndex(doxygen, FolderCategory::PAGES, INDEX_PAGES_FILTER, {});
255+
generator.printIndex(FolderCategory::PAGES, INDEX_PAGES_FILTER, {});
257256
}
258257
if (shouldGenerate(FolderCategory::EXAMPLES)) {
259-
generator.printIndex(doxygen, FolderCategory::EXAMPLES, INDEX_EXAMPLES_FILTER, {});
258+
generator.printIndex(FolderCategory::EXAMPLES, INDEX_EXAMPLES_FILTER, {});
260259
}
261260
}
262261
} else {

0 commit comments

Comments
 (0)