LOWDOWN_DIFF(3) Library Functions Manual LOWDOWN_DIFF(3)

lowdown_diffcompute difference between parsed Markdown trees

library “liblowdown”

#include <sys/queue.h>
#include <stdio.h>
#include <lowdown.h>

struct lowdown_node *
lowdown_diff(const struct lowdown_node *nold, const struct lowdown_node *nnew, size_t *maxn);

Computes the difference between two Markdown trees, the source nold and destination nnew, parsed by lowdown_doc_parse(3). It uses the enum lowdown_chng type in the return tree's nodes to dictate insertions into and deletions from nold. The maxn argument, if not NULL, is set to one greater than the highest node identifier of the returned tree.

Returns a pointer to the difference tree or NULL on memory exhaution. The pointer must be freed with lowdown_node_free(3).

The following parses and compares the NUL-terminated strings old and new. It first allocates the parser, then the document, then the renderer (HTML is used in this case). Then it passes output to the renderer, prints it, and cleans up resources. On any memory errors, it exits with err(3).

struct lowdown_doc *doc;
struct lowdown_node *nold, *nnew, *diff;
struct lowdown_buf *ob;
void *rndr;

doc = lowdown_doc_new(NULL);
if (doc == NULL)
  err(1, NULL);

nold = lowdown_doc_parse(doc, NULL, old, strlen(old));
if (nold == NULL)
  err(1, NULL);
nnew = lowdown_doc_parse(doc, NULL, new, strlen(new));
if (nnew == NULL)
  err(1, NULL);
diff = lowdown_diff(nold, nnew, NULL);
if (diff == NULL)
  err(1, NULL);

rndr = lowdown_html_new(NULL);
if (rndr == NULL)
  err(1, NULL);

ob = lowdown_buf_new(1024);

if (!lowdown_html_rndr(ob, NULL, rndr, diff))
  err(1, NULL);

fwrite(stdout, 1, ob->size, ob->data);

lowdown_buf_free(ob);
lowdown_html_rndr_free(rndr);
lowdown_node_free(nold);
lowdown_node_free(nnew);
lowdown_node_free(diff);
lowdown_doc_free(doc);

lowdown(3)

Gregory Cobena, Serge Abiteboul, and Amelie Marian, Detecting Changes in XML Documents, https://www.cs.rutgers.edu/~amelie/papers/2002/diff.pdf, 2002.

Wu Sun, Manber Udi, and Myers Gene, An O(NP) sequence comparison algorithm, Issue 6, Information Processing Letters, Volume 35, 1990.

January 26, 2021 OpenBSD 6.7