Files
klammertext/mac/target.cpp
Andy Kopra d61336b191 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.
2026-07-25 21:16:21 +02:00

152 lines
4.7 KiB
C++

#include <algorithm>
#include <cctype>
#include "target.h"
#include "log.h"
#include "show.h"
//#include "text.h"
#include "util.h"
void Target::add_transform(std::string original, std::string transformed)
{
m_transforms.push_back({original, transformed});
}
void Target::add_transforms(std::string transforms)
{
add_transforms(parse_transforms(transforms));
}
void Target::add_transforms(string_pairs transforms)
{
for (auto [old_str, new_str] : transforms) {
add_transform(old_str, new_str);
}
}
void Target::transform(katom_list& katoms)
{
(void)K::log(3);
std::for_each(
katoms.begin(), katoms.end(),
[this] (Katom& k) {
// std::cout << "transform: " << k << "\n";
if (k.m_type != katom_t::literal) {
for (auto [a, b] : this->m_transforms) {
// std::cout << " " << a << right_arrow << b << "\n";
k.m_text = string_replace(k.m_text, a, b);
}
}
});
}
std::vector<std::pair<std::string, std::string>>
parse_transforms(std::string transform_string)
{
(void)K::log(3);
if (trim(transform_string).empty()) return {};
auto transforms = regex_split(transform_string, std::regex(R"(\s*\|\s*)"), true);
std::vector<std::pair<std::string, std::string>> result;
std::transform(transforms.begin(), transforms.end(), std::back_inserter(result),
[] (std::string s) {
strings_t v = word_split(s);
if (v.size() < 2) return std::pair(std::string{}, std::string{});
return std::pair(v[0], v[1]);
});
// Remove empty pairs
result.erase(std::remove_if(result.begin(), result.end(),
[](const auto& p) { return p.first.empty(); }), result.end());
return result;
}
void Target::add_escapes(std::string escape_spec)
{
auto words = word_split(escape_spec);
for (size_t i = 0; i + 1 < words.size(); i += 2) {
m_escapes.push_back({words[i], words[i+1]});
}
}
std::string Target::escape_marker(const std::string& ch)
{
std::stringstream ss {};
ss << "KTESC";
for (unsigned char c : ch)
ss << std::hex << std::setfill('0') << std::setw(4) << (int)c;
ss << "KTESC";
return ss.str();
}
std::string Target::escape_text(std::string text) const
{
for (const auto& [ch, repl] : m_escapes) {
text = string_replace(text, ch, escape_marker(ch));
}
return text;
}
std::string Target::unescape_text(std::string text) const
{
// Restore KTESC markers to original characters (for programmatic use)
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)) {
// msg() << "Add " << m_name << " after-apply: " << f << "\n";
m_after_apply.push_back(f);
}
}