mirror of
https://github.com/NixOS/nix.git
synced 2025-11-11 04:56:01 +01:00
The short answer for why we need to do this is so we can consistently do
`#include "nix/..."`. Without this change, there are ways to still make
that work, but they are hacky, and they have downsides such as making it
harder to make sure headers from the wrong Nix library (e..g.
`libnixexpr` headers in `libnixutil`) aren't being used.
The C API alraedy used `nix_api_*`, so its headers are *not* put in
subdirectories accordingly.
Progress on #7876
We resisted doing this for a while because it would be annoying to not
have the header source file pairs close by / easy to change file
path/name from one to the other. But I am ameliorating that with
symlinks in the next commit.
(cherry picked from commit f3e1c47f47)
87 lines
2.3 KiB
C++
87 lines
2.3 KiB
C++
#include "nix/markdown.hh"
|
|
#include "nix/environment-variables.hh"
|
|
#include "nix/error.hh"
|
|
#include "nix/finally.hh"
|
|
#include "nix/terminal.hh"
|
|
|
|
#include "cmd-config-private.hh"
|
|
|
|
#if HAVE_LOWDOWN
|
|
# include <sys/queue.h>
|
|
# include <lowdown.h>
|
|
#endif
|
|
|
|
namespace nix {
|
|
|
|
#if HAVE_LOWDOWN
|
|
static std::string doRenderMarkdownToTerminal(std::string_view markdown)
|
|
{
|
|
int windowWidth = getWindowSize().second;
|
|
|
|
#if HAVE_LOWDOWN_1_4
|
|
struct lowdown_opts_term opts_term {
|
|
.cols = (size_t) std::max(windowWidth - 5, 60),
|
|
.hmargin = 0,
|
|
.vmargin = 0,
|
|
};
|
|
#endif
|
|
struct lowdown_opts opts
|
|
{
|
|
.type = LOWDOWN_TERM,
|
|
#if HAVE_LOWDOWN_1_4
|
|
.term = opts_term,
|
|
#endif
|
|
.maxdepth = 20,
|
|
#if !HAVE_LOWDOWN_1_4
|
|
.cols = (size_t) std::max(windowWidth - 5, 60),
|
|
.hmargin = 0,
|
|
.vmargin = 0,
|
|
#endif
|
|
.feat = LOWDOWN_COMMONMARK | LOWDOWN_FENCED | LOWDOWN_DEFLIST | LOWDOWN_TABLES,
|
|
.oflags = LOWDOWN_TERM_NOLINK,
|
|
};
|
|
|
|
auto doc = lowdown_doc_new(&opts);
|
|
if (!doc)
|
|
throw Error("cannot allocate Markdown document");
|
|
Finally freeDoc([&]() { lowdown_doc_free(doc); });
|
|
|
|
size_t maxn = 0;
|
|
auto node = lowdown_doc_parse(doc, &maxn, markdown.data(), markdown.size(), nullptr);
|
|
if (!node)
|
|
throw Error("cannot parse Markdown document");
|
|
Finally freeNode([&]() { lowdown_node_free(node); });
|
|
|
|
auto renderer = lowdown_term_new(&opts);
|
|
if (!renderer)
|
|
throw Error("cannot allocate Markdown renderer");
|
|
Finally freeRenderer([&]() { lowdown_term_free(renderer); });
|
|
|
|
auto buf = lowdown_buf_new(16384);
|
|
if (!buf)
|
|
throw Error("cannot allocate Markdown output buffer");
|
|
Finally freeBuffer([&]() { lowdown_buf_free(buf); });
|
|
|
|
int rndr_res = lowdown_term_rndr(buf, renderer, node);
|
|
if (!rndr_res)
|
|
throw Error("allocation error while rendering Markdown");
|
|
|
|
return filterANSIEscapes(std::string(buf->data, buf->size), !isTTY());
|
|
}
|
|
|
|
std::string renderMarkdownToTerminal(std::string_view markdown)
|
|
{
|
|
if (auto e = getEnv("_NIX_TEST_RAW_MARKDOWN"); e && *e == "1")
|
|
return std::string(markdown);
|
|
else
|
|
return doRenderMarkdownToTerminal(markdown);
|
|
}
|
|
|
|
#else
|
|
std::string renderMarkdownToTerminal(std::string_view markdown)
|
|
{
|
|
return std::string(markdown);
|
|
}
|
|
#endif
|
|
|
|
} // namespace nix
|