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.

85 lines
1.6 KiB

  1. /* markdown: a C implementation of John Gruber's Markdown markup language.
  2. *
  3. * Copyright (C) 2009 David L Parsons.
  4. * The redistribution terms are provided in the COPYRIGHT file that must
  5. * be distributed with this source code.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdarg.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12. #include <ctype.h>
  13. #include "config.h"
  14. #include "cstring.h"
  15. #include "markdown.h"
  16. #include "amalloc.h"
  17. /*
  18. * dump out stylesheet sections.
  19. */
  20. static void
  21. stylesheets(Paragraph *p, Cstring *f)
  22. {
  23. Line* q;
  24. for ( ; p ; p = p->next ) {
  25. if ( p->typ == STYLE ) {
  26. for ( q = p->text; q ; q = q->next ) {
  27. Cswrite(f, T(q->text), S(q->text));
  28. Csputc('\n', f);
  29. }
  30. }
  31. if ( p->down )
  32. stylesheets(p->down, f);
  33. }
  34. }
  35. /* dump any embedded styles to a string
  36. */
  37. int
  38. mkd_css(Document *d, char **res)
  39. {
  40. Cstring f;
  41. int size;
  42. if ( res && d && d->compiled ) {
  43. *res = 0;
  44. CREATE(f);
  45. RESERVE(f, 100);
  46. stylesheets(d->code, &f);
  47. if ( (size = S(f)) > 0 ) {
  48. EXPAND(f) = 0;
  49. /* HACK ALERT! HACK ALERT! HACK ALERT! */
  50. *res = T(f);/* we know that a T(Cstring) is a character pointer */
  51. /* so we can simply pick it up and carry it away, */
  52. /* leaving the husk of the Ctring on the stack */
  53. /* END HACK ALERT */
  54. }
  55. else
  56. DELETE(f);
  57. return size;
  58. }
  59. return EOF;
  60. }
  61. /* dump any embedded styles to a file
  62. */
  63. int
  64. mkd_generatecss(Document *d, FILE *f)
  65. {
  66. char *res;
  67. int written = EOF, size = mkd_css(d, &res);
  68. if ( size > 0 )
  69. written = fwrite(res, 1, size, f);
  70. if ( res )
  71. free(res);
  72. return (written == size) ? size : EOF;
  73. }