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.

94 lines
1.9 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. /* extratags could be deallocated */
  25. if ( S(extratags) == 0 )
  26. CREATE(extratags);
  27. p = &EXPAND(extratags);
  28. p->id = id;
  29. p->size = strlen(id);
  30. p->selfclose = selfclose;
  31. }
  32. }
  33. /* case insensitive string sort (for qsort() and bsearch() of block tags)
  34. */
  35. static int
  36. casort(struct kw *a, struct kw *b)
  37. {
  38. if ( a->size != b->size )
  39. return a->size - b->size;
  40. return strncasecmp(a->id, b->id, b->size);
  41. }
  42. /* stupid cast to make gcc shut up about the function types being
  43. * passed into qsort() and bsearch()
  44. */
  45. typedef int (*stfu)(const void*,const void*);
  46. /* sort the list of extra html block tags for later searching
  47. */
  48. void
  49. mkd_sort_tags()
  50. {
  51. qsort(T(extratags), S(extratags), sizeof(struct kw), (stfu)casort);
  52. }
  53. /* look for a token in the html block tag list
  54. */
  55. struct kw*
  56. mkd_search_tags(char *pat, int len)
  57. {
  58. struct kw key;
  59. struct kw *ret;
  60. key.id = pat;
  61. key.size = len;
  62. if ( (ret=bsearch(&key,blocktags,NR_blocktags,sizeof key,(stfu)casort)) )
  63. return ret;
  64. if ( S(extratags) )
  65. return bsearch(&key,T(extratags),S(extratags),sizeof key,(stfu)casort);
  66. return 0;
  67. }
  68. /* destroy the extratags list (for shared libraries)
  69. */
  70. void
  71. mkd_deallocate_tags()
  72. {
  73. if ( S(extratags) > 0 )
  74. DELETE(extratags);
  75. } /* mkd_deallocate_tags */