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.

138 lines
4.1 KiB

  1. /* markdown: a C implementation of John Gruber's Markdown markup language.
  2. *
  3. * Copyright (C) 2007-2011 David L Parsons.
  4. * The redistribution terms are provided in the COPYRIGHT file that must
  5. * be distributed with this source code.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <limits.h>
  10. #include <unistd.h>
  11. #include <mkdio.h>
  12. #include <errno.h>
  13. #include <string.h>
  14. #include <stdarg.h>
  15. #include "config.h"
  16. #include "amalloc.h"
  17. #if HAVE_LIBGEN_H
  18. #include <libgen.h>
  19. #endif
  20. static struct _opt {
  21. char *name;
  22. char *desc;
  23. int off;
  24. int skip;
  25. int sayenable;
  26. mkd_flag_t flag;
  27. } opts[] = {
  28. { "tabstop", "default (4-space) tabstops", 0, 0, 1, MKD_TABSTOP },
  29. { "image", "images", 1, 0, 1, MKD_NOIMAGE },
  30. { "links", "links", 1, 0, 1, MKD_NOLINKS },
  31. { "relax", "emphasis inside words", 1, 1, 1, MKD_STRICT },
  32. { "strict", "emphasis inside words", 0, 0, 1, MKD_STRICT },
  33. { "tables", "tables", 1, 0, 1, MKD_NOTABLES },
  34. { "header", "pandoc-style headers", 1, 0, 1, MKD_NOHEADER },
  35. { "html", "raw html", 1, 0, 1, MKD_NOHTML },
  36. { "ext", "extended protocols", 1, 0, 1, MKD_NO_EXT },
  37. { "cdata", "generate cdata", 0, 0, 0, MKD_CDATA },
  38. { "smarty", "smartypants", 1, 0, 1, MKD_NOPANTS },
  39. { "pants", "smartypants", 1, 1, 1, MKD_NOPANTS },
  40. { "toc", "tables of contents", 0, 0, 1, MKD_TOC },
  41. { "autolink", "autolinking", 0, 0, 1, MKD_AUTOLINK },
  42. { "safelink", "safe links", 0, 0, 1, MKD_SAFELINK },
  43. { "strikethrough", "strikethrough", 1, 0, 1, MKD_NOSTRIKETHROUGH },
  44. { "del", "strikethrough", 1, 1, 1, MKD_NOSTRIKETHROUGH },
  45. { "superscript", "superscript", 1, 0, 1, MKD_NOSUPERSCRIPT },
  46. { "emphasis", "emphasis inside words", 0, 0, 1, MKD_NORELAXED },
  47. { "divquote", ">%class% blockquotes", 1, 0, 1, MKD_NODIVQUOTE },
  48. { "alphalist", "alpha lists", 1, 0, 1, MKD_NOALPHALIST },
  49. { "definitionlist","definition lists", 1, 0, 1, MKD_NODLIST },
  50. { "1.0", "markdown 1.0 compatibility", 0, 0, 1, MKD_1_COMPAT },
  51. { "footnotes", "markdown extra footnotes", 0, 0, 1, MKD_EXTRA_FOOTNOTE },
  52. { "footnote", "markdown extra footnotes", 0, 1, 1, MKD_EXTRA_FOOTNOTE },
  53. { "style", "extract style blocks", 1, 0, 1, MKD_NOSTYLE },
  54. } ;
  55. #define NR(x) (sizeof x / sizeof x[0])
  56. typedef int (*stfu)(const void *, const void *);
  57. int
  58. sort_by_name(struct _opt *a, struct _opt *b)
  59. {
  60. return strcmp(a->name,b->name);
  61. }
  62. int
  63. sort_by_flag(struct _opt *a, struct _opt *b)
  64. {
  65. return a->flag - b->flag;
  66. }
  67. void
  68. show_flags(int byname)
  69. {
  70. int i;
  71. if ( byname ) {
  72. qsort(opts, NR(opts), sizeof(opts[0]), (stfu)sort_by_name);
  73. for (i=0; i < NR(opts); i++)
  74. if ( ! opts[i].skip )
  75. fprintf(stderr, "%16s : %s\n", opts[i].name, opts[i].desc);
  76. }
  77. else {
  78. qsort(opts, NR(opts), sizeof(opts[0]), (stfu)sort_by_flag);
  79. for (i=0; i < NR(opts); i++)
  80. if ( ! opts[i].skip ) {
  81. fprintf(stderr, "%08lx : ", (long)opts[i].flag);
  82. if ( opts[i].sayenable )
  83. fprintf(stderr, opts[i].off ? "disable " : "enable ");
  84. fprintf(stderr, "%s\n", opts[i].desc);
  85. }
  86. }
  87. }
  88. int
  89. set_flag(mkd_flag_t *flags, char *optionstring)
  90. {
  91. int i;
  92. int enable;
  93. char *arg;
  94. for ( arg = strtok(optionstring, ","); arg; arg = strtok(NULL, ",") ) {
  95. if ( *arg == '+' || *arg == '-' )
  96. enable = (*arg++ == '+') ? 1 : 0;
  97. else if ( strncasecmp(arg, "no", 2) == 0 ) {
  98. arg += 2;
  99. enable = 0;
  100. }
  101. else
  102. enable = 1;
  103. for ( i=0; i < NR(opts); i++ )
  104. if ( strcasecmp(arg, opts[i].name) == 0 )
  105. break;
  106. if ( i < NR(opts) ) {
  107. if ( opts[i].off )
  108. enable = !enable;
  109. if ( enable )
  110. *flags |= opts[i].flag;
  111. else
  112. *flags &= ~opts[i].flag;
  113. }
  114. else
  115. return 0;
  116. }
  117. return 1;
  118. }