Skip to content

Commit 186db21

Browse files
committed
feat: template context includes config
1 parent 3e1c279 commit 186db21

File tree

4 files changed

+172
-57
lines changed

4 files changed

+172
-57
lines changed

include/mrdocs/Config.hpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class ThreadPool;
3434
A configuration is always connected to a
3535
particular directory from which absolute paths
3636
are calculated from relative paths.
37+
38+
The Config class is an abstract interface
39+
whose concrete implementation includes
40+
.
3741
*/
3842
class MRDOCS_DECL
3943
Config
@@ -42,7 +46,8 @@ class MRDOCS_DECL
4246
Config() noexcept;
4347

4448
public:
45-
49+
/** Settings values used to generate the Corpus and Docs
50+
*/
4651
struct Settings
4752
{
4853
/** Selected documentation generator.
@@ -115,13 +120,16 @@ class MRDOCS_DECL
115120
ThreadPool&
116121
threadPool() const noexcept = 0;
117122

123+
/** Return the settings used to generate the Corpus and Docs.
124+
*/
125+
virtual Settings const& settings() const noexcept = 0;
126+
127+
/// @copydoc settings()
118128
constexpr Settings const*
119129
operator->() const noexcept
120130
{
121131
return &settings();
122132
}
123-
124-
virtual Settings const& settings() const noexcept = 0;
125133
};
126134

127135
} // mrdocs

src/lib/Gen/adoc/Builder.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "Builder.hpp"
1212
#include "lib/Support/Radix.hpp"
13+
#include <lib/Lib/ConfigImpl.hpp>
1314
#include <mrdocs/Metadata/DomMetadata.hpp>
1415
#include <mrdocs/Support/Path.hpp>
1516
#include <llvm/Support/FileSystem.h>
@@ -178,6 +179,101 @@ getRelPrefix(std::size_t depth)
178179
return rel_prefix;
179180
}
180181

182+
class ConfigObjectImpl : public dom::ObjectImpl
183+
{
184+
Config const* config_;
185+
186+
public:
187+
~ConfigObjectImpl() override = default;
188+
189+
ConfigObjectImpl(Config const& config)
190+
: config_(&config)
191+
{}
192+
193+
char const* type_key() const noexcept override
194+
{
195+
return "ConfigObject";
196+
}
197+
198+
dom::Value get(std::string_view key) const override
199+
{
200+
if (key == "multiPage") return (*config_)->multiPage;
201+
if (key == "generate") return (*config_)->generate;
202+
if (key == "workingDir") return (*config_)->workingDir;
203+
auto* config_impl = dynamic_cast<ConfigImpl const*>(config_);
204+
if (config_impl)
205+
{
206+
if (key == "inaccessibleBases") return (*config_impl)->inaccessibleBases;
207+
if (key == "inaccessibleMembers") return (*config_impl)->inaccessibleMembers;
208+
if (key == "anonymousNamespaces") return (*config_impl)->anonymousNamespaces;
209+
if (key == "ignoreFailures") return (*config_impl)->ignoreFailures;
210+
if (key == "defines") {
211+
dom::Array defines;
212+
for (auto& define: (*config_impl)->defines)
213+
{
214+
defines.emplace_back(define);
215+
}
216+
return defines;
217+
}
218+
}
219+
return {};
220+
}
221+
222+
void set(dom::String key, dom::Value value) override
223+
{
224+
// Cannot set values in the config object from templates
225+
}
226+
227+
bool
228+
visit(std::function<bool(dom::String, dom::Value)> fn) const override
229+
{
230+
if (!fn("multiPage", (*config_)->multiPage)) { return false; };
231+
if (!fn("generate", (*config_)->generate)) { return false; };
232+
if (!fn("workingDir", (*config_)->workingDir)) { return false; };
233+
auto* config_impl = dynamic_cast<ConfigImpl const*>(config_);
234+
if (config_impl)
235+
{
236+
if (!fn("inaccessibleBases", (*config_impl)->inaccessibleBases)) { return false; };
237+
if (!fn("inaccessibleMembers", (*config_impl)->inaccessibleMembers)) { return false; };
238+
if (!fn("anonymousNamespaces", (*config_impl)->anonymousNamespaces)) { return false; };
239+
if (!fn("ignoreFailures", (*config_impl)->ignoreFailures)) { return false; };
240+
dom::Array defines;
241+
for (auto& define: (*config_impl)->defines)
242+
{
243+
defines.emplace_back(define);
244+
}
245+
if (!fn("defines", defines)) { return false; };
246+
}
247+
return true;
248+
}
249+
250+
/** Return the number of properties in the object.
251+
*/
252+
std::size_t size() const override {
253+
return 8;
254+
};
255+
256+
/** Determine if a key exists.
257+
*/
258+
bool exists(std::string_view key) const override
259+
{
260+
if (key == "multiPage") return true;
261+
if (key == "generate") return true;
262+
if (key == "workingDir") return true;
263+
auto* config_impl = dynamic_cast<ConfigImpl const*>(config_);
264+
if (config_impl)
265+
{
266+
if (key == "inaccessibleBases") return true;
267+
if (key == "inaccessibleMembers") return true;
268+
if (key == "anonymousNamespaces") return true;
269+
if (key == "ignoreFailures") return true;
270+
if (key == "defines") return true;
271+
}
272+
return false;
273+
}
274+
};
275+
276+
181277
dom::Value
182278
Builder::
183279
createContext(
@@ -188,6 +284,8 @@ createContext(
188284
domCorpus.get(I.id));
189285
props.emplace_back("relfileprefix",
190286
getRelPrefix(I.Namespace.size()));
287+
props.emplace_back("config",
288+
dom::newObject<ConfigObjectImpl>(domCorpus->config));
191289
return dom::Object(std::move(props));
192290
}
193291

