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.


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

相关推荐
思麟呀几秒前
在C++基础上理解Csharp-2
开发语言·jvm·c++·c#
桀人2 分钟前
类和对象——上篇
开发语言·c++
ZC跨境爬虫4 分钟前
跟着 MDN 学 HTML day_57:(HTML 表格进阶特性与无障碍实践)
java·前端·javascript·ui·html·音视频
zzzsde6 分钟前
【Linux】线程概念与控制(3):线程ID&&C++封装线程
linux·运维·服务器·开发语言·算法
老花眼猫11 分钟前
C语言矩形旋转算法介绍
c语言·经验分享·青少年编程·课程设计
消失的旧时光-194312 分钟前
C 语言如何实现“面向对象”?—— 从 struct + 函数指针,到 Linux 内核设计思想
linux·c语言·开发语言
handler0114 分钟前
滑动窗口(同向双指针)算法:模板与例题解析
c语言·c++·笔记·算法·蓝桥杯·双指针·滑动窗口
ZC跨境爬虫26 分钟前
跟着 MDN 学 HTML day_54:(深入掌握 XSLTProcessor API)
前端·javascript·ui·html·媒体
小短腿的代码世界30 分钟前
Qt时间日期处理与QTimer高级应用:从毫秒级精度到跨平台定时器的完整架构解析
开发语言·qt·架构
TAN-90°-35 分钟前
Java 6——成员变量初始值 object equals和== toString instanceof 参数传递问题
java·开发语言