summaryrefslogtreecommitdiffstats
path: root/tdemarkdown/md4c/test/cmark.py
diff options
context:
space:
mode:
authorMavridis Philippe <mavridisf@gmail.com>2022-02-05 17:44:26 +0000
committerSlávek Banko <slavek.banko@axis.cz>2022-04-18 06:58:02 +0200
commite87a929300f82e8b7608039f143b1533b51af4e0 (patch)
tree4cd1e34b81250a10eeb9ad4087c4a611dc080de5 /tdemarkdown/md4c/test/cmark.py
parent8905a8003ddefcb8f57608b48585f98f7ac93958 (diff)
downloadtdelibs-e87a9293.tar.gz
tdelibs-e87a9293.zip
Add tdemarkdown part - embeddable lightweight markdown viewing component.
TDEMarkdown is based on the md4c library and using TDEHTML for rendering its output. For enhanced safety, on HTML widget is turned off everything we don't need for viewing. It integrates nicely into Konqueror and supports both Commonmark and GitHub markdown syntaxes. Signed-off-by: Mavridis Philippe <mavridisf@gmail.com> Prepare to merge tdemarkdown into tdelibs. Signed-off-by: Slávek Banko <slavek.banko@axis.cz> (cherry picked from commit 95279fbf6dfeb43d80590740a9259d7caa614177)
Diffstat (limited to 'tdemarkdown/md4c/test/cmark.py')
-rwxr-xr-xtdemarkdown/md4c/test/cmark.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/tdemarkdown/md4c/test/cmark.py b/tdemarkdown/md4c/test/cmark.py
new file mode 100755
index 000000000..111086030
--- /dev/null
+++ b/tdemarkdown/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)