c++TinML转html

news2025/3/12 18:06:16

c++TinML转html

  • 前言
  • 解析
  • 解释
  • 转译html
    • 类定义
    • 开头html
  • 结果
    • 这是最终效果(部分): ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/6cf6c3e3c821446a84ae542bcc2652d4.png)

前言

在python.tkinter设计标记语言(转译2-html)中提到了将TinML转为静态html的python实现方法;在HtmlRender - c++实现的html生成类中又提供了我自己写的基于c++编辑html的简单方式,这篇笔记就是使用c++将tinml转为静态html。

本示例未使用任何第三方库(除了我之前提供的HtmlRender类),但是在html中代码高亮方面使用了在线的highlight.js

整套流程下来和基于python的TinText平台类似,都是先解析TinML标记文本段,再给每个标记赋以特定含义(解释过程),最后通过这些解释后的内容生成html结构,生成html文本。

项目示例:CppTinParser: TinML to html by c++

解析

这一部分是对TinML标记的语法解析。

class TinLexer{

public:
    TinLexer(string contents){
        this->content = '\n' + contents + '\n';
    }

    void run_test();
    vector<string> run();
    vector<map<string,vector<string>>> lex();

private:
    string content;

};

主要解析功能就是对每一行TinML标记文本段进行基于上下文标记的正则匹配和结构解析,这部分相当于TinText项目中的TinParser

  • 单行TinML标记,分为标记名称和标记参数组
  • 多行模式TinML,转为单行TinML标记解析后格式

部分代码如下:

vector<map<string,vector<string>>> TinLexer::lex(){
    // 预处理
    // tackle <tinfile>: add context of the specific TinML file into this->content
    // 此部分代码省略,可见程序源码
    // ...

    vector<map<string,vector<string>>> tinresults;//总结果
    
    vector<string> contents;
    string nowtag;//当前标签

    contents = split(this->content, '\n');

    bool Lines = false;//多行模式
    regex pattern("^<(.*?)>(.*)");
    smatch result;
    vector<string> args;//参数列表
    
    for (const auto &i : contents){
        if (size(i) == 1){
            // 实际上是empty,不过貌似写不对
            // 既有可能是因为getline将\n转为\0,导致该字符串截止
            continue;
        }
        
        if (size(i)>=2 && i.substr(0,2)=="|-"){
            //  cout << "Comment: " << i << endl;
            continue;
        }

        map<string,vector<string>> lineargs;//单行参数

        if (Lines){
            if (i[0]=='|'){
                if (size(i)>2 && i[i.length()-2]!='|'){
                    string content = i.substr(1,size(i)-2);
                    args.push_back(subreplace(content, "%VEB%", "|"));
                }else if (size(i)==2){
                    args.push_back("");
                }else{
                    string content = i.substr(1,size(i)-3);
                    args.push_back(subreplace(content, "%VEB%", "|"));
                    // for (auto const &j : args){
                    //     cout << "Arg: " << j << endl;
                    // }
                    lineargs[nowtag] = args;
                    tinresults.push_back(lineargs);
                    args.clear();
                    Lines = false;
                }
            }
        }else if (i[i.length()-2] == ';'){
            // getline最后一个为\0,因此-2
            // string本身不以\0结尾,但是getline会改成\0
            Lines = true;
            bool ismatch = regex_search(i, result, pattern);
            if (ismatch){
                // cout << "Tag: " << result[1] << endl;
                nowtag = result[1];
                string content = result[2];
                int lastindex = content.find_last_of(';');
                content = content.substr(0, lastindex);
                args.push_back(subreplace(content, "%VEB%", "|"));
            }else{
                cout << "\033[33;1m不可被解析,当作<p>处理:" << i << "\033[0m" << endl;
                args.push_back(subreplace(i, "%VEB%", "|"));
                lineargs["p"] = args;
                tinresults.push_back(lineargs);
                args.clear();
            }
        }else{
            bool ismatch = regex_search(i, result, pattern);
            if (ismatch){
                nowtag = result[1];
                string content = result[2];
                auto oargs = split(content, '|');
                for (auto const &j : oargs)
                    args.push_back(subreplace(j, "%VEB%", "|"));
                lineargs[nowtag] = args;
                tinresults.push_back(lineargs);
                args.clear();
            }else{
                //无法匹配当作p处理
                cout << "\033[33;1m不可被解析,当作<p>处理:" << i << "\033[0m" << endl;
                args.push_back(subreplace(i, "%VEB%", "|"));
                lineargs["p"] = args;
                tinresults.push_back(lineargs);
                args.clear();
            }
        }
    }

    return tinresults;
}

