| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# Example for using the shared library from python |
| 4 |
# Will work with either python 2 or python 3 |
| 5 |
# Requires cmark library to be installed |
| 6 |
|
| 7 |
from ctypes import CDLL, c_char_p, c_long |
| 8 |
import sys |
| 9 |
import platform |
| 10 |
|
| 11 |
sysname = platform.system() |
| 12 |
|
| 13 |
if sysname == 'Darwin': |
| 14 |
libname = "libcmark.dylib" |
| 15 |
elif sysname == 'Windows': |
| 16 |
libname = "cmark.dll" |
| 17 |
else: |
| 18 |
libname = "libcmark.so" |
| 19 |
cmark = CDLL(libname) |
| 20 |
|
| 21 |
markdown = cmark.cmark_markdown_to_html |
| 22 |
markdown.restype = c_char_p |
| 23 |
markdown.argtypes = [c_char_p, c_long, c_long] |
| 24 |
|
| 25 |
opts = 0 # defaults |
| 26 |
|
| 27 |
def md2html(text): |
| 28 |
if sys.version_info >= (3,0): |
| 29 |
textbytes = text.encode('utf-8') |
| 30 |
textlen = len(textbytes) |
| 31 |
return markdown(textbytes, textlen, opts).decode('utf-8') |
| 32 |
else: |
| 33 |
textbytes = text |
| 34 |
textlen = len(text) |
| 35 |
return markdown(textbytes, textlen, opts) |
| 36 |
|
| 37 |
sys.stdout.write(md2html(sys.stdin.read())) |