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.


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

相关推荐
傻乐u兔3 小时前
C语言进阶————指针4
c语言·开发语言
大模型玩家七七3 小时前
基于语义切分 vs 基于结构切分的实际差异
java·开发语言·数据库·安全·batch
历程里程碑3 小时前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
牛奔4 小时前
Go 如何避免频繁抢占?
开发语言·后端·golang
寻星探路8 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
lly2024069 小时前
Bootstrap 警告框
开发语言
2601_9491465310 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧10 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
KYGALYX10 小时前
服务异步通信
开发语言·后端·微服务·ruby
zmzb010310 小时前
C++课后习题训练记录Day98
开发语言·c++