aboutsummaryrefslogtreecommitdiff
path: root/deps/md4c/test/cmark.py
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2021-10-01 14:21:33 +0200
committerToni Uhlig <matzeton@googlemail.com>2021-10-01 14:21:33 +0200
commitd67b11c832cd0061392e46e847a465f8f73c1ca5 (patch)
tree09bf2e08d0075917f051edbf210be8cb8f79b303 /deps/md4c/test/cmark.py
parent6c04dfe2caff1e03ba5c898b591327439452f616 (diff)
parentd071b4177c1a3897f4682e245046a45f362b6ac5 (diff)
Merge commit 'd071b4177c1a3897f4682e245046a45f362b6ac5' as 'deps/md4c'
Diffstat (limited to 'deps/md4c/test/cmark.py')
-rwxr-xr-xdeps/md4c/test/cmark.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/deps/md4c/test/cmark.py b/deps/md4c/test/cmark.py
new file mode 100755
index 0000000..1110860
--- /dev/null
+++ b/deps/md4c/test/cmark.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+from ctypes import CDLL, c_char_p, c_long
+from subprocess import *
+import platform
+import os
+
+def pipe_through_prog(prog, text):
+ p1 = Popen(prog.split(), stdout=PIPE, stdin=PIPE, stderr=PIPE)
+ [result, err] = p1.communicate(input=text.encode('utf-8'))
+ return [p1.returncode, result.decode('utf-8'), err]
+
+def use_library(lib, text):
+ textbytes = text.encode('utf-8')
+ textlen = len(textbytes)
+ return [0, lib(textbytes, textlen, 0).decode('utf-8'), '']
+
+class CMark:
+ def __init__(self, prog=None, library_dir=None):
+ self.prog = prog
+ if prog:
+ self.to_html = lambda x: pipe_through_prog(prog, x)
+ else:
+ sysname = platform.system()
+ if sysname == 'Darwin':
+ libname = "libcmark.dylib"
+ elif sysname == 'Windows':
+ libname = "cmark.dll"
+ else:
+ libname = "libcmark.so"
+ if library_dir:
+ libpath = os.path.join(library_dir, libname)
+ else:
+ libpath = os.path.join("build", "src", libname)
+ cmark = CDLL(libpath)
+ markdown = cmark.cmark_markdown_to_html
+ markdown.restype = c_char_p
+ markdown.argtypes = [c_char_p, c_long]
+ self.to_html = lambda x: use_library(markdown, x)