main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. static mkd_qstring_t old_qstring;
  51. static void
  52. MyQstring(char *s, MMIOT *f)
  53. {
  54. if(strcmp(s, "<table>\n") == 0) s = "<table border='1'>\n";
  55. else if(strcmp(s, "<p>") == 0) s = "<p class='pc'>";
  56. old_qstring(s,f);
  57. }
  58. int
  59. main(int argc, char **argv)
  60. {
  61. int opt;
  62. int rc;
  63. mkd_flag_t flags = 0;
  64. int debug = 0;
  65. int toc = 0;
  66. int content = 1;
  67. int version = 0;
  68. int with_html5 = 0;
  69. int styles = 0;
  70. int use_mkd_line = 0;
  71. int github_flavoured = 0;
  72. char *extra_footnote_prefix = 0;
  73. char *urlflags = 0;
  74. char *text = 0;
  75. char *ofile = 0;
  76. char *urlbase = 0;
  77. char *q;
  78. MMIOT *doc;
  79. if ( q = getenv("MARKDOWN_FLAGS") )
  80. flags = strtol(q, 0, 0);
  81. pgm = basename(argv[0]);
  82. opterr = 1;
  83. while ( (opt=getopt(argc, argv, "5b:C:df:E:F:Gno:s:St:TV")) != EOF ) {
  84. switch (opt) {
  85. case '5': with_html5 = 1;
  86. break;
  87. case 'b': urlbase = optarg;
  88. break;
  89. case 'd': debug = 1;
  90. break;
  91. case 'V': version++;
  92. break;
  93. case 'E': urlflags = optarg;
  94. break;
  95. case 'F': if ( strcmp(optarg, "?") == 0 ) {
  96. show_flags(0);
  97. exit(0);
  98. }
  99. else
  100. flags = strtol(optarg, 0, 0);
  101. break;
  102. case 'f': if ( strcmp(optarg, "?") == 0 ) {
  103. show_flags(1);
  104. exit(0);
  105. }
  106. else if ( !set_flag(&flags, optarg) )
  107. complain("unknown option <%s>", optarg);
  108. break;
  109. case 'G': github_flavoured = 1;
  110. break;
  111. case 'n': content = 0;
  112. break;
  113. case 's': text = optarg;
  114. break;
  115. case 'S': styles = 1;
  116. break;
  117. case 't': text = optarg;
  118. use_mkd_line = 1;
  119. break;
  120. case 'T': toc = 1;
  121. break;
  122. case 'C': extra_footnote_prefix = optarg;
  123. break;
  124. case 'o': if ( ofile ) {
  125. complain("Too many -o options");
  126. exit(1);
  127. }
  128. if ( !freopen(ofile = optarg, "w", stdout) ) {
  129. perror(ofile);
  130. exit(1);
  131. }
  132. break;
  133. default: fprintf(stderr, "usage: %s [-dTV] [-b url-base]"
  134. " [-F bitmap] [-f {+-}flags]"
  135. " [-o ofile] [-s text]"
  136. " [-t text] [file]\n", pgm);
  137. exit(1);
  138. }
  139. }
  140. if ( version ) {
  141. printf("%s: discount %s%s", pgm, markdown_version,
  142. with_html5 ? " +html5":"");
  143. if ( version > 1 )
  144. mkd_flags_are(stdout, flags, 0);
  145. putchar('\n');
  146. exit(0);
  147. }
  148. argc -= optind;
  149. argv += optind;
  150. old_qstring = mkd_e_qstring(MyQstring);
  151. if ( with_html5 )
  152. mkd_with_html5_tags();
  153. if ( use_mkd_line )
  154. rc = mkd_generateline( text, strlen(text), stdout, flags);
  155. else {
  156. if ( text ) {
  157. doc = github_flavoured ? gfm_string(text, strlen(text), flags)
  158. : mkd_string(text, strlen(text), flags) ;
  159. if ( !doc ) {
  160. perror(text);
  161. exit(1);
  162. }
  163. }
  164. else {
  165. if ( argc && !freopen(argv[0], "r", stdin) ) {
  166. perror(argv[0]);
  167. exit(1);
  168. }
  169. doc = github_flavoured ? gfm_in(stdin,flags) : mkd_in(stdin,flags);
  170. if ( !doc ) {
  171. perror(argc ? argv[0] : "stdin");
  172. exit(1);
  173. }
  174. }
  175. if ( urlbase )
  176. mkd_basename(doc, urlbase);
  177. if ( urlflags ) {
  178. mkd_e_data(doc, urlflags);
  179. mkd_e_flags(doc, e_flags);
  180. }
  181. if ( extra_footnote_prefix )
  182. mkd_ref_prefix(doc, extra_footnote_prefix);
  183. if ( debug )
  184. rc = mkd_dump(doc, stdout, 0, argc ? basename(argv[0]) : "stdin");
  185. else {
  186. rc = 1;
  187. if ( mkd_compile(doc, flags) ) {
  188. rc = 0;
  189. if ( styles )
  190. mkd_generatecss(doc, stdout);
  191. if ( toc )
  192. mkd_generatetoc(doc, stdout);
  193. if ( content )
  194. mkd_generatehtml(doc, stdout);
  195. }
  196. }
  197. mkd_cleanup(doc);
  198. }
  199. mkd_deallocate_tags();
  200. adump();
  201. exit( (rc == 0) ? 0 : errno );
  202. }