Debian Codegraph @

wrappers/wrapper_ext.py

raw
1 #!/usr/bin/env python
2
3 #
4 # Example for using the shared library from python.
5 # Will work with either python 2 or python 3.
6 # Requires cmark-gfm and cmark-gfm-extensions libraries to be installed.
7 #
8 # This particular example uses the GitHub extensions from the gfm-extensions
9 # library. EXTENSIONS specifies which to use, and the sample shows how to
10 # connect them into a parser.
11 #
12
13 import sys
14 import ctypes
15
16 if sys.platform == 'darwin':
17 libname = 'libcmark-gfm.dylib'
18 extname = 'libcmark-gfm-extensions.dylib'
19 elif sys.platform == 'win32':
20 libname = 'cmark-gfm.dll'
21 extname = 'cmark-gfm-extensions.dll'
22 else:
23 libname = 'libcmark-gfm.so'
24 extname = 'libcmark-gfm-extensions.so'
25 cmark = ctypes.CDLL(libname)
26 cmark_ext = ctypes.CDLL(extname)
27
28 # Options for the GFM rendering call
29 OPTS = 0 # defaults
30
31 # The GFM extensions that we want to use
32 EXTENSIONS = (
33 'autolink',
34 'table',
35 'strikethrough',
36 'tagfilter',
37 )
38
39 # Use ctypes to access the functions in libcmark-gfm
40
41 F_cmark_parser_new = cmark.cmark_parser_new
42 F_cmark_parser_new.restype = ctypes.c_void_p
43 F_cmark_parser_new.argtypes = (ctypes.c_int,)
44
45 F_cmark_parser_feed = cmark.cmark_parser_feed
46 F_cmark_parser_feed.restype = None
47 F_cmark_parser_feed.argtypes = (ctypes.c_void_p, ctypes.c_char_p, ctypes.c_size_t)
48
49 F_cmark_parser_finish = cmark.cmark_parser_finish
50 F_cmark_parser_finish.restype = ctypes.c_void_p
51 F_cmark_parser_finish.argtypes = (ctypes.c_void_p,)
52
53 F_cmark_parser_attach_syntax_extension = cmark.cmark_parser_attach_syntax_extension
54 F_cmark_parser_attach_syntax_extension.restype = ctypes.c_int
55 F_cmark_parser_attach_syntax_extension.argtypes = (ctypes.c_void_p, ctypes.c_void_p)
56
57 F_cmark_parser_get_syntax_extensions = cmark.cmark_parser_get_syntax_extensions
58 F_cmark_parser_get_syntax_extensions.restype = ctypes.c_void_p
59 F_cmark_parser_get_syntax_extensions.argtypes = (ctypes.c_void_p,)
60
61 F_cmark_parser_free = cmark.cmark_parser_free
62 F_cmark_parser_free.restype = None
63 F_cmark_parser_free.argtypes = (ctypes.c_void_p,)
64
65 F_cmark_node_free = cmark.cmark_node_free
66 F_cmark_node_free.restype = None
67 F_cmark_node_free.argtypes = (ctypes.c_void_p,)
68
69 F_cmark_find_syntax_extension = cmark.cmark_find_syntax_extension
70 F_cmark_find_syntax_extension.restype = ctypes.c_void_p
71 F_cmark_find_syntax_extension.argtypes = (ctypes.c_char_p,)
72
73 F_cmark_render_html = cmark.cmark_render_html
74 F_cmark_render_html.restype = ctypes.c_char_p
75 F_cmark_render_html.argtypes = (ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p)
76
77
78 # Set up the libcmark-gfm library and its extensions
79 F_register = cmark_ext.cmark_gfm_core_extensions_ensure_registered
80 F_register.restype = None
81 F_register.argtypes = ( )
82 F_register()
83
84
85 def md2html(text):
86 "Use cmark-gfm to render the Markdown into an HTML fragment."
87
88 parser = F_cmark_parser_new(OPTS)
89 assert parser
90 for name in EXTENSIONS:
91 ext = F_cmark_find_syntax_extension(name)
92 assert ext
93 rv = F_cmark_parser_attach_syntax_extension(parser, ext)
94 assert rv
95 exts = F_cmark_parser_get_syntax_extensions(parser)
96
97 F_cmark_parser_feed(parser, text, len(text))
98 doc = F_cmark_parser_finish(parser)
99 assert doc
100
101 output = F_cmark_render_html(doc, OPTS, exts)
102
103 F_cmark_parser_free(parser)
104 F_cmark_node_free(doc)
105
106 return output
107
108
109 sys.stdout.write(md2html(sys.stdin.read()))