Skip to content

Commit

Permalink
inner-xml
Browse files Browse the repository at this point in the history
  • Loading branch information
fizx committed Oct 2, 2009
1 parent 678a5ca commit 6e2fba5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 33 deletions.
67 changes: 44 additions & 23 deletions functions.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdbool.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/parserInternals.h>
Expand All @@ -22,33 +23,53 @@ void parsley_register_all(){
xsltHtmlDocumentFunction);
xsltRegisterExtModuleFunction ((const xmlChar *) "outer-xml", "http://parselets.com/stdlib",
xsltOuterXmlFunction);
xsltRegisterExtModuleFunction ((const xmlChar *) "inner-xml", "http://parselets.com/stdlib",
xsltInnerXmlFunction);
}

static void
xsltStarXMLFunction(xmlXPathParserContextPtr ctxt, int nargs, bool is_inner) {
if (nargs != 1) {
xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
"outer-xml() : invalid number of args %d\n",
nargs);
ctxt->error = XPATH_INVALID_ARITY;
return;
}
if (ctxt->value->type == XPATH_NODESET) {
xmlXPathObjectPtr obj, newobj;

obj = valuePop(ctxt);

xmlBufferPtr buf = xmlBufferCreate();
int n = obj->nodesetval->nodeNr;
for(int i = 0; i < n; i++) {
xmlNodePtr node = obj->nodesetval->nodeTab[i];
xmlDocPtr doc = node->doc;
if(is_inner){
xmlNodePtr child = node->children;
while(child != NULL) {
xmlNodeDump(buf, doc, child, 0, 0);
child = child->next;
}
} else {
xmlNodeDump(buf, doc, node, 0, 0);
}
}
newobj = xmlXPathNewCString(xmlBufferContent(buf));
xmlBufferFree(buf);
valuePush(ctxt, newobj);
}
}

void
xsltOuterXmlFunction(xmlXPathParserContextPtr ctxt, int nargs) {
if (nargs != 1) {
xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
"outer-xml() : invalid number of args %d\n",
nargs);
ctxt->error = XPATH_INVALID_ARITY;
return;
}
if (ctxt->value->type == XPATH_NODESET) {
xmlXPathObjectPtr obj, newobj;
xsltInnerXmlFunction(xmlXPathParserContextPtr ctxt, int nargs) {
xsltStarXMLFunction(ctxt, nargs, true);
}

obj = valuePop(ctxt);

xmlBufferPtr buf = xmlBufferCreate();
int n = obj->nodesetval->nodeNr;
for(int i = 0; i < n; i++) {
xmlNodePtr node = obj->nodesetval->nodeTab[i];
xmlDocPtr doc = node->doc;
xmlNodeDump(buf, doc, node, 0, 0);
}
newobj = xmlXPathNewCString(xmlBufferContent(buf));
xmlBufferFree(buf);
valuePush(ctxt, newobj);
}
void
xsltOuterXmlFunction(xmlXPathParserContextPtr ctxt, int nargs) {
xsltStarXMLFunction(ctxt, nargs, false);
}

void
Expand Down
1 change: 1 addition & 0 deletions functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void parsley_register_all();

static void xsltHtmlDocumentFunction(xmlXPathParserContextPtr, int);
static void xsltHtmlDocumentFunctionLoadDocument(xmlXPathParserContextPtr, xmlChar*);
static void xsltInnerXmlFunction(xmlXPathParserContextPtr ctxt, int nargs);
static void xsltOuterXmlFunction(xmlXPathParserContextPtr ctxt, int nargs);
xsltDocumentPtr xsltLoadHtmlDocument(xsltTransformContextPtr, const xmlChar *);

Expand Down
16 changes: 8 additions & 8 deletions libtool
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# libtool - Provide generalized library-building support services.
# Generated automatically by config.status (parsleyc) 1.0
# Libtool was configured on host kyles-macbook.local:
# Libtool was configured on host kyles-macbook:
# NOTE: Changes made to this file will be lost: look at ltmain.sh.
#
# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005,
Expand Down Expand Up @@ -56,13 +56,13 @@ fast_install=needless

# The host system.
host_alias=
host=i386-apple-darwin9.8.0
host_os=darwin9.8.0
host=i386-apple-darwin10.0.0
host_os=darwin10.0.0

# The build system.
build_alias=
build=i386-apple-darwin9.8.0
build_os=darwin9.8.0
build=i386-apple-darwin10.0.0
build_os=darwin10.0.0

# A sed program that does not truncate output.
SED="/usr/bin/sed"
Expand All @@ -80,7 +80,7 @@ EGREP="/usr/bin/grep -E"
FGREP="/usr/bin/grep -F"

# A BSD- or MS-compatible name lister.
NM="/usr/bin/nm -p"
NM="/usr/bin/nm"

# Whether we need soft or hard links.
LN_S="ln -s"
Expand Down Expand Up @@ -234,7 +234,7 @@ finish_eval=""
hardcode_into_libs=no

# Compile-time system search path for libraries.
sys_lib_search_path_spec="/usr/lib/i686-apple-darwin9/4.0.1 /usr/lib /usr/lib/gcc/i686-apple-darwin9/4.0.1 /usr/local/lib"
sys_lib_search_path_spec="/usr/lib/gcc/i686-apple-darwin10/4.2.1/x86_64 /usr/lib/i686-apple-darwin10/4.2.1 /usr/lib /usr/local/lib"

# Run-time system search path for libraries.
sys_lib_dlsearch_path_spec="/usr/local/lib /lib /usr/lib"
Expand All @@ -254,7 +254,7 @@ striplib="strip -x"


# The linker used to build libraries.
LD="/usr/libexec/gcc/i686-apple-darwin9/4.0.1/ld"
LD="/usr/libexec/gcc/i686-apple-darwin10/4.2.1/ld"

# Commands used to build an old-style archive.
old_archive_cmds="\$AR \$AR_FLAGS \$oldlib\$oldobjs~\$RANLIB \$oldlib"
Expand Down
2 changes: 1 addition & 1 deletion test/outer-xml.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "foo": "<p id=\"foo\">bar<\/p>", "both": [ "<p id=\"foo\">bar<\/p>", "<p id=\"foo\">baz<\/p>" ] }
{ "foo": "<p id=\"foo\">bar<\/p>", "both": [ "<p id=\"foo\">bar<\/p>", "<p id=\"foo\">baz<\/p>" ], "inner": "<p id=\"foo\">bar<\/p><p id=\"foo\">baz<\/p>" }
3 changes: 2 additions & 1 deletion test/outer-xml.let
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"foo": "lib:outer-xml(p)",
"both": [ "lib:outer-xml(p)" ]
"both": [ "lib:outer-xml(p)" ],
"inner": "lib:inner-xml(body)"
}

0 comments on commit 6e2fba5

Please sign in to comment.