MyHTML 是个快速 HTML 解析器,使用线程来实现一个类似纯 C99 库,无任何外部依赖。

MyHTML 当前版本是 1.0.1,扩展了一个 MyCSS 开源库。MyCSS 是个快速的 CSS 解析器,GitHub 地址:

https://github.com/lexborisov/mycss

MyHTML 主要特性:

扩展库

  • MyCSS — Fast C/C++ CSS Parser (Cascading Style Sheets Parser)

支持的 InputStream 编码

X_USER_DEFINED, UTF_8, UTF_16LE, UTF_16BE, BIG5, EUC_KR, GB18030,
IBM866, ISO_8859_10, ISO_8859_13, ISO_8859_14, ISO_8859_15, ISO_8859_16, ISO_8859_2, ISO_8859_3,
ISO_8859_4, ISO_8859_5, ISO_8859_6, ISO_8859_7, ISO_8859_8, KOI8_R, KOI8_U, MACINTOSH,
WINDOWS_1250, WINDOWS_1251, WINDOWS_1252, WINDOWS_1253, WINDOWS_1254, WINDOWS_1255, WINDOWS_1256,
WINDOWS_1257, WINDOWS_1258, WINDOWS_874, X_MAC_CYRILLIC, ISO_2022_JP, GBK, SHIFT_JIS, EUC_JP, ISO_8859_8_I

支持 UTF-8 编码

可检测的字符编码

UTF-8, UTF-16LE, UTF16BE 和 russian windows-1251, koi8-r, iso-8859-5, x-mac-cyrillic, ibm866

构建和安装

Make

make
  • MyHTML_OPTIMIZATION_LEVEL=-O2 set compiler optimization level. Default: -O2
  • MyHTML_BUILD_WITHOUT_THREADS=YES build without POSIX Threads. Default: NO

示例

make MyHTML_BUILD_WITHOUT_THREADS=NO
cp lib/* /usr/local/lib
cp -r include/* /usr/local/include

CMake

在 myhtml/project 目录:

cmake .
make
sudo make install
  • MyHTML_OPTIMIZATION_LEVEL=-O2 set compiler optimization level. Default: -O2
  • CMAKE_INSTALL_LIBDIR=lib set path to install created library. Default: lib
  • MyHTML_BUILD_SHARED=ON build shared library. Default: ON
  • MyHTML_BUILD_STATIC=ON build static library. Default: ON
  • MyHTML_INSTALL_HEADER=OFF install header files. Default OFF
  • MyHTML_BUILD_WITHOUT_THREADS=YES build without POSIX Threads. Default: NO
  • MyHTML_EXTERN_MALLOC=my_malloc_func set extern malloc function. Default: UNDEFINED
  • MyHTML_EXTERN_REALLOC=my_realloc_func set extern realloc function. Default: UNDEFINED
  • MyHTML_EXTERN_CALLOC=my_calloc_func set extern calloc function. Default: UNDEFINED
  • MyHTML_EXTERN_FREE=my_free_func set extern free function. Default: UNDEFINED

示例

cmake . -DCMAKE_INSTALL_LIBDIR=lib64 -DMyHTML_INSTALL_HEADER=ON

程序构建示例

构建共享库

gcc -Wall -Werror -O2 -lmyhtml your_program.c -o your_program

构建静态库

gcc -Wall -Werror -O2 your_program.c /path/to/libmyhtml_static.a -o your_program

其他语言绑定

简单示例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <myhtml/api.h>

int main(int argc, const char * argv[])
{
    char html[] = "<div><span>HTML</span></div>";

    // basic init
    myhtml_t* myhtml = myhtml_create();
    myhtml_init(myhtml, MyHTML_OPTIONS_DEFAULT, 1, 0);

    // first tree init 
    myhtml_tree_t* tree = myhtml_tree_create();
    myhtml_tree_init(tree, myhtml);

    // parse html
    myhtml_parse(tree, MyHTML_ENCODING_UTF_8, html, strlen(html));

    // release resources
    myhtml_tree_destroy(tree);
    myhtml_destroy(myhtml);

    return 0;
}

MyHTML 遵循 LGPL 开源授权协议,GitHub 地址:https://github.com/lexborisov/myhtml