Skip to content

Commit

Permalink
added sg wrapping feature
Browse files Browse the repository at this point in the history
  • Loading branch information
pivotal committed May 28, 2009
1 parent 4a762e2 commit 1825a62
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Expand Up @@ -63,3 +63,4 @@ check-am:
@echo "reddit..."; ./parsley test/reddit.let test/reddit.html 2>&1 | diff test/reddit.json - && echo " success."
@echo "div..."; ./parsley test/div.let test/div.html 2>&1 | diff test/div.json - && echo " success."
@echo "default-namespace..."; ./parsley -x test/default-namespace.let test/default-namespace.xml 2>&1 | diff test/default-namespace.json - && echo " success."
@echo "sg-wrap..."; ./parsley test/sg-wrap.let test/sg-wrap.html 2>&1 | diff test/sg-wrap.json - && echo " success."
1 change: 1 addition & 0 deletions Makefile.in
Expand Up @@ -762,6 +762,7 @@ check-am:
@echo "reddit..."; ./parsley test/reddit.let test/reddit.html 2>&1 | diff test/reddit.json - && echo " success."
@echo "div..."; ./parsley test/div.let test/div.html 2>&1 | diff test/div.json - && echo " success."
@echo "default-namespace..."; ./parsley -x test/default-namespace.let test/default-namespace.xml 2>&1 | diff test/default-namespace.json - && echo " success."
@echo "sg-wrap..."; ./parsley test/sg-wrap.let test/sg-wrap.html 2>&1 | diff test/sg-wrap.json - && echo " success."
# Tell versions [3.59,3.63) of GNU make to not export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT:
6 changes: 3 additions & 3 deletions libtool
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 111.flood.pivotallabs.com:
# Libtool was configured on host cocoa.flood.pivotallabs.com:
# 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,12 +56,12 @@ fast_install=needless

# The host system.
host_alias=
host=i386-apple-darwin9.6.0
host=i686-apple-darwin9.6.0
host_os=darwin9.6.0

# The build system.
build_alias=
build=i386-apple-darwin9.6.0
build=i686-apple-darwin9.6.0
build_os=darwin9.6.0

# A sed program that does not truncate output.
Expand Down
5 changes: 4 additions & 1 deletion parsley.c
Expand Up @@ -382,7 +382,7 @@ visit(parsedParsleyPtr ptr, xmlNodePtr xml, char* err) {

static parsedParsleyPtr current_ptr = NULL;

static void
void
parsleyXsltError(void * ctx, const char * msg, ...) {
if(current_ptr == NULL) return;
va_list ap;
Expand Down Expand Up @@ -449,6 +449,9 @@ parsedParsleyPtr parsley_parse_doc(parsleyPtr parsley, xmlDocPtr doc, int flags)
xsltTransformContextPtr ctxt = xsltNewTransformContext(parsley->stylesheet, doc);
xmlSetGenericErrorFunc(ctxt, parsleyXsltError);
current_ptr = ptr;
if(true) { // TODO: potential performance optimization: only wrap if needed!
doc = parsley_apply_span_wrap(doc);
}
ptr->xml = xsltApplyStylesheetUser(parsley->stylesheet, doc, NULL, NULL, NULL, ctxt);
xsltFreeTransformContext(ctxt);
current_ptr = NULL;
Expand Down
2 changes: 2 additions & 0 deletions parsley.h
Expand Up @@ -77,6 +77,8 @@ parsedParsleyPtr parsley_parse_file(parsleyPtr parsley, char* file, int flags);
parsedParsleyPtr parsley_parse_string(parsleyPtr parsley, char* string, size_t size, char* base_uri, int flags);
parsedParsleyPtr parsley_parse_doc(parsleyPtr, xmlDocPtr, int);

void parsleyXsltError(void * ctx, const char * msg, ...);

void parsley_set_user_agent(char const *agent);
static contextPtr parsley_parsing_context;

Expand Down
2 changes: 1 addition & 1 deletion test/div.json
@@ -1 +1 @@
{ "div": "WebImagesMapsNewsVideoGmailmore »", "a": "Images", "calc1": "Google", "calc2": "2", "calc3": "4", "calc4": "3", "prolly-nan": "NaN" }
{ "div": "Web Images Maps News Video Gmail more »", "a": "Images", "calc1": "Google", "calc2": "2", "calc3": "4", "calc4": "3", "prolly-nan": "NaN" }
5 changes: 5 additions & 0 deletions test/sg-wrap.html
@@ -0,0 +1,5 @@
<html>
<body>
<p><b>Hello</b> world!</p>
</body>
</html>
1 change: 1 addition & 0 deletions test/sg-wrap.json
@@ -0,0 +1 @@
{ "target": " world!" }
3 changes: 3 additions & 0 deletions test/sg-wrap.let
@@ -0,0 +1,3 @@
{
"target": "p sg_wrap"
}
27 changes: 27 additions & 0 deletions util.c
Expand Up @@ -30,6 +30,33 @@ parsley_io_get_mode() {
return parsley_io_mode;
}

static xsltStylesheetPtr span_wrap_sheet = NULL;

xmlDocPtr
parsley_apply_span_wrap(xmlDocPtr doc) {
if(span_wrap_sheet == NULL) {
char * sheet = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:sg=\"http://selectorgadget.com/\"> \
<xsl:template match=\"text()[following-sibling::* or preceding-sibling::*]\"> \
<sg_wrap><xsl:value-of select=\".\" /></sg_wrap> \
</xsl:template> \
<xsl:template match=\"@*|node()\"> \
<xsl:copy> \
<xsl:apply-templates select=\"@*|node()\"/> \
</xsl:copy> \
</xsl:template> \
</xsl:stylesheet>";

xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
xmlDocPtr xml = xmlCtxtReadMemory(ctxt, sheet, strlen(sheet), NULL, NULL, 0);
span_wrap_sheet = xsltParseStylesheetDoc(xml);
}
xsltTransformContextPtr ctxt = xsltNewTransformContext(span_wrap_sheet, doc);
xmlSetGenericErrorFunc(ctxt, parsleyXsltError);
xmlDocPtr out = xsltApplyStylesheetUser(span_wrap_sheet, doc, NULL, NULL, NULL, ctxt);
xsltFreeTransformContext(ctxt);
return out;
}

void
_parsley_set_user_agent(char * agent) {
if(parsley_user_agent_header != NULL) free(parsley_user_agent_header);
Expand Down
2 changes: 2 additions & 0 deletions util.h
Expand Up @@ -29,4 +29,6 @@ int parsley_io_get_mode();
void parsley_io_set_mode(int mode);
void _parsley_set_user_agent(char *agent);

xmlDocPtr parsley_apply_span_wrap(xmlDocPtr ptr);

#endif

0 comments on commit 1825a62

Please sign in to comment.