makepage.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * makepage: Use mkd_xhtmlpage() to convert markdown input to a
  3. * fully-formed xhtml page.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <mkdio.h>
  9. #include "config.h"
  10. #include "pgm_options.h"
  11. #include "gethopt.h"
  12. #ifndef HAVE_BASENAME
  13. char*
  14. basename(char *p)
  15. {
  16. char *ret = strrchr(p, '/');
  17. return ret ? (1+ret) : p;
  18. }
  19. #endif
  20. char *pgm = "makepage";
  21. struct h_opt opts[] = {
  22. { 0, "version", 'V', 0, "show version info" },
  23. { 0, 0, 'F', "bitmap", "set/show hex flags" },
  24. { 0, "flags", 'f', "{+-}flags", "set/show named flags" },
  25. } ;
  26. #define NROPTS (sizeof opts / sizeof opts[0])
  27. int
  28. main(argc, argv)
  29. int argc;
  30. char **argv;
  31. {
  32. MMIOT *doc;
  33. char *q;
  34. int version = 0;
  35. int ret, i;
  36. DWORD bits;
  37. mkd_flag_t *flags = mkd_flags();
  38. struct h_opt *opt;
  39. struct h_context blob;
  40. if ( !flags )
  41. perror("mkd_flags");
  42. if ( (q = getenv("MARKDOWN_FLAGS")) ) {
  43. bits = strtol(q, 0, 0);
  44. for ( i=0; i < 8*sizeof(bits); i++)
  45. if ( bits & (1<<i) )
  46. mkd_set_flag_num(flags, i);
  47. }
  48. hoptset(&blob, argc, argv);
  49. hopterr(&blob, 1);
  50. while ( opt = gethopt(&blob, opts, NROPTS) ) {
  51. if ( opt == HOPTERR ) {
  52. hoptusage(pgm, opts, NROPTS, "[file]");
  53. exit(1);
  54. }
  55. switch ( opt->optchar ) {
  56. case 'V': version++;
  57. break;
  58. case 'F': if ( strcmp(hoptarg(&blob), "?") == 0 ) {
  59. show_flags(0,0,0);
  60. exit(0);
  61. }
  62. else {
  63. bits = strtol(hoptarg(&blob), 0, 0);
  64. for (i=0; i < 8*sizeof(bits); i++)
  65. if ( bits & (1<<i) )
  66. mkd_set_flag_num(flags, i);
  67. }
  68. break;
  69. case 'f': if ( strcmp(hoptarg(&blob), "?") == 0 ) {
  70. show_flags(1,version,0);
  71. exit(0);
  72. }
  73. else if ( q = mkd_set_flag_string(flags, hoptarg(&blob)) )
  74. fprintf(stderr, "unknown option <%s>\n", q);
  75. break;
  76. }
  77. }
  78. argc -= hoptind(&blob);
  79. argv += hoptind(&blob);
  80. if ( version ) {
  81. printf("%s: discount %s", pgm, markdown_version);
  82. if ( version > 1 )
  83. mkd_flags_are(stdout, flags, 0);
  84. putchar('\n');
  85. exit(0);
  86. }
  87. if ( (argc > 0) && !freopen(argv[0], "r", stdin) ) {
  88. perror(argv[0]);
  89. exit(1);
  90. }
  91. if ( (doc = mkd_in(stdin, flags)) == 0 ) {
  92. perror( (argc > 1) ? argv[1] : "stdin" );
  93. exit(1);
  94. }
  95. ret = mkd_xhtmlpage(doc, flags, stdout);
  96. mkd_cleanup(doc);
  97. mkd_free_flags(flags);
  98. return (ret == EOF);
  99. }