这里还遇到一个坑,string.getline会将分割字符转为\0,这样导致string类型变量所包含的文字比表达的文字多一个“字”。string的设计是不包含的\0,所以要注意从string类型中取字符的下标。

解析过程是最基础的,也几乎不会再维护,毕竟TinML的语法就这样了。

解释

这和TinText软件中转译html的过程一样,只不过TinEngine在渲染之前就生成了解释内容并传递给了转译器,而CppTinParser需要单独进行参数解释。

首先,生成每个标记对应的参数含义:

static map<string, vector<string>> tinkws;

void loadkws()
{
    // 标记对应的参数键
    tinkws["ac"] = {"name"};
    tinkws["anchor"] = {"name"};
    tinkws["code"] = {"type", "codes"};
    tinkws["fl"] = {};
    tinkws["follow"] = {};
    tinkws["html"] = {"htmls"};
    tinkws["img"] = {"name", "url", "size"};
    tinkws["image"] = {"name", "url", "size"};
    tinkws["lnk"] = {"text", "url", "description"};
    tinkws["link"] = {"text", "url", "description"};
    tinkws["a"] = {"text", "url", "description"};
    tinkws["ls"] = {"lists"};
    tinkws["list"] = {"lists"};
    tinkws["n"] = {"notes"};
    tinkws["note"] = {"notes"};
    tinkws["nl"] = {"lists"};
    tinkws["numlist"] = {"lists"};
    tinkws["p"] = {"texts"};
    tinkws["pages"] = {"name"};
    tinkws["/page"] = {""};
    tinkws["/pages"] = {""};
    // 展示到这里位置,当然,这不是完整的
    //...
}

由于之前已经生成了类似如下的数据结构:

(
	("<title>",("title","1"),),
	("<p>",("paragraph",)),
	...
)

现在,就需要把这些已经分割好的标记文本转为有意义的内容:

(
	("<title>",("title": "title", "level": "1"),),
	("<p>",("paragraph": ("paragraph",))),
	...
)

这里采用的逻辑是对一个标签所拥有的所有参数一一对应,多出来的统一给最后一个参数。

这部分逻辑实现如下:

map<string, string> tokeywords(string tag, vector<string> contents)
{
    // 将列表顺序参数转为键、值类参数
    map<string, string> keywords;
    cout << "Tag: " << tag << endl;
    keywords["tag"] = tag;

    int args_num = contents.size();
    int index = 0;
    for (auto kws : tinkws[tag])
    {
        if (index >= args_num)
        {
            // 剩下键全为空
            keywords[kws] = "";
            continue;
        }
        keywords[kws] = contents[index];
        // cout << kws << " = " << contents[index] << endl;
        index++;
    }

    if (index < args_num)
    {
        // 将剩下的参数全部放入最后一个键中
        for (int i = index; i < args_num; i++)
        {
            keywords[tinkws[tag].back()] += "\n" + contents[i];
        }
    }

    for (auto kw : keywords)
    {
        if (kw.first == "tag")
        { // 跳过tag键
            continue;
        }
        cout << kw.first << " = " << kw.second << endl;
    }

    return keywords;
}
class TinParser
{
public:
    TinParser(vector<map<string, vector<string>>> contents)
    {
        this->contents = contents;
    }
    void parse();
    void render(string file);

private:
    vector<map<string, vector<string>>> contents;
    vector<map<string, string>> result;
};

void TinParser::parse()
{
    // 判断tag是否存在
    contents = this->contents;
    for (auto content : contents)
    {
        string tag = content.begin()->first;
        if (tinkws.find(tag) == tinkws.end())
        {
            cerr << "\033[33;1mtag not found: " << tag << "\033[0m" << endl;
            // //string转char
            // char c_tag;
            // strcpy(&c_tag, tag.c_str());
            // throw NoTagName(c_tag);
            continue;
        }
        map<string, string> results = tokeywords(tag, content.begin()->second);
        result.push_back(results);
    }
}

转译html

经过前面的准备,这里已经准备好了将TinML转译为html的所有信息。

由于在之前的文章中已经给出了HtmlRender的实现和使用示例,这里便不再赘叙。

这部分的代码和TinText的tin2html.py实现在逻辑上完全一致,甚至可以看作是python到c++的翻译。

类定义

class TinRender {
    //tin->html类
public:
    TinRender(const string &file) : file_(file){}
    HtmlRender* render(vector<map<string,string>> content, bool _style = true, bool _code_style = true);//转译为html
    void output(vector<map<string,string>> content);//输出
private:
    string file_;//html文件路径
    ofstream fout_;//html文件输出流
};

开头html

就是html头中包含的样式、额外引入js、标题这些。

HtmlRender* TinRender::render(vector<map<string, string>> content, bool _style, bool _code_style){
    //转译为HtmlRender。可以直接使用output方法转译+输出保存
    //注意,CPPTinParser只是用c++实现的tinml转译器
    //不作为TinML规范的检查器
    string tag;
    HtmlRender* html = new HtmlRender("html", "", {});
    HtmlRender* head = new HtmlRender("head", "", {});

    HtmlRender* title = new HtmlRender("title", "TinML", {});
    head->add(title);

    HtmlRender* meta = new HtmlRender("meta", "", {{"charset","UTF-8"}}, true);
    head->add(meta);

    //读取css文件
    if (_style){
        string filename = "blubook.css";
        ifstream infile;
        infile.open(filename.data());   //将文件流对象与文件连接起来 
        assert(infile.is_open());   //若失败,则输出错误消息,并终止程序运行 
        string s, strAllLine;
        while (getline(infile, s)){
            strAllLine += s + "\n";
        }
        infile.close();             //关闭文件输入流
        HtmlRender* style = new HtmlRender ("style", strAllLine, {}, false, true);
        head->add(style);
    }

    //<code>样式
    if (_code_style){
        HtmlRender* codestyle = new HtmlRender("link", "", {{"rel","stylesheet"}, {"type", "text/css"}, {"href","https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/vs.min.css"}});
        head->add(codestyle);
        HtmlRender* codescript = new HtmlRender("script", "", {{"src","https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"}});
        head->add(codescript);
        HtmlRender* codestartscript = new HtmlRender("script", "hljs.highlightAll();", {});
        head->add(codestartscript);
    }
    
    HtmlRender* _body = new HtmlRender("body", "", {});
    HtmlRender* body = new HtmlRender("div", "", {{"id","content"}});
    _body->add(body);
    html->add(head);
    html->add(_body);
    
    // 提前定义类型变量
    HtmlRender* table;
    HtmlRender* tbody;
    bool tablehead = false;

    HtmlRender* tabsview;
    bool pagestag = false;
    vector<string> pagesnames;
    vector<map<string, string>> pagescontent;
    int pagescount = 0;
    
    // ...
}

然后就是对每一个标记的单独转译,这些都在上面的那个方法里,这里给几个例子:

// ...
        //常规转译TinML解析结果
        if (tag == "ac" || tag == "anchor"){
            string name = item["name"];
            if (name[0]=='#'){
                //锚点链接
                HtmlRender* anchor = new HtmlRender("a", "🔗", {{"href", name}});
                HtmlRender* lastitem = body->children().back();
                lastitem->add(anchor);
            }else{
                //锚点定义
                HtmlRender* anchor = new HtmlRender("a", "", {{"id", name}});
                body->add(anchor);
            }
        }else if (tag == "code"){
            //代码块
            //使用highlight.js,不支持tinml代码块
            string type = item["type"];
            string codes = item["codes"];
            if (type == "tin"){
                type = "nohighlight";
            }else{
                type = "language-" + type;
            }
            HtmlRender* pre = new HtmlRender("pre", "", {});
            HtmlRender* code = new HtmlRender("code", codes, {{"class",type}});
            pre->add(code);
            body->add(pre);
        }else if (tag == "html"){
            //html文本
            string htmltext = item["htmls"];
            HtmlRender* htmlcont = new HtmlRender("", htmltext, {}, false, true);
            body->add(htmlcont);
        }else if (tag == "img" || tag == "image"){
            //图片
            string name = item["name"];
            string url = item["url"];
            if (url.empty()){
                //因为CppTinParser只是用c++实现的tinml转译器
                //不作为TinML规范的检查器,也不作为tin文件渲染环境
                //所以不会有额外的资源文件储存位置
                //因此,不支持从本地文件中导入图片
                continue;
            }
            vector<string> size = render_split(item["size"], 'x');
            string width = size[0];
            string height = size[1];
            HtmlRender* img = new HtmlRender("img", "", {{"alt", ""},{"src", url}, {"width", width}, {"height", height}});
            body->add(img);
        }// ...
// ...

CppTinParser甚至给对<p>的解析也单独移植了TinText中的逻辑:

void load_para(HtmlRender* p, vector<string> lines){
    //添加<p>内容
    vector<char> tags = {'*','-','_','/','=','!','^','&','#'};
    regex reg(".*?!\\[(.*)\\]\\((.*)\\)");
    smatch result;
    int count = 0;

    if (lines.size()==0){
        //空行
        HtmlRender* br = new HtmlRender("br", "", {}, true);
        p->add(br);
        return;
    }
    
    for (auto &line : lines) {
        HtmlRender* last = p;//p最后一个元素

        if (line[0] == ' '){
            //空格
            string text = line.substr(1);
            HtmlRender* p_item = new HtmlRender("", text, {});
            p->add(p_item);
            continue;
        }

        string head;
        if (line.size() <=9){
            head = line;
        }else{
            head = line.substr(0, 9);
        }
        //<p>开头标记需要连续
        count = 0;
        for (auto &tag_char : head){
            if (find(tags.begin(), tags.end(), tag_char) != tags.end()){
                count++;
            }else{
                break;
            }
        }

        head = head.substr(0, count);

        if (count == 0){
            HtmlRender* p_item = new HtmlRender("", line, {});
            p->add(p_item);
            continue;
        }

        if (head.find('*')!=string::npos){
            HtmlRender* nowlast = new HtmlRender("b", "", {});
            last->add(nowlast);
            last = nowlast;
        }
        if (head.find('/')!=string::npos){
            HtmlRender* nowlast = new HtmlRender("i", "", {});
            last->add(nowlast);
            last = nowlast;
        }
        if (head.find('_')!=string::npos){
            HtmlRender* nowlast = new HtmlRender("u", "", {});
            last->add(nowlast);
            last = nowlast;
        }
        if (head.find('-')!=string::npos){
            HtmlRender* nowlast = new HtmlRender("s", "", {});
            last->add(nowlast);
            last = nowlast;
        }
        if (head.find('=')!=string::npos){
            // = 和 # 只能存在一个
            HtmlRender* nowlast = new HtmlRender("mark", "", {});
            last->add(nowlast);
            last = nowlast;
        }else if (head.find('#')!=string::npos){
            HtmlRender* nowlast = new HtmlRender("code", "", {});
            last->add(nowlast);
            last = nowlast;
        }
        if (head.find('^')!=string::npos){
            // ^ 和 & 只能存在一个
            HtmlRender* nowlast = new HtmlRender("sup", "", {});
            last->add(nowlast);
            last = nowlast;
        }else if (head.find('&')!=string::npos){
            HtmlRender* nowlast = new HtmlRender("sub", "", {});
            last->add(nowlast);
            last = nowlast;
        }
        if (head.find('!')!=string::npos){
            bool ismatch = regex_match(line, result, reg);
            if (ismatch) {
                string text = result[1];
                string url = result[2];
                if (text == ""){
                    text = url;
                }
                HtmlRender* p_item = new HtmlRender("a", text, {{"href",url}});
                last->add(p_item);
            }else{
                string text = line.substr(count);
                HtmlRender* p_item = new HtmlRender("", text, {});
                last->add(p_item);
            }
        }else{
            last->configcnt(line.substr(count));
        }
    }
}

结果

这里用的是TinText项目中的test.tin。效果上来看,除了<code>中对TinML的缺失以及对在线图片、tinfile不支持外,其他几乎一样,而且对于大文件的转译绝对比python实现要快。

这是源文件(部分):
在这里插入图片描述


在这里插入图片描述

这是最终效果(部分):
在这里插入图片描述

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2300584.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

《蓝耘服务器:领先架构、技术创新与行业应用全景解析》

&#x1f31f; 嗨&#xff0c;我是Lethehong&#xff01;&#x1f31f; &#x1f30d; 立志在坚不欲说&#xff0c;成功在久不在速&#x1f30d; &#x1f680; 欢迎关注&#xff1a;&#x1f44d;点赞⬆️留言收藏&#x1f680; &#x1f340;欢迎使用&#xff1a;小智初学…

uniapp webview嵌入外部h5网页后的消息通知

最近开发了个oa系统&#xff0c;pc端的表单使用form-create开发&#xff0c;form-create 是一个可以通过 JSON 生成具有动态渲染、数据收集、验证和提交功能的表单生成组件。移动端使用uniapp开发&#xff0c;但是因为form-create移动端只支持vant&#xff0c;不支持uniapp。官…

java听书项目

项目的架构 网关:1路由转发 2.认证鉴权(token)3.统一处理(跨域) Mysql:关系型数据库 ES:搜索数据库 Redis:页面级缓存,会话状态存储 GitLab:私有托管平台 K8S:自动化部署、扩展和管理容器化应用程序的开源系统 Jenkins:自动化部署 1.环境搭建 创建一个父工程…

C#功能测试

