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.

185 lines
3.7 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. 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 > 2 ) {
  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. }
  90. if ( argc > 1 ) {
  91. char *p, *dot;
  92. source = malloc(strlen(argv[1]) + 6);
  93. dest = malloc(strlen(argv[1]) + 6);
  94. if ( !(source && dest) )
  95. fail("out of memory allocating name buffers");
  96. strcpy(source, argv[1]);
  97. if (( p = strrchr(source, '/') ))
  98. p = source;
  99. else
  100. ++p;
  101. if ( (input = fopen(source, "r")) == 0 ) {
  102. strcat(source, ".text");
  103. if ( (input = fopen(source, "r")) == 0 )
  104. fail("can't open either %s or %s", argv[1], source);
  105. }
  106. strcpy(dest, source);
  107. if (( dot = strrchr(dest, '.') ))
  108. *dot = 0;
  109. strcat(dest, ".html");
  110. if ( (output = fopen(dest, "w")) == 0 )
  111. fail("can't write to %s", dest);
  112. }
  113. else {
  114. input = stdin;
  115. output = stdout;
  116. }
  117. if ( (mmiot = mkd_in(input, 0)) == 0 )
  118. fail("can't read %s", source ? source : "stdin");
  119. if ( !mkd_compile(mmiot, 0) )
  120. fail("couldn't compile input");
  121. h = mkd_doc_title(mmiot);
  122. /* print a header */
  123. fprintf(output,
  124. "<!doctype html public \"-//W3C//DTD HTML 4.0 Transitional //EN\">\n"
  125. "<html>\n"
  126. "<head>\n"
  127. " <meta name=\"GENERATOR\" content=\"mkd2html %s\">\n", markdown_version);
  128. fprintf(output," <meta http-equiv=\"Content-Type\"\n"
  129. " content=\"text/html; charset-us-ascii\">");
  130. for ( i=0; i < S(css); i++ )
  131. fprintf(output, " <link rel=\"stylesheet\"\n"
  132. " type=\"text/css\"\n"
  133. " href=\"%s\" />\n", T(css)[i]);
  134. if ( h ) {
  135. fprintf(output," <title>");
  136. mkd_generateline(h, strlen(h), output, 0);
  137. fprintf(output, "</title>\n");
  138. }
  139. for ( i=0; i < S(headers); i++ )
  140. fprintf(output, " %s\n", T(headers)[i]);
  141. fprintf(output, "</head>\n"
  142. "<body>\n");
  143. /* print the compiled body */
  144. mkd_generatehtml(mmiot, output);
  145. for ( i=0; i < S(footers); i++ )
  146. fprintf(output, "%s\n", T(footers)[i]);
  147. fprintf(output, "</body>\n"
  148. "</html>\n");
  149. mkd_cleanup(mmiot);
  150. exit(0);
  151. }