Commit 0098709f authored by Hugo Hörnquist's avatar Hugo Hörnquist
Browse files

Cleanup and options.

parent 52180eff
.PHONY: all install
CFLAGS=-Wall $(shell pkg-config --cflags gumbo) $(shell guile-config compile)
CFLAGS=-Wall -pedantic $(shell pkg-config --cflags gumbo) $(shell guile-config compile)
LDLIBS=$(shell pkg-config --libs gumbo) $(shell guile-config link)
GUILE_SITE_DIR=$(shell guile -c "(display (%site-dir))")
......@@ -9,7 +9,7 @@ GUILE_CCACHE_DIR=$(shell guile -c "(display (%site-ccache-dir))")
TARGET=libguile-gumbo.so
DESTDIR=
all: ${TARGET} gumbo.go
all: ${TARGET} html.go
guile-gumbo.o: guile-gumbo.x
......@@ -26,8 +26,8 @@ ${TARGET}: guile-gumbo.o
$(CC) -shared -o $@ -fPIC $^ $(LDLIBS)
install: all
install -m 644 -Dt ${DESTDIR}${GUILE_CCACHE_DIR}/sxml/ gumbo.go
install -m 644 -Dt ${DESTDIR}${GUILE_SITE_DIR}/sxml/ gumbo.scm
install -m 644 -Dt ${DESTDIR}${GUILE_SITE_DIR}/sxml/ html.scm
install -m 644 -Dt ${DESTDIR}${GUILE_CCACHE_DIR}/sxml/ html.go
install -Dt ${DESTDIR}/usr/lib/ libguile-gumbo.so
clean:
......
......@@ -12,5 +12,5 @@ Example:
(html->sxml "Hello")
⇒ (*TOP* (html (head) (body "Hello")))
(html->sxml "<!doctype html><title>Minimum valid document</title>")
⇒ (*TOP* (*PI* "html") (html (head (title "Minimum valid document")) (body)))
⇒ (*TOP* (doctype html) (html (head (title "Minimum valid document")) (body)))
#include <gumbo.h>
#include <libguile.h>
#include <stdbool.h>
typedef struct options {
int trim_whitespace;
bool trim_whitespace;
bool full_document; /* should the *TOP* and doctype nodes be included? */
} options;
/*
......@@ -25,8 +27,8 @@ static SCM scm_parse_children (GumboVector* /* GumboNode*/, options*);
body; \
}
SCM_DEFINE_PUBLIC (scm_parse_html, "parse-html", 1, 0, 0,
(SCM string),
SCM_DEFINE_PUBLIC (scm_parse_html, "parse-html", 2, 0, 0,
(SCM string, SCM options_assq),
"")
#define FUNC_NAME s_scm_parse_html
{
......@@ -35,14 +37,17 @@ SCM_DEFINE_PUBLIC (scm_parse_html, "parse-html", 1, 0, 0,
GumboOutput* output = gumbo_parse(str);
/* TODO this doesn't seem to do anything.
* Also, it should be passed as a parameter
*/
options opts = {
.trim_whitespace = 0
.trim_whitespace = scm_to_bool(scm_assq_ref(options_assq, scm_from_utf8_symbol("trim-whitespace?"))),
.full_document = scm_to_bool(scm_assq_ref(options_assq, scm_from_utf8_symbol("full-document?"))),
};
SCM retval = parse_tree (output->document, &opts);
SCM retval;
if (opts.full_document) {
retval = parse_tree (output->document, &opts);
} else {
retval = parse_tree (output->root, &opts);
}
gumbo_destroy_output(&kGumboDefaultOptions, output);
......@@ -112,8 +117,8 @@ SCM parse_tree (GumboNode* node, options* options) {
SCM document = scm_parse_children (&doc->children, options);
if (doc->has_doctype) {
SCM lst = SCM_EOL;
lst = scm_cons(scm_from_utf8_symbol("*PI*"), lst);
SCM name = scm_from_utf8_stringn(doc->name, strlen(doc->name));
lst = scm_cons(scm_from_utf8_symbol("doctype"), lst);
SCM name = scm_string_to_symbol(scm_from_utf8_stringn(doc->name, strlen(doc->name)));
lst = scm_cons(name, lst);
if (*doc->public_identifier != '\0') {
lst = scm_cons(scm_c_concat("public_identifier=", doc->public_identifier), lst);
......
(define-module (sxml gumbo)
;; rename to allow future wrapper with handling of optional
;; arguments.
#:export ((parse-html . html->sxml)))
(load-extension "libguile-gumbo" "scm_init_gumbo")
(define-module (sxml html)
#:export (html->sxml))
(load-extension "libguile-gumbo" "scm_init_gumbo")
(define* (html->sxml str
#:key
(trim-whitespace? #t)
(full-document? #t))
(parse-html str
`((trim-whitespace? . ,trim-whitespace?)
(full-document? . ,full-document?))))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment