Escaping, table layout, and document fixes
Quoted Klammertext specials (^@ ^| ^# ^^ ^: ^*) and ^'...'^ regions now survive re-processing (held as escape markers until final output); :after_apply phase functions receive and return raw target text. Tables: :hpos element position (center|left|right|<length>) replaces the unimplemented :center/:indent; the ranged cell override is renamed :justify; :column_width works in html (colgroup widths) and gains 'fill' -- the remaining width, capped at the column's widest entry, in both targets; a table wider than the text column warns on the console; table edges without an outer line set their text flush on the margins. @document: no empty title bar for untitled documents; @vfill fills to the bottom of the window in html (pure CSS); @vspace in plain text; new @dot klammer; monospace email links.
This commit is contained in:
@@ -114,7 +114,7 @@ void check_cpp_arguments(katom_list args, Locator loc)
|
||||
}
|
||||
|
||||
|
||||
katom_list Eval::eval(katom_iter begin, katom_iter end)
|
||||
std::string Eval::eval_command(katom_iter begin, katom_iter end)
|
||||
{
|
||||
(void)K::log(3, *begin, *(end - 1));
|
||||
//msg() << "in Eval::eval:\n" << ktype << kall << kindex << std::pair(begin, end) << "\n";
|
||||
@@ -150,6 +150,12 @@ katom_list Eval::eval(katom_iter begin, katom_iter end)
|
||||
Eval_cpp E_cpp(m_machine, begin->m_loc);
|
||||
eval_result = E_cpp.eval(libpath, funcname);
|
||||
}
|
||||
return eval_result;
|
||||
}
|
||||
|
||||
katom_list Eval::eval(katom_iter begin, katom_iter end)
|
||||
{
|
||||
std::string eval_result = eval_command(begin, end);
|
||||
katom_list result {};
|
||||
Machine M = m_machine;
|
||||
size_t before = M.m_katoms.size();
|
||||
|
||||
@@ -25,6 +25,14 @@ public:
|
||||
std::vector<Katom> eval(
|
||||
std::vector<Katom>::iterator begin, std::vector<Katom>::iterator end);
|
||||
|
||||
// Dispatch the @eval command (python/shell/haskell/cpp) and return its
|
||||
// raw string result, without re-reading it as Klammertext. Used by
|
||||
// :after_apply phase functions, whose input and output are final target
|
||||
// text -- re-katomizing it would misparse target characters (a "@" in
|
||||
// justified txt output) as Klammertext syntax.
|
||||
std::string eval_command(
|
||||
std::vector<Katom>::iterator begin, std::vector<Katom>::iterator end);
|
||||
|
||||
Machine m_machine;
|
||||
Locator m_loc;
|
||||
};
|
||||
|
||||
@@ -29,6 +29,16 @@ Eval_python::Eval_python(Machine& machine, Locator loc)
|
||||
m_globals = PyDict_New();
|
||||
m_locals = PyDict_New();
|
||||
PyDict_SetItemString(m_globals, "__builtins__", PyEval_GetBuiltins());
|
||||
// The machine's result text, so :after_apply phase functions can take it
|
||||
// as an argument (the Python counterpart of a :cpp phase function reading
|
||||
// machine.m_result). Set directly rather than through the state's
|
||||
// python_code() because document text cannot be safely embedded in a
|
||||
// quoted Python source string.
|
||||
PyObject* result_text = PyUnicode_FromString(m_machine.m_result.c_str());
|
||||
if (result_text) {
|
||||
PyDict_SetItemString(m_globals, "K_result", result_text);
|
||||
Py_DECREF(result_text);
|
||||
}
|
||||
import_module("inspect", false);
|
||||
if (!m_machine.m_state.m_frames.empty()) {
|
||||
PyRun_String(m_machine.m_state.python_code().c_str(), Py_file_input, m_globals, m_locals);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "log.h"
|
||||
#include "show.h"
|
||||
#include "character.h"
|
||||
#include "target.h"
|
||||
|
||||
std::string to_string(std::vector<Katom>::const_iterator begin, std::vector<Katom>::const_iterator end, bool trim_result)
|
||||
{
|
||||
@@ -299,7 +300,75 @@ void mark_literal_katoms(katom_list& katoms)
|
||||
//end->m_type = katom_t::replaced;
|
||||
mark_as_replaced(*(end - 1));
|
||||
//std::for_each(begin + 1, end, [](Katom& k) { k.m_type = katom_t::literal; });
|
||||
std::for_each(begin + 1, end - 1, mark_as_literal);
|
||||
// Hide Klammertext structural characters in the content as KTESC
|
||||
// markers so the literal text survives re-katomization (the
|
||||
// @document :text sub-Machine, the @eval result read-back).
|
||||
// Resolved back to the characters at final processing. Literal
|
||||
// KLAMMER content (@code) is NOT treated this way -- it is marked
|
||||
// by mark_literal_klammer_content() and reaches the @eval code raw.
|
||||
std::for_each(begin + 1, end - 1, [](Katom& k) {
|
||||
mark_as_literal(k);
|
||||
k.m_text = hide_structural_characters(k.m_text);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void hide_special_katoms(katom_list& katoms)
|
||||
{
|
||||
// Replace the text of ^-quoted special-character katoms (^@, ^|, ^#, ^^,
|
||||
// ^:, ^*) with KTESC markers. The katomizer strips the "^" when the
|
||||
// katom is constructed, so without this the bare character leaks into
|
||||
// assembled strings (state values, @eval results) and is re-interpreted
|
||||
// as Klammertext syntax when those strings are re-katomized -- by
|
||||
// @document's :text sub-Machine or the @eval result read-back in
|
||||
// Eval::eval. Markers are inert text at every level and are resolved to
|
||||
// the characters at final processing (Target::resolve_escapes).
|
||||
//
|
||||
// Skipped inside:
|
||||
// * @@...@@ and @@@...@@@ definition spans -- parameter declarations,
|
||||
// descriptions, and argtype patterns are extracted as plain strings
|
||||
// (kdesc display, validation regexes); a klammer BODY is re-processed
|
||||
// through process_katoms() at application time, outside any
|
||||
// definition span, so its quoted specials are hidden then.
|
||||
// * @eval/@read/@cond argument spans -- code, filenames, and
|
||||
// predicates consumed by the primitive, not target text (the same
|
||||
// rule as the general-body escape pass in Machine::apply_klammer).
|
||||
(void)K::log(4);
|
||||
int definition_depth = 0;
|
||||
int code_depth = 0; // inside an @eval/@read/@cond span
|
||||
std::vector<bool> apply_is_code; // one entry per open application
|
||||
for (auto& k : katoms) {
|
||||
switch (k.m_type) {
|
||||
case katom_t::define_begin:
|
||||
case katom_t::machine_begin:
|
||||
++definition_depth;
|
||||
continue;
|
||||
case katom_t::define_end:
|
||||
case katom_t::machine_end:
|
||||
if (definition_depth > 0) --definition_depth;
|
||||
continue;
|
||||
case katom_t::eval_begin:
|
||||
case katom_t::read_begin:
|
||||
case katom_t::cond_begin:
|
||||
apply_is_code.push_back(true);
|
||||
++code_depth;
|
||||
continue;
|
||||
case katom_t::apply_begin:
|
||||
apply_is_code.push_back(false);
|
||||
continue;
|
||||
case katom_t::apply_end:
|
||||
if (!apply_is_code.empty()) {
|
||||
if (apply_is_code.back()) --code_depth;
|
||||
apply_is_code.pop_back();
|
||||
}
|
||||
continue;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (k.m_type == katom_t::special &&
|
||||
definition_depth == 0 && code_depth == 0) {
|
||||
k.m_text = hide_structural_characters(k.m_text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ find_span_katoms(
|
||||
|
||||
void encode_nonascii_characters(std::vector<Katom>& katoms);
|
||||
void mark_literal_katoms(std::vector<Katom>& katoms);
|
||||
void hide_special_katoms(std::vector<Katom>& katoms);
|
||||
void mark_ignored_katoms(std::vector<Katom>& katoms);
|
||||
|
||||
void process_klammer_katoms(std::vector<Katom>& katoms);
|
||||
|
||||
@@ -239,6 +239,7 @@ void Machine::process_katoms(
|
||||
{
|
||||
mark_literal_klammer_content(katoms);
|
||||
if (literal) mark_literal_katoms(katoms);
|
||||
hide_special_katoms(katoms);
|
||||
if (nonascii) encode_nonascii_characters(katoms);
|
||||
if (ignore) mark_ignored_katoms(katoms);
|
||||
if (whitespace) process_whitespace_modifiers(katoms);
|
||||
@@ -516,13 +517,22 @@ std::string Machine::run_phase_functions()
|
||||
Target target = m_targets.get(m_state.value("K_target"), Locator());
|
||||
if (!target.m_after_apply.empty()) {
|
||||
(void)K::log(2, target);
|
||||
Eval E(*this, Locator());
|
||||
for (auto f : target.m_after_apply) {
|
||||
// A mode-tagged spec (":cpp ...") names a function that receives
|
||||
// the Machine itself; a bare Python function is called with the
|
||||
// result text. The Eval is constructed per phase so a chained
|
||||
// phase sees its predecessor's result in K_result.
|
||||
Eval E(*this, Locator());
|
||||
if (!f.empty() && f[0] != ':') {
|
||||
f += "(K_result)";
|
||||
}
|
||||
f = "@eval " + f + " @";
|
||||
auto katoms = katomize(line_split(f), "phase");
|
||||
katom_list eval_katoms = E.eval(katoms.begin(), katoms.end() - 2);
|
||||
// msg() << "eval_katoms: " << eval_katoms << "\n";
|
||||
m_result = to_string(eval_katoms.begin(), eval_katoms.end());
|
||||
// A phase function's input and output are final target text, not
|
||||
// Klammertext: take the raw result string. Re-reading it as
|
||||
// Klammertext (Eval::eval) would misparse target characters --
|
||||
// e.g. a "@" from a quoted ^@ in justified txt output.
|
||||
m_result = E.eval_command(katoms.begin(), katoms.end() - 2);
|
||||
}
|
||||
}
|
||||
return m_result;
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
#include "target.h"
|
||||
#include "log.h"
|
||||
#include "show.h"
|
||||
@@ -85,20 +88,60 @@ std::string Target::escape_text(std::string text) const
|
||||
std::string Target::unescape_text(std::string text) const
|
||||
{
|
||||
// Restore KTESC markers to original characters (for programmatic use)
|
||||
for (const auto& [ch, repl] : m_escapes) {
|
||||
text = string_replace(text, escape_marker(ch), ch);
|
||||
}
|
||||
return text;
|
||||
return ktesc_resolve(text);
|
||||
}
|
||||
|
||||
std::string Target::resolve_escapes(std::string text) const
|
||||
{
|
||||
// Target-declared escapes first (marker -> declared replacement), then
|
||||
// the generic decode for the remaining markers (marker -> the character
|
||||
// itself: quoted Klammertext specials and literal-span content).
|
||||
for (const auto& [ch, repl] : m_escapes) {
|
||||
text = string_replace(text, escape_marker(ch), repl);
|
||||
}
|
||||
return ktesc_resolve(text);
|
||||
}
|
||||
|
||||
std::string ktesc_resolve(std::string text)
|
||||
{
|
||||
// Hand-rolled scan: no std::regex here, this runs over document-sized
|
||||
// strings.
|
||||
static const std::string tag = "KTESC";
|
||||
size_t pos = 0;
|
||||
while ((pos = text.find(tag, pos)) != std::string::npos) {
|
||||
size_t start = pos + tag.size();
|
||||
size_t close = text.find(tag, start);
|
||||
if (close == std::string::npos) break;
|
||||
size_t len = close - start;
|
||||
bool is_hex = len > 0 && len % 4 == 0 &&
|
||||
std::all_of(text.begin() + start, text.begin() + close,
|
||||
[](unsigned char c) { return std::isxdigit(c) != 0; });
|
||||
if (!is_hex) {
|
||||
// Not a marker body; the closing tag may open a real marker.
|
||||
pos = start;
|
||||
continue;
|
||||
}
|
||||
std::string chars {};
|
||||
for (size_t i = start; i < close; i += 4)
|
||||
chars += (char)std::stoi(text.substr(i, 4), nullptr, 16);
|
||||
text.replace(pos, close + tag.size() - pos, chars);
|
||||
pos += chars.size();
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
std::string hide_structural_characters(const std::string& s)
|
||||
{
|
||||
std::string result {};
|
||||
for (char c : s) {
|
||||
if (c == '@' || c == '|' || c == '#' || c == '^' || c == ':' || c == '*')
|
||||
result += Target::escape_marker(std::string(1, c));
|
||||
else
|
||||
result += c;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void Target::add_after_apply(std::string function_specs)
|
||||
{
|
||||
for (auto f : regex_split(function_specs, std::regex(R"(\s+;\s+)"), true)) {
|
||||
|
||||
13
mac/target.h
13
mac/target.h
@@ -47,3 +47,16 @@ public:
|
||||
|
||||
std::vector<std::pair<std::string, std::string>>
|
||||
parse_transforms(std::string transform_string);
|
||||
|
||||
// Decode every KTESC<hex>KTESC marker in text back to its original
|
||||
// characters. Used for the final output (after target-declared escapes have
|
||||
// been resolved to their replacements) and for programmatic use of argument
|
||||
// values. The Python counterpart is unescape_ktesc() in klammer_base.py.
|
||||
std::string ktesc_resolve(std::string text);
|
||||
|
||||
// Replace each Klammertext structural character (@ | # ^ : *) in s with its
|
||||
// KTESC marker, so text that has already been interpreted once (quoted
|
||||
// specials, ^'...'^ literal content) survives re-katomization by
|
||||
// sub-Machines and the @eval result read-back. Resolved by ktesc_resolve()
|
||||
// at final processing.
|
||||
std::string hide_structural_characters(const std::string& s);
|
||||
|
||||
Reference in New Issue
Block a user