<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -3,6 +3,7 @@ Copyright (C) 2007 David Loren Parsons.
 
 The Discount Lua extension sources are
 Copyright (C) 2008 A.S. Bradbury.
+Copyright (C) 2008 Tim Channon.
 
 All rights reserved.
 </diff>
      <filename>LICENSE</filename>
    </modified>
    <modified>
      <diff>@@ -12,7 +12,7 @@ CC= gcc
 CFLAGS= $(INCS) $(WARN) -O2 -fPIC ${DEFS}
 WARN= -Wall
 INCS= -I$(LUAINC)
-DEFS = -DHAVE_FOPENCOOKIE
+DEFS = 
 
 DISCOUNT_OBJS = docheader.o \
 	dumptree.o \</diff>
      <filename>Makefile</filename>
    </modified>
    <modified>
      <diff>@@ -1,28 +1,27 @@
-#if defined(HAVE_FOPENCOOKIE)
-#  define _GNU_SOURCE
-#endif
-
 #include &lt;stdio.h&gt;
+#include &lt;ctype.h&gt;
 
 #include &quot;lua.h&quot;
 #include &quot;lauxlib.h&quot;
 
-#include &quot;mkdio.h&quot;
+#include &quot;markdown.h&quot;
 
-static int fakehandle_write(void *cookie, const char *data, int len) {
-  luaL_Buffer *b = (luaL_Buffer*)cookie;
-  luaL_addlstring(b, data, len);
-  return len;
-}
+/* copied from mkdio.h */
+/* special flags for markdown() and mkd_text()
+ */
+#define MKD_NOLINKS	0x0001	/* don't do link processing, block &lt;a&gt; tags  */
+#define MKD_NOIMAGE	0x0002	/* don't do image processing, block &lt;img&gt; */
+#define MKD_NOPANTS	0x0004	/* don't run smartypants() */
+#define MKD_NOHTML	0x0008	/* don't allow raw html through AT ALL */
+#define MKD_TAGTEXT	0x0020	/* don't expand `_` and `*` */
+#define MKD_NO_EXT	0x0040	/* don't allow pseudo-protocols */
+#define MKD_CDATA	0x0080	/* generate code for xml ![CDATA[...]] */
+#define MKD_EMBED	MKD_NOLINKS|MKD_NOIMAGE|MKD_TAGTEXT
 
