css.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /* null-terminate, then strdup() into a free()able memory
  49. * chunk
  50. */
  51. EXPAND(f) = 0;
  52. *res = strdup(T(f));
  53. }
  54. DELETE(f);
  55. return size;
  56. }
  57. return EOF;
  58. }
  59. /* dump any embedded styles to a file
  60. */
  61. int
  62. mkd_generatecss(Document *d, FILE *f)
  63. {
  64. char *res;
  65. int written;
  66. int size = mkd_css(d, &res);
  67. written = (size > 0) ? fwrite(res,1,size,f) : 0;
  68. if ( res )
  69. free(res);
  70. return (written == size) ? size : EOF;
  71. }