Literal @c, @source_listing with :marker, and a large-directory speedup
Three changes. @c now takes its content literally, like @code -- it is the inline form and @code the block form of the same thing. The named close "c@" is required, and characters that are special in a target no longer break the file: @c a_b c@ renders correctly everywhere. The Markdown converter stops quoting inline code, since nothing needs protecting. @source_file is renamed @source_listing. Code read from a file is its own klammer; @code is only for a block written inline (its never- implemented :filename and :pattern options are removed). The new :marker P option lists the region between two lines that are exactly //P, so the source file declares its own extractable regions. A marker missing or not appearing exactly twice is an error, never a fallback. Rendering a document that sits in a large directory was paying a recursive walk of that directory's whole tree on every @eval -- 27 seconds for a document that renders in a third of one. The walk is now a non-recursive look decided once per directory. Assembled from dev commit 071b1b183de4. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include <regex>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
#include "show.h"
|
||||
#include "file.h"
|
||||
@@ -10,6 +11,35 @@
|
||||
|
||||
int State::class_id = 0;
|
||||
|
||||
namespace {
|
||||
|
||||
// A sys.path entry reaches only the directory's own files, so the decision
|
||||
// must not recurse; and the answer is fixed for the run (the search-dir set
|
||||
// only grows, and a directory's answer does not change), so it is decided
|
||||
// once per directory per process. The former recursive walk here ran on
|
||||
// every @eval and took ~200 ms per eval on a document in a large home
|
||||
// directory (27 s for one render).
|
||||
bool directory_has_python_file(const std::string& dir)
|
||||
{
|
||||
static std::map<std::string, bool> cache {};
|
||||
auto it = cache.find(dir);
|
||||
if (it != cache.end()) {
|
||||
return it->second;
|
||||
}
|
||||
bool found = false;
|
||||
std::error_code ec {};
|
||||
for (const auto& entry : std::filesystem::directory_iterator(dir, ec)) {
|
||||
if (entry.is_regular_file(ec) && entry.path().extension() == ".py") {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
cache[dir] = found;
|
||||
return found;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool Var::defined() const
|
||||
{
|
||||
return !m_name.empty();
|
||||
@@ -297,8 +327,7 @@ std::string State::python_code()
|
||||
python_dirs.insert(python_dirs.end(),
|
||||
m_search_dirs.begin(), m_search_dirs.end());
|
||||
for (const auto& d : python_dirs) {
|
||||
auto python_files = pathnames_with_extension(d, "py");
|
||||
if (!python_files.empty()) {
|
||||
if (directory_has_python_file(d)) {
|
||||
ss << "sys.path.append('" << d << "')\n";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user