aboutsummaryrefslogtreecommitdiff
path: root/test/cmark.py
blob: 253e3a88402fae2c9608138f1daf601952aae74a (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. def pipe_through_prog(prog, text):
  7. p1 = Popen(prog.split(), stdout=PIPE, stdin=PIPE, stderr=PIPE)
  8. [result, err] = p1.communicate(input=text.encode('utf-8'))
  9. return [p1.returncode, result.decode('utf-8'), err]
  10. def use_library(lib, text):
  11. textbytes = text.encode('utf-8')
  12. textlen = len(textbytes)
  13. return [0, lib(textbytes, textlen).decode('utf-8'), '']
  14. class CMark:
  15. def __init__(self, prog=None, library_dir=None):
  16. self.prog = prog
  17. if prog:
  18. self.to_html = lambda x: pipe_through_prog(prog, x)
  19. else:
  20. sysname = platform.system()
  21. libname = "libcmark"
  22. if sysname == 'Darwin':
  23. libname += ".dylib"
  24. elif sysname == 'Windows':
  25. libname = "cmark.dll"
  26. else:
  27. libname += ".so"
  28. if library_dir:
  29. libpath = library_dir + "/" + libname
  30. else:
  31. libpath = "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)