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.

191 lines
3.8 KiB

  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. main(argc, argv)
  60. char **argv;
  61. {
  62. char *h;
  63. char *source = 0, *dest = 0;
  64. MMIOT *mmiot;
  65. int i;
  66. FILE *input, *output;
  67. STRING(char*) css, headers, footers;
  68. CREATE(css);
  69. CREATE(headers);
  70. CREATE(footers);
  71. pgm = basename(argv[0]);
  72. while ( argc ) {
  73. if ( strcmp(argv[1], "-css") == 0 ) {
  74. EXPAND(css) = argv[2];
  75. argc -= 2;
  76. argv += 2;
  77. }
  78. else if ( strcmp(argv[1], "-header") == 0 ) {
  79. EXPAND(headers) = argv[2];
  80. argc -= 2;
  81. argv += 2;
  82. }
  83. else if ( strcmp(argv[1], "-footer") == 0 ) {
  84. EXPAND(footers) = argv[2];
  85. argc -= 2;
  86. argv += 2;
  87. }
  88. else
  89. break;
  90. }
  91. switch ( argc ) {
  92. char *p, *dot;
  93. case 1:
  94. input = stdin;
  95. output = stdout;
  96. break;
  97. case 2:
  98. case 3:
  99. dest = malloc(strlen(argv[argc-1]) + 6);
  100. source = malloc(strlen(argv[1]) + 6);
  101. if ( !(source && dest) )
  102. fail("out of memory allocating name buffers");
  103. strcpy(source, argv[1]);
  104. if (( p = strrchr(source, '/') ))
  105. p = source;
  106. else
  107. ++p;
  108. if ( (input = fopen(source, "r")) == 0 ) {
  109. strcat(source, ".text");
  110. if ( (input = fopen(source, "r")) == 0 )
  111. fail("can't open either %s or %s", argv[1], source);
  112. }
  113. strcpy(dest, source);
  114. if (( dot = strrchr(dest, '.') ))
  115. *dot = 0;
  116. strcat(dest, ".html");
  117. if ( (output = fopen(dest, "w")) == 0 )
  118. fail("can't write to %s", dest);
  119. break;
  120. default:
  121. fprintf(stderr, "usage: %s [opts] source [dest]\n", pgm);
  122. exit(1);
  123. }
  124. if ( (mmiot = mkd_in(input, 0)) == 0 )
  125. fail("can't read %s", source ? source : "stdin");
  126. if ( !mkd_compile(mmiot, 0) )
  127. fail("couldn't compile input");
  128. h = mkd_doc_title(mmiot);
  129. /* print a header */
  130. fprintf(output,
  131. "<!doctype html public \"-//W3C//DTD HTML 4.0 Transitional //EN\">\n"
  132. "<html>\n"
  133. "<head>\n"
  134. " <meta name=\"GENERATOR\" content=\"mkd2html %s\">\n", markdown_version);
  135. fprintf(output," <meta http-equiv=\"Content-Type\"\n"
  136. " content=\"text/html; charset-us-ascii\">");
  137. for ( i=0; i < S(css); i++ )
  138. fprintf(output, " <link rel=\"stylesheet\"\n"
  139. " type=\"text/css\"\n"
  140. " href=\"%s\" />\n", T(css)[i]);
  141. if ( h ) {
  142. fprintf(output," <title>");
  143. mkd_generateline(h, strlen(h), output, 0);
  144. fprintf(output, "</title>\n");
  145. }
  146. for ( i=0; i < S(headers); i++ )
  147. fprintf(output, " %s\n", T(headers)[i]);
  148. fprintf(output, "</head>\n"
  149. "<body>\n");
  150. /* print the compiled body */
  151. mkd_generatehtml(mmiot, output);
  152. for ( i=0; i < S(footers); i++ )
  153. fprintf(output, "%s\n", T(footers)[i]);
  154. fprintf(output, "</body>\n"
  155. "</html>\n");
  156. mkd_cleanup(mmiot);
  157. exit(0);
  158. }