Text::Markdown::Discount

Discount.xs 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "EXTERN.h"
  2. #include "perl.h"
  3. #include "XSUB.h"
  4. #include "ppport.h"
  5. #include <string.h>
  6. #include <mkdio.h>
  7. MODULE = Text::Markdown::Discount PACKAGE = Text::Markdown::Discount PREFIX = TextMarkdown_
  8. PROTOTYPES: DISABLE
  9. BOOT:
  10. HV* stash = gv_stashpvn("Text::Markdown::Discount", strlen("Text::Markdown::Discount"), TRUE);
  11. newCONSTSUB(stash, "MKD_NOLINKS", newSViv(MKD_NOLINKS));
  12. newCONSTSUB(stash, "MKD_NOIMAGE", newSViv(MKD_NOIMAGE));
  13. newCONSTSUB(stash, "MKD_NOPANTS", newSViv(MKD_NOPANTS));
  14. newCONSTSUB(stash, "MKD_NOHTML", newSViv(MKD_NOHTML));
  15. newCONSTSUB(stash, "MKD_STRICT", newSViv(MKD_STRICT));
  16. newCONSTSUB(stash, "MKD_TAGTEXT", newSViv(MKD_TAGTEXT));
  17. newCONSTSUB(stash, "MKD_NO_EXT", newSViv(MKD_NO_EXT));
  18. newCONSTSUB(stash, "MKD_CDATA", newSViv(MKD_CDATA));
  19. newCONSTSUB(stash, "MKD_NOSUPERSCRIPT", newSViv(MKD_NOSUPERSCRIPT));
  20. newCONSTSUB(stash, "MKD_NORELAXED", newSViv(MKD_NORELAXED));
  21. newCONSTSUB(stash, "MKD_NOTABLES", newSViv(MKD_NOTABLES));
  22. newCONSTSUB(stash, "MKD_NOSTRIKETHROUGH", newSViv(MKD_NOSTRIKETHROUGH));
  23. newCONSTSUB(stash, "MKD_TOC", newSViv(MKD_TOC));
  24. newCONSTSUB(stash, "MKD_1_COMPAT", newSViv(MKD_1_COMPAT));
  25. newCONSTSUB(stash, "MKD_AUTOLINK", newSViv(MKD_AUTOLINK));
  26. newCONSTSUB(stash, "MKD_SAFELINK", newSViv(MKD_SAFELINK));
  27. newCONSTSUB(stash, "MKD_NOHEADER", newSViv(MKD_NOHEADER));
  28. newCONSTSUB(stash, "MKD_TABSTOP", newSViv(MKD_TABSTOP));
  29. newCONSTSUB(stash, "MKD_NODIVQUOTE", newSViv(MKD_NODIVQUOTE));
  30. newCONSTSUB(stash, "MKD_NOALPHALIST", newSViv(MKD_NOALPHALIST));
  31. newCONSTSUB(stash, "MKD_NODLIST", newSViv(MKD_NODLIST));
  32. newCONSTSUB(stash, "MKD_EXTRA_FOOTNOTE", newSViv(MKD_EXTRA_FOOTNOTE));
  33. SV *
  34. TextMarkdown__markdown(sv_str, flags)
  35. SV *sv_str
  36. int flags;
  37. PREINIT:
  38. bool is_utf8 = SvUTF8(sv_str) != 0; // SvUTF8 doesn't typecast consistently to bool across various archs
  39. char *text = SvPV_nolen(sv_str);
  40. SV* r = &PL_sv_undef;
  41. char *html = NULL;
  42. int szhtml;
  43. MMIOT *doc;
  44. CODE:
  45. if ( (doc = mkd_string(text, strlen(text), flags)) == 0 ) {
  46. croak("failed at mkd_string");
  47. }
  48. if ( !mkd_compile(doc, flags) ) {
  49. mkd_cleanup(doc);
  50. croak("failed at mkd_compile");
  51. }
  52. if ( (szhtml = mkd_document(doc, &html)) == EOF ) {;
  53. mkd_cleanup(doc);
  54. croak("failed at mkd_document");
  55. }
  56. r = newSVpvn(html, szhtml);
  57. sv_catpv(r, "\n");
  58. if (is_utf8) {
  59. sv_utf8_decode(r);
  60. }
  61. mkd_cleanup(doc);
  62. RETVAL = r;
  63. OUTPUT:
  64. RETVAL
  65. void
  66. TextMarkdown_with_html5_tags()
  67. CODE:
  68. mkd_with_html5_tags();