💾 Archived View for gmi.noulin.net › gitRepositories › md4c › file › test › cmark.py.gmi captured on 2023-01-29 at 13:23:41. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
cmark.py (1372B)
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 4 from ctypes import CDLL, c_char_p, c_long 5 from subprocess import * 6 import platform 7 import os 8 9 def pipe_through_prog(prog, text): 10 p1 = Popen(prog.split(), stdout=PIPE, stdin=PIPE, stderr=PIPE) 11 [result, err] = p1.communicate(input=text.encode('utf-8')) 12 return [p1.returncode, result.decode('utf-8'), err] 13 14 def use_library(lib, text): 15 textbytes = text.encode('utf-8') 16 textlen = len(textbytes) 17 return [0, lib(textbytes, textlen, 0).decode('utf-8'), ''] 18 19 class CMark: 20 def __init__(self, prog=None, library_dir=None): 21 self.prog = prog 22 if prog: 23 self.to_html = lambda x: pipe_through_prog(prog, x) 24 else: 25 sysname = platform.system() 26 if sysname == 'Darwin': 27 libname = "libcmark.dylib" 28 elif sysname == 'Windows': 29 libname = "cmark.dll" 30 else: 31 libname = "libcmark.so" 32 if library_dir: 33 libpath = os.path.join(library_dir, libname) 34 else: 35 libpath = os.path.join("build", "src", libname) 36 cmark = CDLL(libpath) 37 markdown = cmark.cmark_markdown_to_html 38 markdown.restype = c_char_p 39 markdown.argtypes = [c_char_p, c_long] 40 self.to_html = lambda x: use_library(markdown, x)