cmark

My personal build of CMark ✏️

Commit
8dab8b603d3f115d7830f3d90b071ab784596978
Parent
36ab6fcfb426000eeb2d341d049f65a3d3d46450
Author
John MacFarlane <jgm@berkeley.edu>
Date

Removed wrapper3.py, made wrapper.py work with python 2/3.

Also improved the wrapper to work with windows, and to use smart punctuation (as an example).

In the future we may want to use cffi instead of ctypes. It is like luajit ffi, and allows us to use constants from the C library.

Diffstat

2 files changed, 18 insertions, 29 deletions

Status File Name N° Changes Insertions Deletions
Modified wrappers/wrapper.py 22 18 4
Deleted wrappers/wrapper3.py 25 0 25
diff --git a/wrappers/wrapper.py b/wrappers/wrapper.py
@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 
 # Example for using the shared library from python
+# Will work with either python 2 or python 3
+# Requires cmark library to be installed
 
 from ctypes import CDLL, c_char_p, c_long
 import sys
@@ -9,15 +11,27 @@ import platform
 sysname = platform.system()
 
 if sysname == 'Darwin':
-    cmark = CDLL("build/src/libcmark.dylib")
+    libname = "libcmark.dylib"
+elif sysname == 'Windows':
+    libname = "cmark.dll"
 else:
-    cmark = CDLL("build/src/libcmark.so")
+    libname = "libcmark.so"
+cmark = CDLL(libname)
 
 markdown = cmark.cmark_markdown_to_html
 markdown.restype = c_char_p
-markdown.argtypes = [c_char_p, c_long]
+markdown.argtypes = [c_char_p, c_long, c_long]
+
+opts = 8 # CMARK_OPT_PRETTY
 
 def md2html(text):
-    return markdown(text, len(text))
+    if sys.version_info >= (3,0):
+        textbytes = text.encode('utf-8')
+        textlen = len(textbytes)
+        return markdown(textbytes, textlen, opts).decode('utf-8')
+    else:
+        textbytes = text
+        textlen = len(text)
+        return markdown(textbytes, textlen, opts)
 
 sys.stdout.write(md2html(sys.stdin.read()))
diff --git a/wrappers/wrapper3.py b/wrappers/wrapper3.py
@@ -1,25 +0,0 @@
-#!/usr/bin/env python3
-
-# Example for using the shared library from python
-
-from ctypes import CDLL, c_char_p, c_long
-import sys
-import platform
-
-sysname = platform.system()
-
-if sysname == 'Darwin':
-    cmark = CDLL("build/src/libcmark.dylib")
-else:
-    cmark = CDLL("build/src/libcmark.so")
-
-markdown = cmark.cmark_markdown_to_html
-markdown.restype = c_char_p
-markdown.argtypes = [c_char_p, c_long]
-
-def md2html(text):
-    textbytes = text.encode('utf-8')
-    textlen = len(textbytes)
-    return markdown(textbytes, textlen).decode('utf-8')
-
-sys.stdout.write(md2html(sys.stdin.read()))