Skip to content

Instantly share code, notes, and snippets.

@pudquick
Last active September 5, 2020 04:16
Show Gist options
  • Save pudquick/03e454f4d2ad3ebe09216b78559c0541 to your computer and use it in GitHub Desktop.
Save pudquick/03e454f4d2ad3ebe09216b78559c0541 to your computer and use it in GitHub Desktop.
Abuse Xcode's embedded CommonMark framework to generate HTML from markdown
# hax hax hax hax hax
import ctypes, os, sys
newstderr = os.dup(2) # This is to mute dyld LC_RPATH warnings
os.dup2(os.open('/dev/null', os.O_WRONLY), 2) # because we're loading Xcode frameworks from python
CM = ctypes.CDLL('/Applications/Xcode.app/Contents/SharedFrameworks/DVTMarkup.framework/Versions/A/Frameworks/CommonMark.framework/CommonMark')
sys.stderr = os.fdopen(newstderr, 'w') # This restores stderr
cmark_markdown_to_html = CM.cmark_markdown_to_html
cmark_markdown_to_html.restype = ctypes.c_char_p
def markdown_to_html(markdown):
mc = ctypes.create_string_buffer(markdown)
return cmark_markdown_to_html(mc, len(mc)-1, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment