C HTML格式解析与生成

cmake报错替换

if(NOT MyHTML_BUILD_WITHOUT_THREADS OR NOT MyCORE_BUILD_WITHOUT_THREADS)

set(CMAKE_THREAD_PREFER_PTHREAD 1)

if (WIN32)

set(CMAKE_USE_WIN32_THREADS_INIT ON)

set(CMAKE_THREAD_PREFER_PTHREADS TRUE)

set(THREADS_PREFER_PTHREAD_FLAG TRUE)

else ()

find_package(Threads REQUIRED)

if(NOT CMAKE_USE_PTHREADS_INIT)

message(FATAL_ERROR "Could NOT find pthreads (missing: CMAKE_USE_PTHREADS_INIT)")

endif()

endif()

endif()

测试

add_executable(example examples/myhtml/modify_and_serialize.c)

target_link_libraries(example ${PROJECT_LIB_STATIC})

代码
cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <myhtml/serialization.h>

/**
 *  Write output
 *  @param  buffer
 *  @param  size
 *  @param  ptr
 */
mystatus_t write_output(const char* data, size_t len, void* ctx)
{
    printf("%.*s", (int)len, data);
    return MyCORE_STATUS_OK;
}

/**
 *  Main procedure
 *  @return int
 */
int main()
{
    // initalize html engine
    myhtml_t *myhtml = myhtml_create();
    myhtml_init(myhtml, MyHTML_OPTIONS_DEFAULT, 1, 0);
    
    // init tree
    myhtml_tree_t *tree = myhtml_tree_create();
    myhtml_tree_init(tree, myhtml);
    
    // input string
    const char *input = "<html><head></head><body><a href=http://nu.nl></body></html>";
    
    // parse html
    myhtml_parse(tree, MyENCODING_UTF_8, input, strlen(input));

    // collection of links
    myhtml_collection_t *collection = myhtml_get_nodes_by_name(tree, NULL, "a", 1, NULL);
    
    // iterate over all nodes
    for (size_t i = 0; i < collection->length; ++i)
    {
        // add attribute
        myhtml_attribute_add(collection->list[i], "title", 5, "my value", 8, MyENCODING_UTF_8);
    }
    

    // write the document again
    myhtml_serialization_tree_callback(myhtml_tree_get_document(tree), write_output, NULL);
    
    myhtml_collection_destroy(collection);
    myhtml_tree_destroy(tree);
    myhtml_destroy(myhtml);
    
    // done
    return 0;
}
修改前

"<html><head></head><body><a href=http://nu.nl></body></html>

修改后

<html><head></head><body><a href="http://nu.nl" title="my value"></a></body></html>

参考

https://github.com/lexborisov/Modest

GitHub - lexborisov/myhtml: Fast C/C++ HTML 5 Parser. Using threads.


创作不易,小小的支持一下吧!

相关推荐
草履虫建模13 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
naruto_lnq15 小时前
分布式系统安全通信
开发语言·c++·算法
学嵌入式的小杨同学16 小时前
【Linux 封神之路】信号编程全解析:从信号基础到 MP3 播放器实战(含核心 API 与避坑指南)
java·linux·c语言·开发语言·vscode·vim·ux
Re.不晚16 小时前
Java入门17——异常
java·开发语言
精彩极了吧16 小时前
C语言基本语法-自定义类型:结构体&联合体&枚举
c语言·开发语言·枚举·结构体·内存对齐·位段·联合
南极星100517 小时前
蓝桥杯JAVA--启蒙之路(十)class版本 模块
java·开发语言
baidu_2474386117 小时前
Android ViewModel定时任务
android·开发语言·javascript
Dev7z18 小时前
基于 MATLAB 的铣削切削力建模与仿真
开发语言·matlab
不能隔夜的咖喱18 小时前
牛客网刷题(2)
java·开发语言·算法
小天源18 小时前
Error 1053 Error 1067 服务“启动后立即停止” Java / Python 程序无法后台运行 windows nssm注册器下载与报错处理
开发语言·windows·python·nssm·error 1053·error 1067