Files
klammertext/sks/target/html_util.h
Andy Kopra 8a2699a253 Typed arguments, calculated tables, spans, closed-world fonts, top-level fnt/ and env/
Sync with klammertext-dev through b90b0e09:

- Argument types end to end: :python_cast values are applied (Python
  @eval receives real bools/numbers/lists), argument values are
  validated against their argtype patterns with the argtype's
  description as the error message, argtypes can declare :default
  (overridable per declaration), and parameterized type families are
  supported: rest(N) casts a rest argument to an N-dimensional list
  (bar-count = dimension).
- Unified indexed_range syntax (selector with parenthesized subsets,
  composable mnemonic names) for table lines and spans.
- Table klammer: caption fonts fixed in both targets, :column_width /
  :leading / :colsep wired, :colspan and :rowspan render (HTML
  attributes; \multicolumn / \multirow), calculated cell values (:calc)
  with prefix operators, display-precision semantics, :calc_format and
  :decimal period|comma.
- Fonts: closed-world resolution on the Klammertext font store
  (infrastructure in mac/font_store; no Google Fonts links or fetch).
  Default fonts live in the top-level fnt/; additional fonts install
  into KLAMMERTEXT_FONTS directories via kdesc --font (list, samples,
  preview, install — classification by font metadata).  CSS font family
  names are quoted (digit-initial families were silently lost).
- Environment files moved from mac/env/ to the top-level env/; shell
  profiles source env/runtime.env.  Dead per-host variants removed.
- Container: fnt/ ships in the image; curl removed (no network use).
2026-07-22 18:17:43 +02:00

116 lines
3.9 KiB
C++

#pragma once
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <variant>
#include <utility>
using strings_t = std::vector<std::string>;
using attr_t = std::variant<int, float, std::string>;
class HTML;
using html_element_t = std::variant<std::string,HTML>;
using elements_t = std::vector<html_element_t>;
const std::string preamble_tag = "!DOCTYPE html";
const std::vector<std::string> block_elements {
"!DOCTYPE", "html", "head", "body",
"address", "article", "aside", "blockquote", "details", "dialog",
"div", "dl", "dt", "dd", "fieldset", "figcaption", "figure", "footer", "form",
"kt-part", "kt-chapter", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup", "hr", "img", "li",
"main", "nav", "ol", "p", "section", "table", "ul", "td", "tr", "pre" };
std::ostream& operator<<(std::ostream& os, const elements_t& elements);
std::string to_string(elements_t e);
class HTML {
public:
operator std::string() {
std::stringstream ss {}; ss << *this; return ss.str(); };
HTML(std::string tag) :
m_tag(tag) {};
HTML(std::string tag, std::string text) :
m_tag(tag), m_text({text}) {};
HTML(std::string tag, elements_t elements) :
m_tag(tag), m_elements(elements) {};
HTML& attr(std::string name, attr_t value);
HTML& attrs(std::string s);
HTML& cls(std::string name) { return attr("class", name); };
HTML& sty(std::string css) { return attr("style", css); };
HTML& id(std::string id) { return attr("id", id); };
bool void_element(std::string tag) const;
bool empty_element(std::string tag) const;
friend std::ostream& operator<<(std::ostream& os, const HTML& h);
friend std::ostream& operator<<(std::ostream& os, const std::vector<HTML>& hv);
private:
std::string m_tag {};
std::vector<std::pair<std::string, attr_t>> m_attrs {};
std::string m_text {};
elements_t m_elements {};
inline static const std::vector<std::string> m_void_tags {
"!DOCTYPE html", "area", "base", "br", "col", "command", "embed", "hr","img",
"input", "keygen", "link", "meta", "param", "source", "track", "wbr" };
inline static const std::vector<std::string> m_empty_tags { "script" };
};
namespace html {
HTML elt(std::string tag);
HTML elt(std::string tag, std::string text);
HTML elt(std::string tag, elements_t elements);
HTML elt(std::string tag, HTML e, elements_t elts);
//HTML preamble();
//HTML head(strings_t css_filenames, strings_t js_filenames);
elements_t page(
std::string title,
std::string page_title,
std::string output_dir,
std::string nav,
int max_level,
std::string toc,
std::string text,
std::string date,
std::string version,
std::string copyright,
std::string css,
strings_t css_filenames = {},
strings_t js_filenames = {},
strings_t local_fonts = {},
std::string logo = {});
void add_title(elements_t& body, std::string title, std::string logo="");
void add_title(elements_t& body, std::string title, std::string logo);
void add_nav(elements_t& body, int max_level);
void add_text(elements_t& body, std::string text, std::string toc_text, bool text_only);
void add_bottom_spacer(elements_t& body);
void add_status_bar(elements_t& body, std::string date, std::string version, std::string copyright);
void add_page_cache(elements_t& body);
void add_js_links(elements_t& body, std::vector<std::string> js_filenames, std::string output_dir);
elements_t make_page(
elements_t body,
std::string page_title, std::string css,
std::vector<std::string> css_filenames,
std::vector<std::string> local_fonts);
bool tag_is_block_element(std::string tag);
std::string make_paragraphs(std::string html_text);
std::string minimize_css(std::string src);
std::string minimize_js(std::string src);
}