Text::Markdown::Discount
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
1.8 KiB

  1. /* block-level tags for passing html blocks through the blender
  2. */
  3. #define __WITHOUT_AMALLOC 1
  4. #include "cstring.h"
  5. #include "tags.h"
  6. STRING(struct kw) extratags;
  7. /* the standard collection of tags are built and sorted when
  8. * discount is configured, so all we need to do is pull them
  9. * in and use them.
  10. *
  11. * Additional tags still need to be allocated, sorted, and deallocated.
  12. */
  13. #include "blocktags"
  14. /* define an additional html block tag
  15. */
  16. void
  17. mkd_define_tag(char *id, int selfclose)
  18. {
  19. struct kw *p;
  20. /* only add the new tag if it doesn't exist in
  21. * either the standard or extra tag tables.
  22. */
  23. if ( !(p = mkd_search_tags(id, strlen(id))) ) {
  24. p = &EXPAND(extratags);
  25. p->id = id;
  26. p->size = strlen(id);
  27. p->selfclose = selfclose;
  28. }
  29. }
  30. /* case insensitive string sort (for qsort() and bsearch() of block tags)
  31. */
  32. static int
  33. casort(struct kw *a, struct kw *b)
  34. {
  35. if ( a->size != b->size )
  36. return a->size - b->size;
  37. return strncasecmp(a->id, b->id, b->size);
  38. }
  39. /* stupid cast to make gcc shut up about the function types being
  40. * passed into qsort() and bsearch()
  41. */
  42. typedef int (*stfu)(const void*,const void*);
  43. /* sort the list of extra html block tags for later searching
  44. */
  45. void
  46. mkd_sort_tags()
  47. {
  48. qsort(T(extratags), S(extratags), sizeof(struct kw), (stfu)casort);
  49. }
  50. /* look for a token in the html block tag list
  51. */
  52. struct kw*
  53. mkd_search_tags(char *pat, int len)
  54. {
  55. struct kw key;
  56. struct kw *ret;
  57. key.id = pat;
  58. key.size = len;
  59. if ( (ret=bsearch(&key,blocktags,NR_blocktags,sizeof key,(stfu)casort)) )
  60. return ret;
  61. if ( S(extratags) )
  62. return bsearch(&key,T(extratags),S(extratags),sizeof key,(stfu)casort);
  63. return 0;
  64. }
  65. /* destroy the extratags list (for shared libraries)
  66. */
  67. void
  68. mkd_deallocate_tags()
  69. {
  70. if ( S(extratags) > 0 )
  71. DELETE(extratags);
  72. } /* mkd_deallocate_tags */