-#if defined(HAVE_FOPENCOOKIE)
-static cookie_io_functions_t fakehandle_functions = {
-  (cookie_read_function_t*)NULL,
-  (cookie_write_function_t*)fakehandle_write,
-  (cookie_seek_function_t*)NULL,
-  (cookie_close_function_t*)NULL
-};
-#endif
+/* special flags for mkd_in() and mkd_string()
+ */
+#define MKD_NOHEADER	0x0100	/* don't process header blocks */
+#define MKD_TABSTOP	0x0200	/* expand tabs to 4 spaces */
 
 static const char *const discount_opts[] = {
   &quot;nolinks&quot;,
@@ -47,30 +46,69 @@ static const int discount_opts_codes[] = {
   MKD_EMBED
 };
 
+/* routines duplicated from markdown source without filesystem access */
+
+/* write output in XML format */
+static void local___mkd_xml(char *p, int size, luaL_Buffer *b) {
+  char c;
+
+  while (size-- &gt; 0) {
+    if (!isascii(c = *p++))
+      continue;
+    switch (c) {
+      case '&lt;': luaL_addlstring(b, &quot;&amp;lt;&quot;, 4);   break;
+      case '&gt;': luaL_addlstring(b, &quot;&amp;gt;&quot;, 4);   break;
+      case '&amp;': luaL_addlstring(b, &quot;&amp;amp;&quot;, 5);  break;
+      case '&quot;': luaL_addlstring(b, &quot;&amp;quot;&quot;, 6); break;
+      case '\'':luaL_addlstring(b, &quot;&amp;apos;&quot;, 6); break;
+      default:  luaL_addchar(b, c);
+    }
+  }
+}
+
+/* write the html to a buffer (xmlified if necessary) */
+static int local_mkd_generatehtml(Document *p, luaL_Buffer *b) {
+  char *doc;
+  int szdoc;
+
+  if ((szdoc = mkd_document(p, &amp;doc)) != EOF) {
+    if (p-&gt;ctx-&gt;flags &amp; CDATA_OUTPUT)
+      local___mkd_xml(doc, szdoc, b);
+    else
+      luaL_addlstring(b, doc, szdoc);
+    luaL_addchar(b, '\n');
+    return 0;
+  }
+  return -1;
+}
+
+/* convert some markdown text to html. */
+static int local_markdown(Document *document, luaL_Buffer *b, int flags) {
+  if (mkd_compile(document, flags)) {
+    local_mkd_generatehtml(document, b);
+    mkd_cleanup(document);
+    return 0;
+  }
+  return -1;
+}
+
 static int ldiscount(lua_State *L) {
   size_t len;
   const char *str = luaL_checklstring(L, 1, &amp;len);
   int flags = 0;
   int num_args = lua_gettop(L);
-  FILE *fh;
-  int i;
+  luaL_Buffer b;
+  Document *doc;
+  int ret, i;
+
   for (i = 2; i &lt;= num_args; i++) {
     int opt_index = luaL_checkoption(L, i, NULL, discount_opts);
     flags |= discount_opts_codes[opt_index];
   }
 
-  luaL_Buffer b;
   luaL_buffinit(L, &amp;b);
-
-#if defined(HAVE_FOPENCOOKIE)
-  fh = fopencookie((void*)&amp;b, &quot;w&quot;, fakehandle_functions);
-#else
-  fh = funopen((void*)&amp;b, NULL, fakehandle_write, NULL, NULL);
-#endif
-
-  MMIOT *doc = mkd_string(str, len, MKD_TABSTOP|MKD_NOHEADER);
-  int ret = markdown(doc, fh, flags);
-  fclose(fh);
+  doc = mkd_string(str, len, MKD_TABSTOP|MKD_NOHEADER);
+  ret = local_markdown(doc, &amp;b, flags);
   luaL_pushresult(&amp;b);
   if (ret &lt; 0)
     return luaL_error(L, &quot;error in markdown conversion&quot;);</diff>
      <filename>ldiscount.c</filename>
    </modified>
    <modified>
      <diff>@@ -24,14 +24,5 @@ build = {
          &quot;resource.c&quot;,
          &quot;ldiscount.c&quot;
       }
-   },
-   platforms = {
-      linux = {
-         modules = {
-            discount = {
-               defines = {&quot;HAVE_FOPENCOOKIE&quot;}
-            }
-         }
-      }
    }
 }</diff>
      <filename>lua-discount-dev-1.rockspec</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>0ab88be17ba56e4ee6fe1d475233cd8a1db31424</id>
    </parent>
  </parents>
  <author>
    <name>A.S. Bradbury</name>
    <email>asb@asbradbury.org</email>
  </author>
  <url>http://github.com/asb/lua-discount/commit/864280ca8413c6ca9a30b90d57ad069d94e29874</url>
  <id>864280ca8413c6ca9a30b90d57ad069d94e29874</id>
  <committed-date>2008-09-17T02:42:10-07:00</committed-date>
  <authored-date>2008-09-17T02:35:05-07:00</authored-date>
  <message>remove dependency on fopencookie/funopen

Thanks to Tim Channon for contributing a patch demonstrating this approach.
The definitions of a few short discount functions are copied into ldiscount.c,
and changed to write to a luaL_Buffer rather than a file. Hopefully windows
support will now be possible (with a few more modifications, Tim has had
lua-discount building on several non-MS windows compilers).</message>
  <tree>c7592037e7c07e36d156834ac34bb3a9b41f3bed</tree>
  <committer>
    <name>A.S. Bradbury</name>
    <email>asb@asbradbury.org</email>
  </committer>
</commit>
