Skip to content

Commit

Permalink
Update documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
weetmuts committed Jan 3, 2025
1 parent 4f27de3 commit 419283d
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 181 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ xmq index.html delete //script delete //style br
# List all hyperlinks in a web page.
xmq index.html select //a

# Compose a new web page with all the images from the source html.
# And immediately view it using your default browser.
xmq page.html select //img add-root body add-root html to-html br

# List all href attributes in a web page.
xmq page.html select //a/@href

# Replace entities with strings you can also --with-text-file=abc
# which inserts the content safely quoted, or as DOM --with-file=abc.xml
# where the file has be parseable.
Expand Down
17 changes: 12 additions & 5 deletions src/main/c/xmq-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -2235,13 +2235,20 @@ bool cmd_select(XMQCliCommand *command)
assert(nodes->nodeTab[i]);
xmlNode *n = nodes->nodeTab[i];

while (n && !is_element_node(n) && !is_content_node(n))
if (is_attribute_node(n))
{
// We found an attribute move to parent.
n = n->parent;
xmlAttr *a = (xmlAttr*)n;
// we found an attribute node, eg href="...", rewrite this into
// <href>...</href> which prints nicely as href=... in xmq.
xmlNodePtr new_node = xmlNewDocNode(new_doc, NULL, (xmlChar*)a->name, NULL);
xmlAddChildList(new_node, xmlDocCopyNodeList(new_doc, a->children));
xml_add_root_child(new_doc, new_node);
}
else
{
xmlNodePtr new_node = xmlCopyNode(n, 1);
xml_add_root_child(new_doc, new_node);
}
xmlNodePtr new_node = xmlCopyNode(n, 1);
xml_add_root_child(new_doc, new_node);

xmlUnlinkNode(n);
xmlFreeNode(n);
Expand Down
7 changes: 7 additions & 0 deletions tests/select_001_simple.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
START
<html>
<body>
<a href="https://somewhere.coo">Go there!</a>
<body>
</html>
SELECT //a/@href href=https://somewhere.coo
7 changes: 7 additions & 0 deletions tests/select_002_text.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
START
<html>
<body>
<a href="https://somewhere.coo">Goggogogo</a>
<body>
</html>
SELECT //a/text() 'Goggogogo'
6 changes: 6 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ do
tests/test_backforth.sh "$PROG" "$OUTPUT" "$i"
done

for i in tests/select_???_*.test
do
if [ -n $FILTER ] && [[ ! "$i" =~ $FILTER ]]; then continue; fi
tests/test_select.sh "$PROG" "$OUTPUT" "$i"
done

for i in tests/xslt_???_*.test
do
if [ -n $FILTER ] && [[ ! "$i" =~ $FILTER ]]; then continue; fi
Expand Down
46 changes: 46 additions & 0 deletions tests/test_select.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/sh
# libxmq - Copyright 2023 Fredrik Öhrström (spdx: MIT)

PROG=$1
OUTPUT=$2
TEST_FILE=$3
TEST_NAME=$(basename $3 2> /dev/null)
TEST_NAME=${TEST_NAME%.*}

if [ -z "$OUTPUT" ] || [ -z "$TEST_FILE" ] || [ -z "$PROG" ]
then
echo "Usage: tests/test_selects.sh [XQ_BINARY] [OUTPUT_DIR] tests/select_001_foo.test"
exit 1
fi

if [ ! -f "$TEST_FILE" ]
then
echo "No such test file $TEST_FILE"
exit 1
fi

mkdir -p $OUTPUT

sed -n '/^START.*$/,/^SELECT$/p' $TEST_FILE | tail -n +2 | sed '$d' > $OUTPUT/${TEST_NAME}.input

SELECT=$(grep SELECT $TEST_FILE | cut -b 8- | tr -d '\n')

XPATH=$(echo "$SELECT" | cut -f 1 -d ' ')
echo "$SELECT" | cut -f 2 -d ' ' > $OUTPUT/${TEST_NAME}.expected

$PROG $OUTPUT/${TEST_NAME}.input select "$XPATH" to-xmq --compact > $OUTPUT/${TEST_NAME}.output

if diff $OUTPUT/${TEST_NAME}.expected $OUTPUT/${TEST_NAME}.output > /dev/null
then
echo OK: $TEST_NAME
else
echo ERR: $TEST_NAME
echo "Formatting differ:"
if [ -n "$USE_MELD" ]
then
meld $OUTPUT/${TEST_NAME}.expected $OUTPUT/${TEST_NAME}.output
else
diff $OUTPUT/${TEST_NAME}.expected $OUTPUT/${TEST_NAME}.output
fi
exit 1
fi
Loading

0 comments on commit 419283d

Please sign in to comment.