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.

97 lines
3.1 KiB

15 years ago
15 years ago
15 years ago
15 years ago
11 years ago
15 years ago
11 years ago
15 years ago
15 years ago
15 years ago
15 years ago
11 years ago
15 years ago
  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. typedef struct tmdd_obj {
  8. bool html5;
  9. } tmdd_obj;
  10. typedef tmdd_obj *Text__Markdown__Discount;
  11. MODULE = Text::Markdown::Discount PACKAGE = Text::Markdown::Discount PREFIX = TextMarkdown_
  12. PROTOTYPES: DISABLE
  13. BOOT:
  14. HV* stash = gv_stashpvn("Text::Markdown::Discount", strlen("Text::Markdown::Discount"), TRUE);
  15. newCONSTSUB(stash, "MKD_NOLINKS", newSViv(MKD_NOLINKS));
  16. newCONSTSUB(stash, "MKD_NOIMAGE", newSViv(MKD_NOIMAGE));
  17. newCONSTSUB(stash, "MKD_NOPANTS", newSViv(MKD_NOPANTS));
  18. newCONSTSUB(stash, "MKD_NOHTML", newSViv(MKD_NOHTML));
  19. newCONSTSUB(stash, "MKD_STRICT", newSViv(MKD_STRICT));
  20. newCONSTSUB(stash, "MKD_TAGTEXT", newSViv(MKD_TAGTEXT));
  21. newCONSTSUB(stash, "MKD_NO_EXT", newSViv(MKD_NO_EXT));
  22. newCONSTSUB(stash, "MKD_CDATA", newSViv(MKD_CDATA));
  23. newCONSTSUB(stash, "MKD_NOSUPERSCRIPT", newSViv(MKD_NOSUPERSCRIPT));
  24. newCONSTSUB(stash, "MKD_NORELAXED", newSViv(MKD_NORELAXED));
  25. newCONSTSUB(stash, "MKD_NOTABLES", newSViv(MKD_NOTABLES));
  26. newCONSTSUB(stash, "MKD_NOSTRIKETHROUGH", newSViv(MKD_NOSTRIKETHROUGH));
  27. newCONSTSUB(stash, "MKD_TOC", newSViv(MKD_TOC));
  28. newCONSTSUB(stash, "MKD_1_COMPAT", newSViv(MKD_1_COMPAT));
  29. newCONSTSUB(stash, "MKD_AUTOLINK", newSViv(MKD_AUTOLINK));
  30. newCONSTSUB(stash, "MKD_SAFELINK", newSViv(MKD_SAFELINK));
  31. newCONSTSUB(stash, "MKD_NOHEADER", newSViv(MKD_NOHEADER));
  32. newCONSTSUB(stash, "MKD_TABSTOP", newSViv(MKD_TABSTOP));
  33. newCONSTSUB(stash, "MKD_NODIVQUOTE", newSViv(MKD_NODIVQUOTE));
  34. newCONSTSUB(stash, "MKD_NOALPHALIST", newSViv(MKD_NOALPHALIST));
  35. newCONSTSUB(stash, "MKD_NODLIST", newSViv(MKD_NODLIST));
  36. newCONSTSUB(stash, "MKD_EXTRA_FOOTNOTE", newSViv(MKD_EXTRA_FOOTNOTE));
  37. SV *
  38. TextMarkdown__markdown(self, sv_str, flags)
  39. Text::Markdown::Discount self
  40. SV *sv_str
  41. int flags;
  42. PREINIT:
  43. bool is_utf8 = SvUTF8(sv_str) != 0; // SvUTF8 doesn't typecast consistently to bool across various archs
  44. char *text = SvPV_nolen(sv_str);
  45. SV* r = &PL_sv_undef;
  46. char *html = NULL;
  47. int szhtml;
  48. MMIOT *doc;
  49. CODE:
  50. if ( (doc = mkd_string(text, strlen(text), flags)) == 0 ) {
  51. croak("failed at mkd_string");
  52. }
  53. if ( !mkd_compile(doc, flags) ) {
  54. mkd_cleanup(doc);
  55. croak("failed at mkd_compile");
  56. }
  57. if ( (szhtml = mkd_document(doc, &html)) == EOF ) {;
  58. mkd_cleanup(doc);
  59. croak("failed at mkd_document");
  60. }
  61. r = newSVpvn(html, szhtml);
  62. sv_catpv(r, "\n");
  63. if (is_utf8) {
  64. sv_utf8_decode(r);
  65. }
  66. mkd_cleanup(doc);
  67. RETVAL = r;
  68. OUTPUT:
  69. RETVAL
  70. Text::Markdown::Discount
  71. TextMarkdown__new(clazz, html5)
  72. char *clazz
  73. bool html5
  74. PREINIT:
  75. Text__Markdown__Discount self;
  76. CODE:
  77. self = calloc(1, sizeof(struct tmdd_obj));
  78. if (html5) {
  79. mkd_with_html5_tags();
  80. }
  81. self->html5 = html5;
  82. RETVAL = self;
  83. OUTPUT:
  84. RETVAL