Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support partials in subdirectories #491

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/mrdocs/Support/Path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ struct MRDOCS_VISIBLE
This will iterate all the regular files in
a directory and invoke the visitor with the
path.

@param recursive If true, files in subdirectories are
also visited, recursively.
*/
MRDOCS_DECL
Error
forEachFile(
std::string_view dirPath,
bool recursive,
AnyFileVisitor& visitor);

/** Visit each file in a directory.
Expand All @@ -46,6 +50,7 @@ template<class Visitor>
Error
forEachFile(
std::string_view dirPath,
bool recursive,
Visitor&& visitor)
{
struct FileVisitor : AnyFileVisitor
Expand All @@ -65,7 +70,8 @@ forEachFile(
};

FileVisitor v{visitor};
return forEachFile(dirPath, static_cast<AnyFileVisitor&>(v));
return forEachFile(dirPath, recursive,
static_cast<AnyFileVisitor&>(v));
}

//------------------------------------------------
Expand Down
28 changes: 14 additions & 14 deletions src/lib/-HTML/Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/Path.h>
#include <fmt/format.h>
#include <filesystem>

namespace clang {
namespace mrdocs {
Expand All @@ -33,38 +34,37 @@ Builder(
, corpus_(domCorpus_.getCorpus())
, options_(options)
{
namespace fs = llvm::sys::fs;
namespace path = llvm::sys::path;
namespace fs = std::filesystem;

Config const& config = corpus_.config;

// load partials
std::string partialsPath = files::appendPath(
config->addonsDir, "generator", "html", "partials");
forEachFile(partialsPath,
forEachFile(partialsPath, true,
[&](std::string_view pathName) -> Error
{
constexpr std::string_view ext = ".html.hbs";
if (!pathName.ends_with(ext))
{
fs::path path = pathName;
if(path.extension() != ".hbs")
return Error::success();
}
auto name = files::getFileName(pathName);
name.remove_suffix(ext.size());
path = path.lexically_relative(partialsPath);
while(path.has_extension())
path.replace_extension();

auto text = files::getFileText(pathName);
if (!text)
{
if (! text)
return text.error();
}
hbs_.registerPartial(name, *text);

hbs_.registerPartial(
path.generic_string(), *text);
return Error::success();
}).maybeThrow();

// load helpers
js::Scope scope(ctx_);
std::string helpersPath = files::appendPath(
config->addonsDir, "generator", "asciidoc", "helpers");
forEachFile(helpersPath,
forEachFile(helpersPath, true,
[&](std::string_view pathName)
{
// Register JS helper function in the global object
Expand Down
28 changes: 14 additions & 14 deletions src/lib/-adoc/Builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/Path.h>
#include <fmt/format.h>
#include <filesystem>

namespace clang {
namespace mrdocs {
Expand All @@ -30,38 +31,37 @@ Builder(
AdocCorpus const& corpus)
: domCorpus(corpus)
{
namespace fs = llvm::sys::fs;
namespace path = llvm::sys::path;
namespace fs = std::filesystem;

Config const& config = domCorpus->config;

// load partials
std::string partialsPath = files::appendPath(
config->addonsDir, "generator", "asciidoc", "partials");
forEachFile(partialsPath,
forEachFile(partialsPath, true,
[&](std::string_view pathName) -> Error
{
constexpr std::string_view ext = ".adoc.hbs";
if (!pathName.ends_with(ext))
{
fs::path path = pathName;
if(path.extension() != ".hbs")
return Error::success();
}
auto name = files::getFileName(pathName);
name.remove_suffix(ext.size());
path = path.lexically_relative(partialsPath);
while(path.has_extension())
path.replace_extension();

auto text = files::getFileText(pathName);
if (!text)
{
if (! text)
return text.error();
}
hbs_.registerPartial(name, *text);

hbs_.registerPartial(
path.generic_string(), *text);
return Error::success();
}).maybeThrow();

// load helpers
js::Scope scope(ctx_);
std::string helpersPath = files::appendPath(
config->addonsDir, "generator", "asciidoc", "helpers");
forEachFile(helpersPath,
forEachFile(helpersPath, true,
[&](std::string_view pathName)
{
// Register JS helper function in the global object
Expand Down
8 changes: 8 additions & 0 deletions src/lib/Support/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ convert_to_slash(
Error
forEachFile(
std::string_view dirPath,
bool recursive,
AnyFileVisitor& visitor)
{
namespace fs = llvm::sys::fs;
Expand All @@ -56,6 +57,13 @@ forEachFile(
auto err = visitor.visitFile(s);
if(err)
return err;

if(recursive)
{
if(auto err = forEachFile(
it->path(), recursive, visitor))
return err;
}
}
else if(it->type() == fs::file_type::regular_file)
{
Expand Down
Loading