aboutsummaryrefslogtreecommitdiff
path: root/test/cmark.py
blob: 1110860306d1126e6f948d6269c0b95358bef37e (plain)
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from ctypes import CDLL, c_char_p, c_long
  4. from subprocess import *
  5. import platform
  6. import os
  7. def pipe_through_prog(prog, text):
  8. p1 = Popen(prog.split(), stdout=PIPE, stdin=PIPE, stderr=PIPE)
  9. [result, err] = p1.communicate(input=text.encode('utf-8'))
  10. return [p1.returncode, result.decode('utf-8'), err]
  11. def use_library(lib, text):
  12. textbytes = text.encode('utf-8')
  13. textlen = len(textbytes)
  14. return [0, lib(textbytes, textlen, 0).decode('utf-8'), '']
  15. class CMark:
  16. def __init__(self, prog=None, library_dir=None):
  17. self.prog = prog
  18. if prog:
  19. self.to_html = lambda x: pipe_through_prog(prog, x)
  20. else:
  21. sysname = platform.system()
  22. if sysname == 'Darwin':
  23. libname = "libcmark.dylib"
  24. elif sysname == 'Windows':
  25. libname = "cmark.dll"
  26. else:
  27. libname = "libcmark.so"
  28. if library_dir:
  29. libpath = os.path.join(library_dir, libname)
  30. else:
  31. libpath = os.path.join("build", "src", libname)
  32. cmark = CDLL(libpath)
  33. markdown = cmark.cmark_markdown_to_html
  34. markdown.restype = c_char_p
  35. markdown.argtypes = [c_char_p, c_long]
  36. self.to_html = lambda x: use_library(markdown, x)