Initial commit: Klammertext source distribution
Curated source subset assembled by klammertext-dev's doc/make_dist.sh: the Klammermachine (mac), the Standard Klammer Set (sks), the commands (com), editor plugins and install guides (doc), a test subset (tst), and lib/bin placeholders. Builds with 'make -C com'. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
119
sks/target/html_util.h
Normal file
119
sks/target/html_util.h
Normal file
@@ -0,0 +1,119 @@
|
||||
#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);
|
||||
|
||||
void install_local_fonts(strings_t font_dirs, strings_t names, std::string output_dir);
|
||||
|
||||
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 = {},
|
||||
strings_t google_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,
|
||||
std::vector<std::string> google_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);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user