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:
2026-08-16 16:58:50 +02:00
parent 8b38a34841
commit bd39d9a369
8 changed files with 284 additions and 47 deletions

View File

@@ -322,9 +322,46 @@ void Machine::mark_literal_klammer_content(katom_list& katoms)
if (literal_names.empty()) return;
// Scan for matching @name ... name@ spans.
// Stop at ## (ignore-rest) since everything after it will be removed.
//
// REMOVED TEXT IS SKIPPED. This pass runs first in process_katoms(), before
// mark_ignored_katoms() takes out the "#" forms, and that ordering is not
// accidental: a literal klammer's content must be marked before anything
// else can interpret what is inside it, or removal would take a "#" that
// belongs to the literal body. The cost is that this scan sees text the
// writer has removed, so a literal klammer merely NAMED in a comment --
//
// # @image and @code share the same arguments.
//
// -- was found as an opening delimiter, went unclosed, and failed the whole
// file (TODO #40, diagnosed 2026-08-05). "##" was already handled, which is
// half the case; the fix is to teach the scan the other three removal forms
// rather than to reorder the passes.
//
// The converse still holds, structurally: this skipping happens only while
// looking for an OPENING delimiter, and finding one jumps k past the whole
// span, so a "#" inside literal content is never examined here and stays
// content.
for (auto k = katoms.begin(); k != katoms.end(); ++k) {
if (k->m_type == katom_t::ignore_rest) break;
if (k->m_type == katom_t::ignore_rest) break; // "##": rest of the file
if (k->m_type == katom_t::ignore_line) { // "#": rest of the line
while (k + 1 != katoms.end() && (k + 1)->m_type != katom_t::newline) {
++k;
}
continue;
}
if (k->m_type == katom_t::ignore_begin) { // "#[ ... ]#", nestable
int removed = 1;
while (++k != katoms.end() && removed > 0) {
if (k->m_type == katom_t::ignore_begin) {
++removed;
} else if (k->m_type == katom_t::ignore_end) {
--removed;
}
}
if (k == katoms.end()) break; // unterminated: the rest is removed
--k; // the loop's ++k steps past the "]#"
continue;
}
if (k->m_type != katom_t::apply_begin) continue;
std::string name = trim_char(k->m_text, '@');
if (literal_names.count(name) == 0) continue;

View File

@@ -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";
}
}