Created
November 22, 2012 02:58
-
-
Save syohex/4129208 to your computer and use it in GitHub Desktop.
Using Github Markdown Rendering API from Emacs
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
| (eval-when-compile | |
| (require 'cl)) | |
| (require 'url) | |
| (require 'url-http) | |
| (require 'json) | |
| (defvar github-markdown:url "https://api.github.com/markdown") | |
| (defvar github-markdown:url-raw "https://api.github.com/markdown/raw") | |
| (defun github-markdown:read-markdown (file) | |
| (let ((buf (find-file-noselect file))) | |
| (with-current-buffer buf | |
| (buffer-substring-no-properties (point-min) (point-max))))) | |
| (defun github-markdown:construct-post-input (file &optional mode context) | |
| (let ((mode (or mode "gfm")) | |
| (context (or context "github/gollun")) | |
| (data (github-markdown:read-markdown file))) | |
| (json-encode `((mode . ,mode) (context . ,context) (text . ,data))))) | |
| (defun github-markdown:post (file) | |
| (let ((url-request-method "POST") | |
| (url-request-data (github-markdown:construct-post-input file))) | |
| (with-current-buffer (url-retrieve-synchronously github-markdown:url) | |
| (goto-char (point-min)) | |
| (if (re-search-forward "^$" nil t) | |
| (message "%s" (buffer-substring (1+ (point)) (point-max))) | |
| (error "Error"))))) | |
| (defun github-markdown:construct-post-raw-input (file) | |
| (github-markdown:read-markdown file)) | |
| (defun github-markdown:post-raw (file) | |
| (let ((url-request-method "POST") | |
| (url-request-data (github-markdown:construct-post-raw-input file)) | |
| (url-request-extra-headers '(("Content-Type" . "text/x-markdown")))) | |
| (with-current-buffer (url-retrieve-synchronously github-markdown:url-raw) | |
| (goto-char (point-min)) | |
| (if (re-search-forward "^$" nil t) | |
| (write-region (1+ (point)) (point-max) "test.html") | |
| (error "Error"))))) | |
| (defun github-markdown (file) | |
| (interactive | |
| (list | |
| (read-file-name "Input Markdown File: "))) | |
| (github-markdown:post file)) | |
| (defun github-markdown-raw (file) | |
| (interactive | |
| (list | |
| (read-file-name "Input Markdown File: "))) | |
| (github-markdown:post-raw file)) | |
| (provide 'github-markdown) | |
| ;;; github-markdown.el ends here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment