Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clib.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"which": "0.1.3",
"stephenmathieson/str-flatten.c": "0.0.4",
"commander": "1.3.2",
"stephenmathieson/wiki-registry.c": "0.0.4",
"clibs/wiki-registry.c": "0.1.2",
"stephenmathieson/case.c": "0.1.3",
"jwerle/fs.c": "0.2.0",
"stephenmathieson/str-replace.c": "0.0.6",
Expand Down
4 changes: 2 additions & 2 deletions deps/wiki-registry/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wiki-registry",
"version": "0.0.4",
"repo": "stephenmathieson/wiki-registry.c",
"version": "0.1.2",
"repo": "clibs/wiki-registry.c",
"description": "Turn a GitHub Wiki page into a package registry",
"keywords": [ "registry", "github", "wiki" ],
"license": "MIT",
Expand Down
53 changes: 28 additions & 25 deletions deps/wiki-registry/wiki-registry.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

//
// wiki-registry.c
//
Expand Down Expand Up @@ -96,31 +95,37 @@ wiki_registry_parse(const char *html) {

GumboNode *body = gumbo_get_element_by_id("wiki-body", output->root);
if (body) {
// grab all category `<h2 />`s
list_t *h2s = gumbo_get_elements_by_tag_name("h2", body);
list_node_t *heading_node;
list_iterator_t *heading_iterator = list_iterator_new(h2s, LIST_HEAD);
while ((heading_node = list_iterator_next(heading_iterator))) {
GumboNode *heading = (GumboNode *) heading_node->val;
char *category = gumbo_text_content(heading);
// die if we failed to parse a category, as it's
// almost certinaly a malloc error
if (!category) break;
trim(case_lower(category));
GumboVector *siblings = &heading->parent->v.element.children;
size_t pos = heading->index_within_parent;

// skip elements until the UL
// TODO: don't hardcode position here
// 2:
// 1 - whitespace
// 2 - actual node
GumboNode *ul = siblings->data[pos + 2];
if (GUMBO_TAG_UL != ul->v.element.tag) {
free(category);
GumboNode* markdown_body = ((GumboNode*)((GumboVector)body->v.element.children).data[1]);
GumboVector children = (GumboVector)markdown_body->v.element.children;

size_t count = children.length - 1;

for (size_t index = 0; index < count; index++) {
GumboNode *heading = (GumboNode *)children.data[index];
GumboNode *ul = NULL;

if (heading->v.element.tag != GUMBO_TAG_DIV) {
continue;
}

GumboAttribute *node_id = gumbo_get_attribute(&heading->v.element.attributes, "class");
if (node_id == NULL || strncmp(node_id->value, "markdown-heading", 16) != 0) {
continue;
}

for (; index < count; index++) {
ul = (GumboNode *)children.data[index];

if (ul->v.element.tag == GUMBO_TAG_UL) {
break;
}
}

list_t *h2 = gumbo_get_elements_by_tag_name("h2", heading);
char *category = gumbo_text_content(h2->head->val);
if (!category) break;
trim(case_lower(category));

list_t *lis = gumbo_get_elements_by_tag_name("li", ul);
list_iterator_t *li_iterator = list_iterator_new(lis, LIST_HEAD);
list_node_t *li_node;
Expand All @@ -138,8 +143,6 @@ wiki_registry_parse(const char *html) {
list_destroy(lis);
free(category);
}
list_iterator_destroy(heading_iterator);
list_destroy(h2s);
}

gumbo_destroy_output(&kGumboDefaultOptions, output);
Expand Down
4 changes: 2 additions & 2 deletions test/search-basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
CACHE=$TMPDIR/clib-search.cache
rm -f "$CACHE" 2> /dev/null

N=$(clib search | wc -l)
N=$(./clib-search | wc -l)
# lame check for more than 100 lines of output
[ "$N" -lt 100 ] && {
echo >&2 "Expected \`clib search\` to return at least 100 results"
exit 1
}

TRIM=$(clib search trim)
TRIM=$(./clib-search trim)
case "$TRIM" in
*"stephenmathieson/trim.c"*)
:
Expand Down