Skip to content

Commit

Permalink
refactor(HandlebarsGenerator): remove unused options
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Nov 14, 2024
1 parent 5209438 commit 5f9e5a1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 113 deletions.
6 changes: 6 additions & 0 deletions docs/mrdocs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@
"title": "Include files to extract",
"type": "object"
},
"legible-names": {
"default": true,
"description": "Use legible names for ids in the documentation. When set to true, MrDocs uses legible names for symbols in the documentation. These are symbols that are legible but still safe for URLs. When the option is set to false, MrDocs uses a hash of the symbol ID.",
"title": "Use legible names",
"type": "boolean"
},
"multipage": {
"default": true,
"description": "Generates a multipage documentation. The output directory must be a directory. This option acts as a hint to the generator to create a multipage documentation. Whether the hint is followed or not depends on the generator.",
Expand Down
6 changes: 5 additions & 1 deletion src/lib/Gen/hbs/Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ std::string
Builder::
getRelPrefix(std::size_t depth)
{
Config const& config = domCorpus->config;

std::string rel_prefix;
if(! depth || ! domCorpus.options.legible_names ||
if(! depth || ! config->legibleNames ||
! domCorpus->config->multipage)
return rel_prefix;
--depth;
Expand Down Expand Up @@ -208,6 +210,7 @@ createContext(
const Info& Parent = domCorpus->get(OS.Parent);
props.emplace_back("relfileprefix",
getRelPrefix(Parent.Namespace.size() + 1));
props.emplace_back("config", domCorpus->config.object());
props.emplace_back("sectionref",
domCorpus.names_.getQualified(OS, '-'));
return dom::Object(std::move(props));
Expand All @@ -232,6 +235,7 @@ operator()(OverloadSet const& OS)
createContext(OS));
}

// Define Builder::operator() for each Info type
#define DEFINE(T) template Expected<std::string> \
Builder::operator()<T>(T const&)

Expand Down
45 changes: 28 additions & 17 deletions src/lib/Gen/hbs/Builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#ifndef MRDOCS_LIB_GEN_HBS_BUILDER_HPP
#define MRDOCS_LIB_GEN_HBS_BUILDER_HPP

#include "Options.hpp"
#include "HandlebarsCorpus.hpp"
#include "lib/Support/Radix.hpp"
#include <mrdocs/Metadata/DomCorpus.hpp>
Expand All @@ -25,7 +24,7 @@ namespace clang {
namespace mrdocs {
namespace hbs {

/** Builds reference output.
/** Builds reference output as a string for any Info type
This contains all the state information
for a single thread to generate output.
Expand All @@ -35,24 +34,25 @@ class Builder
js::Context ctx_;
Handlebars hbs_;

std::string getRelPrefix(std::size_t depth);
std::string
getRelPrefix(std::size_t depth);

public:
HandlebarsCorpus const& domCorpus;

explicit
Builder(
HandlebarsCorpus const& corpus);
Builder(HandlebarsCorpus const& corpus);

dom::Value createContext(Info const& I);
dom::Value createContext(OverloadSet const& OS);
/** Render the contents for a symbol.
*/
template<class T>
Expected<std::string>
operator()(T const&);

/** Render a Handlebars template from the templates directory.
/** Render the contents for an overload set.
*/
Expected<std::string>
callTemplate(
std::string_view name,
dom::Value const& context);
operator()(OverloadSet const&);

/** Render the header for a single page.
*/
Expand All @@ -64,16 +64,27 @@ class Builder
Expected<std::string>
renderSinglePageFooter();

/** Render the contents for a symbol.
private:
/** Create a handlebars context with the symbol and helper information.
The helper information includes all information from the
config file, plus the symbol information.
It also includes a sectionref helper that describes
the section where the symbol is located.
*/
template<class T>
Expected<std::string>
operator()(T const&);
dom::Value createContext(Info const& I);

/** Render the contents for an overload set.
/// @copydoc createContext(Info const&)
dom::Value createContext(OverloadSet const& OS);

/** Render a Handlebars template from the templates directory.
*/
Expected<std::string>
operator()(OverloadSet const&);
callTemplate(
std::string_view name,
dom::Value const& context);

};

} // hbs
Expand Down
11 changes: 3 additions & 8 deletions src/lib/Gen/hbs/HandlebarsCorpus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include <mrdocs/Platform.hpp>
#include "lib/Support/LegibleNames.hpp"
#include "Options.hpp"
#include <mrdocs/Metadata/DomCorpus.hpp>
#include <optional>

Expand All @@ -32,9 +31,6 @@ namespace hbs {
class HandlebarsCorpus : public DomCorpus
{
public:
/** Options for the Handlebars corpus. */
Options options;

/** Legible names for the Handlebars corpus. */
LegibleNames names_;

Expand All @@ -49,16 +45,15 @@ class HandlebarsCorpus : public DomCorpus
Initializes the HandlebarsCorpus with the given corpus and options.
@param corpus The base corpus.
@param opts Options for the Handlebars corpus.
@param fileExtension The file extension for the generated files.
@param toStringFn The function to convert a Javadoc node to a string.
*/
HandlebarsCorpus(
Corpus const& corpus,
Options&& opts,
std::string_view fileExtension,
std::function<std::string(HandlebarsCorpus const&, doc::Node const&)> toStringFn)
: DomCorpus(corpus)
, options(std::move(opts))
, names_(corpus, options.legible_names)
, names_(corpus, corpus.config->legibleNames)
, fileExtension(fileExtension)
, toStringFn(std::move(toStringFn))
{
Expand Down
33 changes: 0 additions & 33 deletions src/lib/Gen/hbs/HandlebarsGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "HandlebarsGenerator.hpp"
#include "HandlebarsCorpus.hpp"
#include "Builder.hpp"
#include "Options.hpp"
#include "MultiPageVisitor.hpp"
#include "SinglePageVisitor.hpp"
#include <mrdocs/Support/Path.hpp>
Expand Down Expand Up @@ -43,45 +42,13 @@ createExecutors(
return group;
}

/** Return loaded Options from a configuration.
*/
Options
loadOptions(
std::string_view fileExtension,
Config const& config)
{
Options opt;
dom::Value domOpts = config.object().get("generator").get(fileExtension);
if (domOpts.get("legible-names").isBoolean())
{
opt.legible_names = domOpts.get("legible-names").getBool();
}
if (domOpts.get("template-dir").isString())
{
opt.template_dir = domOpts.get("template-dir").getString();
opt.template_dir = files::makeAbsolute(
opt.template_dir,
config->configDir);
}
else
{
opt.template_dir = files::appendPath(
config->addons,
"generator",
fileExtension);
}
return opt;
}

HandlebarsCorpus
createDomCorpus(
HandlebarsGenerator const& gen,
Corpus const& corpus)
{
auto options = loadOptions(gen.fileExtension(), corpus.config);
return {
corpus,
std::move(options),
gen.fileExtension(),
[&gen](HandlebarsCorpus const& c, doc::Node const& n) {
return gen.toString(c, n);
Expand Down
1 change: 0 additions & 1 deletion src/lib/Gen/hbs/HandlebarsGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <mrdocs/Platform.hpp>
#include <mrdocs/Generator.hpp>
#include <mrdocs/Metadata/DomCorpus.hpp>
#include <lib/Gen/hbs/Options.hpp>
#include <lib/Gen/hbs/HandlebarsCorpus.hpp>
#include <utility>

Expand Down
53 changes: 0 additions & 53 deletions src/lib/Gen/hbs/Options.hpp

This file was deleted.

7 changes: 7 additions & 0 deletions src/lib/Lib/ConfigOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@
"type": "path",
"default": "<mrdocs-root>/share/mrdocs/addons",
"relativeto": "<config-dir>"
},
{
"name": "legible-names",
"brief": "Use legible names",
"details": "Use legible names for ids in the documentation. When set to true, MrDocs uses legible names for symbols in the documentation. These are symbols that are legible but still safe for URLs. When the option is set to false, MrDocs uses a hash of the symbol ID.",
"type": "bool",
"default": true
}
]
},
Expand Down

0 comments on commit 5f9e5a1

Please sign in to comment.