mkd2html.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * mkd2html: parse a markdown input file and generate a web page.
  3. *
  4. * usage: mkd2html [options] filename
  5. * or mkd2html [options] < markdown > html
  6. *
  7. * options
  8. * -css css-file
  9. * -header line-to-add-to-<HEADER>
  10. * -footer line-to-add-before-</BODY>
  11. *
  12. * example:
  13. *
  14. * mkd2html -cs /~orc/pages.css syntax
  15. * ( read syntax OR syntax.text, write syntax.html )
  16. */
  17. /*
  18. * Copyright (C) 2007 David L Parsons.
  19. * The redistribution terms are provided in the COPYRIGHT file that must
  20. * be distributed with this source code.
  21. */
  22. #include "config.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #ifdef HAVE_BASENAME
  27. # ifdef HAVE_LIBGEN_H
  28. # include <libgen.h>
  29. # else
  30. # include <unistd.h>
  31. # endif
  32. #endif
  33. #include <stdarg.h>
  34. #include "mkdio.h"
  35. #include "cstring.h"
  36. #include "amalloc.h"
  37. char *pgm = "mkd2html";
  38. #ifndef HAVE_BASENAME
  39. char *
  40. basename(char *path)
  41. {
  42. char *p;
  43. if ( p = strrchr(path, '/') )
  44. return 1+p;
  45. return path;
  46. }
  47. #endif
  48. void
  49. fail(char *why, ...)
  50. {
  51. va_list ptr;
  52. va_start(ptr,why);
  53. fprintf(stderr, "%s: ", pgm);
  54. vfprintf(stderr, why, ptr);
  55. fputc('\n', stderr);
  56. va_end(ptr);
  57. exit(1);
  58. }
  59. void
  60. main(argc, argv)
  61. char **argv;
  62. {
  63. char *h;
  64. char *source = 0, *dest = 0;
  65. MMIOT *mmiot;
  66. int i;
  67. FILE *input, *output;
  68. STRING(char*) css, headers, footers;
  69. CREATE(css);
  70. CREATE(headers);
  71. CREATE(footers);
  72. pgm = basename(argv[0]);
  73. while ( argc > 1 ) {
  74. if ( strcmp(argv[1], "-css") == 0 ) {
  75. EXPAND(css) = argv[2];
  76. argc -= 2;
  77. argv += 2;
  78. }
  79. else if ( strcmp(argv[1], "-header") == 0 ) {
  80. EXPAND(headers) = argv[2];
  81. argc -= 2;
  82. argv += 2;
  83. }
  84. else if ( strcmp(argv[1], "-footer") == 0 ) {
  85. EXPAND(footers) = argv[2];
  86. argc -= 2;
  87. argv += 2;
  88. }
  89. else
  90. break;
  91. }
  92. switch ( argc ) {
  93. char *p, *dot;
  94. case 1:
  95. input = stdin;
  96. output = stdout;
  97. break;
  98. case 2:
  99. case 3:
  100. dest = malloc(strlen(argv[argc-1]) + 6);
  101. source = malloc(strlen(argv[1]) + 6);
  102. if ( !(source && dest) )
  103. fail("out of memory allocating name buffers");
  104. strcpy(source, argv[1]);
  105. if (( p = strrchr(source, '/') ))
  106. p = source;
  107. else
  108. ++p;
  109. if ( (input = fopen(source, "r")) == 0 ) {
  110. strcat(source, ".text");
  111. if ( (input = fopen(source, "r")) == 0 )
  112. fail("can't open either %s or %s", argv[1], source);
  113. }
  114. strcpy(dest, source);
  115. if (( dot = strrchr(dest, '.') ))
  116. *dot = 0;
  117. strcat(dest, ".html");
  118. if ( (output = fopen(dest, "w")) == 0 )
  119. fail("can't write to %s", dest);
  120. break;
  121. default:
  122. fprintf(stderr, "usage: %s [opts] source [dest]\n", pgm);
  123. exit(1);
  124. }
  125. if ( (mmiot = mkd_in(input, 0)) == 0 )
  126. fail("can't read %s", source ? source : "stdin");
  127. if ( !mkd_compile(mmiot, 0) )
  128. fail("couldn't compile input");
  129. h = mkd_doc_title(mmiot);
  130. /* print a header */
  131. fprintf(output,
  132. "<!doctype html public \"-//W3C//DTD HTML 4.0 Transitional //EN\">\n"
  133. "<html>\n"
  134. "<head>\n"
  135. " <meta name=\"GENERATOR\" content=\"mkd2html %s\">\n", markdown_version);
  136. fprintf(output," <meta http-equiv=\"Content-Type\"\n"
  137. " content=\"text/html; charset=utf-8\">");
  138. for ( i=0; i < S(css); i++ )
  139. fprintf(output, " <link rel=\"stylesheet\"\n"
  140. " type=\"text/css\"\n"
  141. " href=\"%s\" />\n", T(css)[i]);
  142. if ( h ) {
  143. fprintf(output," <title>");
  144. mkd_generateline(h, strlen(h), output, 0);
  145. fprintf(output, "</title>\n");
  146. }
  147. for ( i=0; i < S(headers); i++ )
  148. fprintf(output, " %s\n", T(headers)[i]);
  149. fprintf(output, "</head>\n"
  150. "<body>\n");
  151. /* print the compiled body */
  152. mkd_generatehtml(mmiot, output);
  153. for ( i=0; i < S(footers); i++ )
  154. fprintf(output, "%s\n", T(footers)[i]);
  155. fprintf(output, "</body>\n"
  156. "</html>\n");
  157. mkd_cleanup(mmiot);
  158. exit(0);
  159. }