List 内部元素为引用 src[0]为"11" List<Source> src new List<Source>(); src.Add(new Source() { Name "1", Age 1, Description "1" }); src.Add(new Source() { Name "2", Age 2, Description "2"…

2025百度快排技术分析:模拟点击与发包算法的背后原理

一晃做SEO已经15年了&#xff0c;2025年还有人问我如何做百度快速排名&#xff0c;我能给出的答案就是&#xff1a;做好内容的前提下&#xff0c;多刷刷吧&#xff01;百度的SEO排名算法一直是众多SEO从业者研究的重点&#xff0c;模拟算法、点击算法和发包算法是百度快速排名的…

DeepSeek+即梦 做AI视频

DeepSeek做AI视频 制作流程第一步&#xff1a;DeepSeek 生成视频脚本和分镜 第二步&#xff1a;生成分镜图片绘画提示词第三步&#xff1a;生成分镜图片第四步&#xff1a;使用可灵 AI 工具&#xff0c;将生成的图片转成视频。第五步&#xff1a;剪映成短视频 DeepSeek 真的强&…

【R语言】聚类分析

聚类分析是一种常用的无监督学习方法&#xff0c;是将所观测的事物或者指标进行分类的一种统计分析方法&#xff0c;其目的是通过辨认在某些特征上相似的事物&#xff0c;并将它们分成各种类别。R语言提供了多种聚类分析的方法和包。 方法优点缺点适用场景K-means计算效率高需…

LVS相关原理

一、LVS集群的体系结构 1.1 LVS简介 LVS 是 Linux Virtual Server 的简称&#xff0c;也就是 Linux 虚拟服务器 , 是一个由章文嵩博士发起的自由软件项目&#xff0c;它的官方站点是 www.linuxvirtualserver.org 。现在 LVS 已经是 Linux标准内核的一部分&#xff0c;在Linux2…

linux--关于makefile

makefile文件 可以指定编译顺序&#xff0c;这样方便一个项目的多个文件要编译的挨个操作的麻烦。 makefile文件的命名&#xff1a;makefile 或者 Makefile 必须是这俩&#xff0c;系统才能识别 规则的书写语法如下&#xff1a; 一个makefile内可以有多个规则 目标:依赖a 依…

Java发展史

JavaEE的由来 语言的诞生 Java的前身是Oak语言&#xff0c;其目的是搞嵌入式开发开发智能面包机 叮~~~&#x1f35e;&#x1f35e;&#x1f35e; 产品以失败告终 巅峰 网景公司需要网景浏览器打开网页&#xff0c;Oak->Java&#xff0c;进行前端开发&#xff08;相关技…

Jenkins 新建配置 Freestyle project 任务 六

Jenkins 新建配置 Freestyle project 任务 六 一、新建任务 在 Jenkins 界面 点击 New Item 点击 Apply 点击 Save 回到任务主界面 二、General 点击左侧 Configure Description&#xff1a;任务描述 勾选 Discard old builds Discard old builds&#xff1a;控制何时…

Electron视图进程和主进程通讯

快速创建基于vue的electron项目&#xff1a;quick-start/create-electron - npm 视图线程也就index.html是无法直接访问这个api的&#xff08;如果没有开启视图层访问nodejs的功能&#xff0c;现在几乎没法直接开启&#xff0c;开启了一堆警告提示&#xff09; 所以需要通过r…

【湖南-益阳】《益阳市市本级政府投资信息化项目预算编制与财政评审工作指南》益财评〔2024〕346号-省市费用标准解读系列40

《益阳市市本级政府投资信息化项目预算编制与财政评审工作指南&#xff08;试行&#xff09;》&#xff08;益财评〔2024〕346号&#xff09;由益阳市财政局主编&#xff0c;2024年10月17日起正式执行&#xff0c;本指南主要规定了政府投资信息化项目费用的构成、测量过程和方法…

具身智能在智能巡检机器人中的应用——以开关柜带电操作机器人为例

随着机器人技术和人工智能的迅速发展&#xff0c;具身智能在各行业的应用日益广泛&#xff0c;尤其是在电力行业中的智能巡检领域。传统的电力巡检和维护工作通常需要人工操作&#xff0c;存在着高温、高压、强电磁场等危险环境&#xff0c;且效率较低。开关柜带电操作机器人作…

nuxt中引入element-ui组件控制台报错问题

在使用element-ui组件的外层加一层 <client-only placeholder"Loading..."><van-button type"primary">主要按钮</van-button> </client-only> 实际使用&#xff1a; <div class"tab"><client-only placehol…

Java面试第二山!《计算机网络》!

在 Java 面试里&#xff0c;计算机网络知识是高频考点&#xff0c;今天就来盘点那些最容易被问到的计算机网络面试题&#xff0c;帮你轻松应对面试&#xff0c;也方便和朋友们一起探讨学习。 一、HTTP 和 HTTPS 的区别 1. 面试题呈现 HTTP 和 HTTPS 有什么区别&#xff1f;在…

RocketMQ 5.0安装部署

0.前言 在微服务架构逐渐成为主流的今天&#xff0c;消息队列如同数字世界的快递员&#xff0c;承担着系统间高效通信的重要使命。 Apache RocketMQ 自诞生以来&#xff0c;因其架构简单、业务功能丰富、具备极强可扩展性等特点被众多企业开发者以及云厂商广泛采用。历经十余…

俄罗斯方块游戏完整代码示例

以下是一个基于Cocos Creator引擎开发的俄罗斯方块游戏的完整代码示例。该游戏实现了俄罗斯方块的基本功能&#xff0c;并且代码整合在单个文件中&#xff0c;无需任何外部依赖&#xff0c;可以直接在浏览器中运行。 1. 创建Cocos Creator项目 首先&#xff0c;确保你已经安装了…

Ubuntu22.04配置cuda/cudnn/pytorch

Ubuntu22.04配置cuda/cudnn/pytorch 安装cuda官网下载.run文件并且安装/etc/profile中配置cuda环境变量 cudnn安装官网找cuda版本对应的cudnn版本下载复制相应文件到系统文件中 安装pytorch官网找cuda对应版本的pytorchpython代码测试pytorch-GPU版本安装情况 安装cuda 官网下…

【九】Golang 数组

&#x1f4a2;欢迎来到张胤尘的技术站 &#x1f4a5;技术如江河&#xff0c;汇聚众志成。代码似星辰&#xff0c;照亮行征程。开源精神长&#xff0c;传承永不忘。携手共前行&#xff0c;未来更辉煌&#x1f4a5; 文章目录 数组数组初始化默认初始化显式初始化省略长度初始化索…