-
-
Save erikng/07370a6cbcc8e88e122ea53096fbad17 to your computer and use it in GitHub Desktop.
Convert markdown to html in a shell script using Xcode's CommonMark framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
cat <<__MARKDOWN__ | | |
# Title ÅÄÖ | |
* list | |
__MARKDOWN__ | |
/usr/bin/python <( cat <<__EOF__ | |
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# https://gist.github.com/pudquick/03e454f4d2ad3ebe09216b78559c0541 | |
import os | |
import sys | |
import ctypes | |
import subprocess | |
def save_stderr(): | |
oldstderr = os.dup(2) | |
os.dup2(os.open("/dev/null", os.O_WRONLY), 2) | |
return oldstderr | |
def restore_stderr(oldstderr): | |
sys.stderr = os.fdopen(oldstderr, "w") | |
def open_commonmark(): | |
oldstderr = save_stderr() | |
dev_path = subprocess.check_output(["xcode-select", "-p"]).rstrip() | |
xcode_path = dev_path.partition(".app/")[0] + ".app" | |
dtvmarkup_path = os.path.join(xcode_path, "Contents/SharedFrameworks/DVTMarkup.framework") | |
commonmark_path = os.path.join(dtvmarkup_path, "Versions/A/Frameworks/CommonMark.framework") | |
CM = ctypes.CDLL(os.path.join(commonmark_path, "CommonMark")) | |
restore_stderr(oldstderr) | |
return CM | |
CM = open_commonmark() | |
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.encode("utf-8")) | |
return cmark_markdown_to_html(mc, len(mc)-1, 0).decode("utf-8") | |
sys.stdout.write(markdown_to_html(sys.stdin.read().decode("utf-8")).encode("utf-8")) | |
__EOF__ | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment