main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #if HAVE_LIBGEN_H
  21. #include <libgen.h>
  22. #endif
  23. #ifndef HAVE_BASENAME
  24. #include <string.h>
  25. char*
  26. basename(char *p)
  27. {
  28. char *ret = strrchr(p, '/');
  29. return ret ? (1+ret) : p;
  30. }
  31. #endif
  32. char *pgm = "markdown";
  33. char *
  34. e_flags(const char *text, const int size, void *context)
  35. {
  36. return (char*)context;
  37. }
  38. void
  39. complain(char *fmt, ...)
  40. {
  41. va_list ptr;
  42. fprintf(stderr, "%s: ", pgm);
  43. va_start(ptr, fmt);
  44. vfprintf(stderr, fmt, ptr);
  45. va_end(ptr);
  46. fputc('\n', stderr);
  47. fflush(stderr);
  48. }
  49. static mkd_qstring_t old_qstring;
  50. static void
  51. MyQstring(char *s, MMIOT *f)
  52. {
  53. if(strcmp(s, "<table>\n") == 0) s = "<table border='1'>\n";
  54. else if(strcmp(s, "<p>") == 0) s = "<p class='pc'>";
  55. old_qstring(s,f);
  56. }
  57. int
  58. main(int argc, char **argv)
  59. {
  60. int opt;
  61. int rc;
  62. mkd_flag_t flags = 0;
  63. int debug = 0;
  64. int toc = 0;
  65. int version = 0;
  66. int with_html5 = 0;
  67. int use_mkd_line = 0;
  68. char *extra_footnote_prefix = 0;
  69. char *urlflags = 0;
  70. char *text = 0;
  71. char *ofile = 0;
  72. char *urlbase = 0;
  73. char *q;
  74. MMIOT *doc;
  75. if ( q = getenv("MARKDOWN_FLAGS") )
  76. flags = strtol(q, 0, 0);
  77. pgm = basename(argv[0]);
  78. opterr = 1;
  79. while ( (opt=getopt(argc, argv, "5b:C:df:E:F:o:s:t:TV")) != EOF ) {
  80. switch (opt) {
  81. case '5': with_html5 = 1;
  82. break;
  83. case 'b': urlbase = optarg;
  84. break;
  85. case 'd': debug = 1;
  86. break;
  87. case 'V': version++;
  88. break;
  89. case 'E': urlflags = optarg;
  90. break;
  91. case 'F': if ( strcmp(optarg, "?") == 0 ) {
  92. show_flags(0);
  93. exit(0);
  94. }
  95. else
  96. flags = strtol(optarg, 0, 0);
  97. break;
  98. case 'f': if ( strcmp(optarg, "?") == 0 ) {
  99. show_flags(1);
  100. exit(0);
  101. }
  102. else if ( !set_flag(&flags, optarg) )
  103. complain("unknown option <%s>", optarg);
  104. break;
  105. case 't': text = optarg;
  106. use_mkd_line = 1;
  107. break;
  108. case 'T': toc = 1;
  109. break;
  110. case 's': text = optarg;
  111. break;
  112. case 'C': extra_footnote_prefix = optarg;
  113. break;
  114. case 'o': if ( ofile ) {
  115. complain("Too many -o options");
  116. exit(1);
  117. }
  118. if ( !freopen(ofile = optarg, "w", stdout) ) {
  119. perror(ofile);
  120. exit(1);
  121. }
  122. break;
  123. default: fprintf(stderr, "usage: %s [-dTV] [-b url-base]"
  124. " [-F bitmap] [-f {+-}flags]"
  125. " [-o ofile] [-s text]"
  126. " [-t text] [file]\n", pgm);
  127. exit(1);
  128. }
  129. }
  130. if ( version ) {
  131. printf("%s: discount %s%s", pgm, markdown_version,
  132. with_html5 ? " +html5":"");
  133. if ( version > 1 )
  134. mkd_flags_are(stdout, flags, 0);
  135. putchar('\n');
  136. exit(0);
  137. }
  138. argc -= optind;
  139. argv += optind;
  140. old_qstring = mkd_e_qstring(MyQstring);
  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. if ( (doc = mkd_string(text, strlen(text), flags)) == 0 ) {
  148. perror(text);
  149. exit(1);
  150. }
  151. }
  152. else {
  153. if ( argc && !freopen(argv[0], "r", stdin) ) {
  154. perror(argv[0]);
  155. exit(1);
  156. }
  157. if ( (doc = mkd_in(stdin,flags)) == 0 ) {
  158. perror(argc ? argv[0] : "stdin");
  159. exit(1);
  160. }
  161. }
  162. if ( urlbase )
  163. mkd_basename(doc, urlbase);
  164. if ( urlflags ) {
  165. mkd_e_data(doc, urlflags);
  166. mkd_e_flags(doc, e_flags);
  167. }
  168. if ( extra_footnote_prefix )
  169. mkd_ref_prefix(doc, extra_footnote_prefix);
  170. if ( debug )
  171. rc = mkd_dump(doc, stdout, 0, argc ? basename(argv[0]) : "stdin");
  172. else {
  173. rc = 1;
  174. if ( mkd_compile(doc, flags) ) {
  175. rc = 0;
  176. if ( toc )
  177. mkd_generatetoc(doc, stdout);
  178. mkd_generatehtml(doc, stdout);
  179. mkd_cleanup(doc);
  180. }
  181. }
  182. }
  183. mkd_deallocate_tags();
  184. adump();
  185. exit( (rc == 0) ? 0 : errno );
  186. }