aboutsummaryrefslogtreecommitdiff
path: root/test/cmark.py
blob: 432a3e03b0eff565e98e4bd09d4523bd8e56794a (plain)
  1. #!/usr/bin/env python
  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)
  9. return [p1.returncode, result, err]
  10. def use_library(lib, text):
  11. return [0, lib(text, len(text)), '']
  12. class CMark:
  13. def __init__(self, prog=None, library_dir=None):
  14. self.prog = prog
  15. if prog:
  16. self.to_html = lambda x: pipe_through_prog(prog, x)
  17. else:
  18. sysname = platform.system()
  19. libname = "libcmark"
  20. if sysname == 'Darwin':
  21. libname += ".dylib"
  22. elif sysname == 'Windows':
  23. libname += ".dll"
  24. else:
  25. libname += ".so"
  26. if library_dir:
  27. libpath = library_dir + "/" + libname
  28. else:
  29. libpath = "build/src/" + libname
  30. cmark = CDLL(libpath)
  31. markdown = cmark.cmark_markdown_to_html
  32. markdown.restype = c_char_p
  33. markdown.argtypes = [c_char_p, c_long]
  34. self.to_html = lambda x: use_library(markdown, x)