forked from xavi/libweb
Add HTML serializer example
This commit is contained in:
parent
07c59a83d1
commit
70670e129e
|
@ -2,3 +2,4 @@
|
|||
*.a
|
||||
*.d
|
||||
examples/hello/hello
|
||||
examples/html/html
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
cmake_minimum_required(VERSION 3.13)
|
||||
add_subdirectory(hello)
|
||||
add_subdirectory(html)
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
cmake_minimum_required(VERSION 3.13)
|
||||
project(html C)
|
||||
add_executable(html main.c)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE slweb dynstr)
|
|
@ -0,0 +1,26 @@
|
|||
.POSIX:
|
||||
|
||||
PROJECT = html
|
||||
DEPS = \
|
||||
main.o
|
||||
SLWEB = ../../libslweb.a
|
||||
DYNSTR = ../../dynstr/libdynstr.a
|
||||
CFLAGS = -I ../../include -I ../../dynstr/include
|
||||
SLWEB_FLAGS = -L ../../ -l slweb
|
||||
DYNSTR_FLAGS = -L ../../dynstr -l dynstr
|
||||
|
||||
all: $(PROJECT)
|
||||
|
||||
clean:
|
||||
rm -f $(DEPS)
|
||||
|
||||
FORCE:
|
||||
|
||||
$(PROJECT): $(DEPS) $(SLWEB) $(DYNSTR)
|
||||
$(CC) $(LDFLAGS) $(DEPS) $(SLWEB_FLAGS) $(DYNSTR_FLAGS) -o $@
|
||||
|
||||
$(SLWEB): FORCE
|
||||
+cd ../../ && $(MAKE)
|
||||
|
||||
$(DYNSTR): FORCE
|
||||
+cd ../../dynstr && $(MAKE)
|
|
@ -0,0 +1,25 @@
|
|||
# HTML serializer example
|
||||
|
||||
This example shows a minimal application that serializes a HTML file
|
||||
and writes it to standard output.
|
||||
|
||||
## How to build
|
||||
|
||||
If using `make(1)`, just run `make` from this directory.
|
||||
|
||||
If using CMake, examples are built by default when configuring the project
|
||||
from [the top-level `CMakeLists.txt`](../../CMakeLists.txt).
|
||||
|
||||
## How to run
|
||||
|
||||
Run the executable without any command line arguments. The following text
|
||||
should be written to the standard output:
|
||||
|
||||
```
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
</head>
|
||||
<body>testing slweb</body>
|
||||
</html>
|
||||
```
|
|
@ -0,0 +1,59 @@
|
|||
#include <slweb/html.h>
|
||||
#include <dynstr.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int ret = EXIT_FAILURE;
|
||||
struct dynstr d;
|
||||
struct html_node *const html = html_node_alloc("html"), *head,
|
||||
*meta, *body;
|
||||
static const char text[] = "testing slweb";
|
||||
|
||||
dynstr_init(&d);
|
||||
|
||||
if (!html)
|
||||
{
|
||||
fprintf(stderr, "html_node_alloc_failed\n");
|
||||
goto end;
|
||||
}
|
||||
else if (!(head = html_node_add_child(html, "head")))
|
||||
{
|
||||
fprintf(stderr, "html_node_add_child head failed\n");
|
||||
goto end;
|
||||
}
|
||||
else if (!(meta = html_node_add_child(head, "meta")))
|
||||
{
|
||||
fprintf(stderr, "html_node_add_child meta failed\n");
|
||||
goto end;
|
||||
}
|
||||
else if (html_node_add_attr(meta, "charset", "UTF-8"))
|
||||
{
|
||||
fprintf(stderr, "%s: html_node_add_attr charset failed\n", __func__);
|
||||
goto end;
|
||||
}
|
||||
else if (!(body = html_node_add_child(html, "body")))
|
||||
{
|
||||
fprintf(stderr, "html_node_add_child failed\n");
|
||||
goto end;
|
||||
}
|
||||
else if (html_node_set_value(body, text))
|
||||
{
|
||||
fprintf(stderr, "html_node_set_value failed\n");
|
||||
goto end;
|
||||
}
|
||||
else if (html_serialize(html, &d))
|
||||
{
|
||||
fprintf(stderr, "html_serialize failed\n");
|
||||
goto end;
|
||||
}
|
||||
|
||||
printf("%s", d.str);
|
||||
ret = EXIT_SUCCESS;
|
||||
|
||||
end:
|
||||
dynstr_free(&d);
|
||||
html_node_free(html);
|
||||
return ret;
|
||||
}
|
Loading…
Reference in New Issue