src/lib/Lib/ConfigImpl.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,11 @@ ConfigImpl(
156156
namespace fs = llvm::sys::fs;
157157
namespace path = llvm::sys::path;
158158

159-
if(! files::isAbsolute(workingDir))
159+
// Check working dir
160+
if (!files::isAbsolute(workingDir))
161+
{
160162
formatError("working path \"{}\" is not absolute", workingDir).Throw();
163+
}
161164
settings_.workingDir = files::makeDirsy(files::normalizePath(workingDir));
162165

163166
// Addons directory
@@ -167,14 +170,17 @@ ConfigImpl(
167170
MRDOCS_ASSERT(files::isDirsy(settings_.addonsDir));
168171
}
169172

173+
// Config strings
170174
settings_.configYaml = configYaml;
171175
settings_.extraYaml = extraYaml;
172176

173177
// Parse the YAML strings
174178
YamlReporter reporter;
175179
{
176-
llvm::yaml::Input yin(settings_.configYaml,
177-
&reporter, reporter);
180+
llvm::yaml::Input yin(
181+
settings_.configYaml,
182+
&reporter,
183+
reporter);
178184
yin.setAllowUnknownKeys(true);
179185
yin >> settings_;
180186
Error(yin.error()).maybeThrow();
@@ -187,20 +193,26 @@ ConfigImpl(
187193
Error(yin.error()).maybeThrow();
188194
}
189195

190-
// This has to be forward slash style
196+
// Source root has to be forward slash style
191197
settings_.sourceRoot = files::makePosixStyle(files::makeDirsy(
192198
files::makeAbsolute(settings_.sourceRoot, settings_.workingDir)));
193199

194-
// adjust input files
200+
// Adjust input files
195201
for(auto& name : inputFileIncludes_)
202+
{
196203
name = files::makePosixStyle(
197204
files::makeAbsolute(name, settings_.workingDir));
205+
}
198206

199-
// Parse the symbol filters
207+
// Parse the filters
200208
for(std::string_view pattern : settings_.filters.symbols.exclude)
209+
{
201210
parseSymbolFilter(settings_.symbolFilter, pattern, true);
211+
}
202212
for(std::string_view pattern : settings_.filters.symbols.include)
213+
{
203214
parseSymbolFilter(settings_.symbolFilter, pattern, false);
215+
}
204216
settings_.symbolFilter.finalize(false, false, false);
205217
}
206218

@@ -277,16 +289,16 @@ loadConfig(
277289
namespace fs = llvm::sys::fs;
278290
namespace path = llvm::sys::path;
279291

280-
auto temp = files::normalizePath(filePath);
292+
std::string normFilePath = files::normalizePath(filePath);
281293

282-
// load the config file into a string
283-
MRDOCS_TRY(auto absPath, files::makeAbsolute(temp));
284-
MRDOCS_TRY(auto configYaml, files::getFileText(absPath));
294+
// Load the config file into a string
295+
MRDOCS_TRY(auto absConfigPath, files::makeAbsolute(normFilePath));
296+
MRDOCS_TRY(auto configYaml, files::getFileText(absConfigPath));
285297

286-
// calculate the working directory
287-
auto workingDir = files::getParentDir(absPath);
298+
// Calculate the working directory
299+
auto workingDir = files::getParentDir(absConfigPath);
288300

289-
// attempt to create the config
301+
// Attempt to create the config
290302
try
291303
{
292304
return std::make_shared<ConfigImpl>(

src/lib/Lib/ConfigImpl.hpp

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,67 +31,38 @@ class ConfigImpl
3131
public:
3232
struct SettingsImpl : Settings
3333
{
34-
struct FileFilter
35-
{
36-
std::vector<std::string> include;
37-
};
34+
/** Extraction policy for declarations.
3835
39-
struct Filters
40-
{
41-
struct Category
42-
{
43-
std::vector<std::string> include;
44-
std::vector<std::string> exclude;
45-
};
46-
47-
Category symbols;
48-
};
36+
This determines how declarations are extracted.
4937
38+
*/
5039
enum class ExtractPolicy
5140
{
41+
/// Always extract the declaration.
5242
Always,
43+
/// Extract the declaration if it is referenced.
5344
Dependency,
45+
/// Never extract the declaration.
5446
Never
5547
};
5648

5749
/** Extraction policy for references to external declarations.
5850
5951
This determines how declarations which are referenced by
6052
explicitly extracted declarations are extracted.
61-
62-
Given a function parameter of type `std::string`, `std::string`
63-
would be extracted if this option is set to `Policy::Always`.
64-
*/
53+
*/
6554
ExtractPolicy referencedDeclarations = ExtractPolicy::Dependency;
6655

6756
/** Extraction policy for anonymous namespace.
68-
69-
@li `ExtractPolicy::Always`: anonymous namespaces and their
70-
members will always be extracted.
71-
72-
@li `ExtractPolicy::Dependency`: members of anonymous namespaces will only
73-
be extracted via dependency.
74-
75-
@li `ExtractPolicy::Never`: members of anonymous namespace will
76-
never be extracted, regardless of how they are referenced.
77-
*/
57+
*/
7858
ExtractPolicy anonymousNamespaces = ExtractPolicy::Always;
7959

8060
/** Extraction policy for inaccessible members.
81-
82-
@li `ExtractPolicy::Always`: all `private` and `protected` members
83-
will be extracted.
84-
85-
@li `ExtractPolicy::Dependency`: `private` and `protected` members will only
86-
be extracted via dependency.
87-
88-
@li `ExtractPolicy::Never`: `private` and `protected` will never be extracted.
89-
*/
61+
*/
9062
ExtractPolicy inaccessibleMembers = ExtractPolicy::Always;
9163

9264
ExtractPolicy inaccessibleBases = ExtractPolicy::Always;
9365

94-
9566
/** Additional defines passed to the compiler.
9667
*/
9768
std::vector<std::string> defines;
@@ -111,8 +82,34 @@ class ConfigImpl
11182
*/
11283
std::string sourceRoot;
11384

85+
/** Specifies files that should be filtered
86+
*/
87+
struct FileFilter
88+
{
89+
/// Directories to include
90+
std::vector<std::string> include;
91+
};
92+
93+
/// @copydoc FileFilter
11494
FileFilter input;
11595

96+
/** Specifies filters for various kinds of symbols.
97+
*/
98+
struct Filters
99+
{
100+
/** Specifies inclusion and exclusion patterns
101+
*/
102+
struct Category
103+
{
104+
std::vector<std::string> include;
105+
std::vector<std::string> exclude;
106+
};
107+
108+
/// Specifies filter patterns for symbols
109+
Category symbols;
110+
};
111+
112+
/// @copydoc Filters
116113
Filters filters;
117114

118115
/** Symbol filter root node.
@@ -125,20 +122,20 @@ class ConfigImpl
125122

126123
};
127124

125+
/// @copydoc Config::settings()
128126
Settings const&
129-
settings()const noexcept override
127+
settings() const noexcept override
130128
{
131129
return settings_;
132130
}
133131

132+
/// @copydoc Config::settings()
134133
constexpr SettingsImpl const*
135134
operator->() const noexcept
136135
{
137136
return &settings_;
138137
}
139138

140-
//--------------------------------------------
141-
142139
private:
143140
SettingsImpl settings_;
144141
ThreadPool& threadPool_;

0 commit comments

Comments
 (0)