This Space for Rent

New Code!

Discount has been shoved up to version 1.6.0 with the addition of one new feature; I’ve added support for user callback functions so a program can modify urls and add additional arguments to the tags generated from []() and <link> constructs.

They are not documented well right now (I spun out 1.6.0 from a larger project to remove all the build time feature flags and do all the feature tweaking at runtime) but what you do is write a callback function (char *e_url(char*,int,void*), which is passed a pointer & the side of the url, plus a pointer to (optional) user data) which can return a pointer to a modified url (or null if there is nothing that needs to be changed.) Discount will write that url onto the output, then attempt to free any dynamic memory that was used to create it (via the void e_free(char*,void*) function.)

There is also a char* e_flags(char*,int,void*), which can be used to add additional flags to the generated tags. As an example, one of the companies that’s using discount wants to add rel="nofollow" to generated links. This would be impossible with standard discount, but with callbacks they can write:

char *
nofollow(char *string, int len, void *data)
{
    return "rel=\"nofollow\";
}

.
.
.

mkd_e_flags(Document, nofollow);

And then [foo](bar) will become <a href="bar" rel="nofollow">foo</a>. (you can test this with the markdown program – the -E option creates a simple callback that does what the nofollow() function does here.

More complicated callbacks (ones that actually rewrite urls) can return their new urls or flags in dynamically allocated memory, which discount will then attempt to free with the deallocator callback assigned by the mkd_e_free(MMIOT*,(void*)(free)(char*,void*)) function.

This code is not yet fully documented, because it’s still being revised, but I’m using it now and it hasn’t caused anything to explode, so it’s the New Code! of your dreams. Please let me know if it doesn’t work.