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.

215 lines
4.2 KiB

  1. /*
  2. * markdown: convert a single markdown document into html
  3. */
  4. /*
  5. * Copyright (C) 2007 David L Parsons.
  6. * The redistribution terms are provided in the COPYRIGHT file that must
  7. * be distributed with this source code.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <limits.h>
  12. #include <unistd.h>
  13. #include <mkdio.h>
  14. #include <errno.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17. #include "config.h"
  18. #include "amalloc.h"
  19. #include "pgm_options.h"
  20. #include "tags.h"
  21. #if HAVE_LIBGEN_H
  22. #include <libgen.h>
  23. #endif
  24. #ifndef HAVE_BASENAME
  25. #include <string.h>
  26. char*
  27. basename(char *p)
  28. {
  29. char *ret = strrchr(p, '/');
  30. return ret ? (1+ret) : p;
  31. }
  32. #endif
  33. char *pgm = "markdown";
  34. char *
  35. e_flags(const char *text, const int size, void *context)
  36. {
  37. return (char*)context;
  38. }
  39. void
  40. complain(char *fmt, ...)
  41. {
  42. va_list ptr;
  43. fprintf(stderr, "%s: ", pgm);
  44. va_start(ptr, fmt);
  45. vfprintf(stderr, fmt, ptr);
  46. va_end(ptr);
  47. fputc('\n', stderr);
  48. fflush(stderr);
  49. }
  50. main(int argc, char **argv)
  51. {
  52. int opt;
  53. int rc;
  54. mkd_flag_t flags = 0;
  55. int debug = 0;
  56. int toc = 0;
  57. int content = 1;
  58. int version = 0;
  59. int with_html5 = 0;
  60. int styles = 0;
  61. int use_mkd_line = 0;
  62. int github_flavoured = 0;
  63. char *extra_footnote_prefix = 0;
  64. char *urlflags = 0;
  65. char *text = 0;
  66. char *ofile = 0;
  67. char *urlbase = 0;
  68. char *q;
  69. MMIOT *doc;
  70. if ( q = getenv("MARKDOWN_FLAGS") )
  71. flags = strtol(q, 0, 0);
  72. pgm = basename(argv[0]);
  73. opterr = 1;
  74. while ( (opt=getopt(argc, argv, "5b:C:df:E:F:Gno:s:St:TV")) != EOF ) {
  75. switch (opt) {
  76. case '5': with_html5 = 1;
  77. break;
  78. case 'b': urlbase = optarg;
  79. break;
  80. case 'd': debug = 1;
  81. break;
  82. case 'V': version++;
  83. break;
  84. case 'E': urlflags = optarg;
  85. break;
  86. case 'F': if ( strcmp(optarg, "?") == 0 ) {
  87. show_flags(0);
  88. exit(0);
  89. }
  90. else
  91. flags = strtol(optarg, 0, 0);
  92. break;
  93. case 'f': if ( strcmp(optarg, "?") == 0 ) {
  94. show_flags(1);
  95. exit(0);
  96. }
  97. else if ( !set_flag(&flags, optarg) )
  98. complain("unknown option <%s>", optarg);
  99. break;
  100. case 'G': github_flavoured = 1;
  101. break;
  102. case 'n': content = 0;
  103. break;
  104. case 's': text = optarg;
  105. break;
  106. case 'S': styles = 1;
  107. break;
  108. case 't': text = optarg;
  109. use_mkd_line = 1;
  110. break;
  111. case 'T': toc = 1;
  112. break;
  113. case 'C': extra_footnote_prefix = optarg;
  114. break;
  115. case 'o': if ( ofile ) {
  116. complain("Too many -o options");
  117. exit(1);
  118. }
  119. if ( !freopen(ofile = optarg, "w", stdout) ) {
  120. perror(ofile);
  121. exit(1);
  122. }
  123. break;
  124. default: fprintf(stderr, "usage: %s [-dTV] [-b url-base]"
  125. " [-F bitmap] [-f {+-}flags]"
  126. " [-o ofile] [-s text]"
  127. " [-t text] [file]\n", pgm);
  128. exit(1);
  129. }
  130. }
  131. if ( version ) {
  132. printf("%s: discount %s%s", pgm, markdown_version,
  133. with_html5 ? " +html5":"");
  134. if ( version > 1 )
  135. mkd_flags_are(stdout, flags, 0);
  136. putchar('\n');
  137. exit(0);
  138. }
  139. argc -= optind;
  140. argv += optind;
  141. if ( with_html5 )
  142. mkd_with_html5_tags();
  143. if ( use_mkd_line )
  144. rc = mkd_generateline( text, strlen(text), stdout, flags);
  145. else {
  146. if ( text ) {
  147. doc = github_flavoured ? gfm_string(text, strlen(text), flags)
  148. : mkd_string(text, strlen(text), flags) ;
  149. if ( !doc ) {
  150. perror(text);
  151. exit(1);
  152. }
  153. }
  154. else {
  155. if ( argc && !freopen(argv[0], "r", stdin) ) {
  156. perror(argv[0]);
  157. exit(1);
  158. }
  159. doc = github_flavoured ? gfm_in(stdin,flags) : mkd_in(stdin,flags);
  160. if ( !doc ) {
  161. perror(argc ? argv[0] : "stdin");
  162. exit(1);
  163. }
  164. }
  165. if ( urlbase )
  166. mkd_basename(doc, urlbase);
  167. if ( urlflags ) {
  168. mkd_e_data(doc, urlflags);
  169. mkd_e_flags(doc, e_flags);
  170. }
  171. if ( extra_footnote_prefix )
  172. mkd_ref_prefix(doc, extra_footnote_prefix);
  173. if ( debug )
  174. rc = mkd_dump(doc, stdout, 0, argc ? basename(argv[0]) : "stdin");
  175. else {
  176. rc = 1;
  177. if ( mkd_compile(doc, flags) ) {
  178. rc = 0;
  179. if ( styles )
  180. mkd_generatecss(doc, stdout);
  181. if ( toc )
  182. mkd_generatetoc(doc, stdout);
  183. if ( content )
  184. mkd_generatehtml(doc, stdout);
  185. mkd_cleanup(doc);
  186. }
  187. }
  188. }
  189. mkd_deallocate_tags();
  190. adump();
  191. exit( (rc == 0) ? 0 : errno );
  192. }