tags.c 1.95 KB
Newer Older
1 2
/* block-level tags for passing html blocks through the blender
 */
3 4
#include "config.h"

5 6 7 8
#define __WITHOUT_AMALLOC 1
#include "cstring.h"
#include "tags.h"

9 10 11 12 13 14 15 16 17
STRING(struct kw) extratags;

/* the standard collection of tags are built and sorted when
 * discount is configured, so all we need to do is pull them
 * in and use them.
 *
 * Additional tags still need to be allocated, sorted, and deallocated.
 */
#include "blocktags"
18 19


20
/* define an additional html block tag
21 22 23 24
 */
void
mkd_define_tag(char *id, int selfclose)
{
25 26 27 28 29 30
    struct kw *p;

    /* only add the new tag if it doesn't exist in
     * either the standard or extra tag tables.
     */
    if ( !(p = mkd_search_tags(id, strlen(id))) ) {
31 32 33
	/* extratags could be deallocated */
	if ( S(extratags) == 0 )
	    CREATE(extratags);
34 35 36 37 38
	p = &EXPAND(extratags);
	p->id = id;
	p->size = strlen(id);
	p->selfclose = selfclose;
    }
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
}


/* case insensitive string sort (for qsort() and bsearch() of block tags)
 */
static int
casort(struct kw *a, struct kw *b)
{
    if ( a->size != b->size )
	return a->size - b->size;
    return strncasecmp(a->id, b->id, b->size);
}


/* stupid cast to make gcc shut up about the function types being
 * passed into qsort() and bsearch()
 */
typedef int (*stfu)(const void*,const void*);


59
/* sort the list of extra html block tags for later searching
60 61 62 63
 */
void
mkd_sort_tags()
{
64
    qsort(T(extratags), S(extratags), sizeof(struct kw), (stfu)casort);
65 66 67 68 69 70 71 72 73
}


/* look for a token in the html block tag list
 */
struct kw*
mkd_search_tags(char *pat, int len)
{
    struct kw key;
74
    struct kw *ret;
75 76 77 78
    
    key.id = pat;
    key.size = len;
    
79 80
    if ( (ret=bsearch(&key,blocktags,NR_blocktags,sizeof key,(stfu)casort)) )
	return ret;
81

82 83 84 85 86
    if ( S(extratags) )
	return bsearch(&key,T(extratags),S(extratags),sizeof key,(stfu)casort);
    
    return 0;
}
87 88


89
/* destroy the extratags list (for shared libraries)
90 91 92 93
 */
void
mkd_deallocate_tags()
{
94 95
    if ( S(extratags) > 0 )
	DELETE(extratags);
96
} /* mkd_deallocate_tags */