From mboxrd@z Thu Jan 1 00:00:00 1970 From: Lars Tveito Subject: Add-on: Github Flavored Markdown exporter Date: Sun, 06 Apr 2014 23:33:13 +0200 Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:39775) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WWugw-0000Nj-49 for emacs-orgmode@gnu.org; Sun, 06 Apr 2014 17:33:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WWugn-0003px-Rm for emacs-orgmode@gnu.org; Sun, 06 Apr 2014 17:33:26 -0400 Received: from mail-out1.uio.no ([129.240.10.57]:44439) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WWugn-0003or-DH for emacs-orgmode@gnu.org; Sun, 06 Apr 2014 17:33:17 -0400 Received: from mail-mx1.uio.no ([129.240.10.29]) by mail-out1.uio.no with esmtp (Exim 4.75) (envelope-from ) id 1WWugk-0006wK-16 for emacs-orgmode@gnu.org; Sun, 06 Apr 2014 23:33:14 +0200 Received: from host-37-191-192-30.lynet.no ([37.191.192.30] helo=Larss-MacBook-Air.local) by mail-mx1.uio.no with esmtpsa (TLSv1.2:AES128-GCM-SHA256:128) user larstvei (Exim 4.80) (envelope-from ) id 1WWugj-0008Lt-K1 for emacs-orgmode@gnu.org; Sun, 06 Apr 2014 23:33:13 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Hi! I have written an exporter for Github Flavored Markdown, which is a derived back-end from the Markdown (vanilla) exporter. It adds Github-style src-blocks, strike-through and table of contents. I think this could be useful because Org is a great tool for writing README's. Org is supported by Github, but it currently does not support syntax highlighting and table of contents (and has some other minor quirks). Should I contribute this to the /lisp/contrib/? Best regards, - Lars --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=ox-gfm.el Content-Transfer-Encoding: quoted-printable ;;; ox-gfm.el --- Github Flavored Markdown Back-End for Org Export Engine ;; Copyright (C) 2014 Free Software Foundation, Inc. ;; Author: Lars Tveito ;; Keywords: org, wp, markdown, github ;; This file is not part of GNU Emacs. ;; GNU Emacs is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; GNU Emacs is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs. If not, see . ;;; Commentary: ;; This library implements a Markdown back-end (github flavor) for Org ;; exporter, based on the `md' back-end. ;;; Code: (require 'ox-md) ;;; User-Configurable Variables (defgroup org-export-gfm nil "Options specific to Markdown export back-end." :tag "Org Github Flavored Markdown" :group 'org-export :version "24.4" :package-version '(Org . "8.0")) (defcustom org-gfm-lang '(("emacs-lisp" . "lisp") ("elisp" . "lisp")) "Alist of languages that are not recognized by Github, to languages that are. Emacs lisp is a good example of this, where we can use lisp as a nice replacement." :group 'org-export-gfm) ;;; Define Back-End (org-export-define-derived-backend 'gfm 'md :export-block '("GFM" "GITHUB FLAVORED MARKDOWN") :filters-alist '((:filter-parse-tree . org-md-separate-elements)) :menu-entry '(?g "Export to Github Flavored Markdown" ((?G "To temporary buffer" (lambda (a s v b) (org-gfm-export-as-markdown a s v))) (?g "To file" (lambda (a s v b) (org-gfm-export-to-markdown a s v))) (?o "To file and open" (lambda (a s v b) (if a (org-gfm-export-to-markdown t s v) (org-open-file (org-gfm-export-to-markdown nil s v))))))) :translate-alist '((inner-template . org-gfm-inner-template) (strike-through . org-gfm-strike-through) (src-block . org-gfm-src-block))) ;;; Transcode Functions ;;;; Src Block (defun org-gfm-src-block (src-block contents info) "Transcode SRC-BLOCK element into Github Flavored Markdown format. CONTENTS is nil. INFO is a plist used as a communication channel." (let* ((lang (org-element-property :language src-block)) (lang (or (assoc-default lang org-gfm-lang) lang)) (code (org-export-format-code-default src-block info)) (prefix (concat "```" lang "\n")) (suffix "```")) (concat prefix code suffix))) ;;;; Strike-Through (defun org-html-strike-through (strike-through contents info) "Transcode STRIKE-THROUGH from Org to Markdown (GFM). CONTENTS is the text with strike-through markup. INFO is a plist holding contextual information." (format "~~%s~~" contents)) ;;;; Table of contents (defun org-gfm-format-toc (headline) "Return an appropriate table of contents entry for HEADLINE. INFO is a plist used as a communication channel." (let* ((title (org-export-data (org-export-get-alt-title headline info) info)) (level (1- (org-element-property :level headline))) (indent (concat (make-string (* level 2) ? ))) (ref-str (replace-regexp-in-string " " "-" (downcase title)))) (concat indent "- [" title "]" "(#" ref-str ")"))) ;;;; Template (defun org-gfm-inner-template (contents info) "Return body of document after converting it to Markdown syntax. CONTENTS is the transcoded contents string. INFO is a plist holding export options." (let* ((depth (plist-get info :with-toc)) (headlines (and depth (org-export-collect-headlines info depth))) (toc-string (or (mapconcat 'org-gfm-format-toc headlines "\n") "")) (toc-tail (if headlines "\n\n" ""))) (concat toc-string toc-tail contents))) ;;; Interactive function ;;;###autoload (defun org-gfm-export-as-markdown (&optional async subtreep visible-only) "Export current buffer to a Github Flavored Markdown buffer. If narrowing is active in the current buffer, only export its narrowed part. If a region is active, export that region. A non-nil optional argument ASYNC means the process should happen asynchronously. The resulting buffer should be accessible through the `org-export-stack' interface. When optional argument SUBTREEP is non-nil, export the sub-tree at point, extracting information from the headline properties first. When optional argument VISIBLE-ONLY is non-nil, don't export contents of hidden elements. Export is done in a buffer named \"*Org GFM Export*\", which will be displayed when `org-export-show-temporary-export-buffer' is non-nil." (interactive) (org-export-to-buffer 'gfm "*Org GFM Export*" async subtreep visible-only nil nil (lambda () (text-mode)))) ;;;###autoload (defun org-gfm-convert-region-to-md () "Assume the current region has org-mode syntax, and convert it to Github Flavored Markdown. This can be used in any buffer. For example, you can write an itemized list in org-mode syntax in a Markdown buffer and use this command to convert it." (interactive) (org-export-replace-region-by 'gfm)) ;;;###autoload (defun org-gfm-export-to-markdown (&optional async subtreep visible-only) "Export current buffer to a Github Flavored Markdown file. If narrowing is active in the current buffer, only export its narrowed part. If a region is active, export that region. A non-nil optional argument ASYNC means the process should happen asynchronously. The resulting file should be accessible through the `org-export-stack' interface. When optional argument SUBTREEP is non-nil, export the sub-tree at point, extracting information from the headline properties first. When optional argument VISIBLE-ONLY is non-nil, don't export contents of hidden elements. Return output file's name." (interactive) (let ((outfile (org-export-output-file-name ".md" subtreep))) (org-export-to-file 'gfm outfile async subtreep visible-only))) (provide 'ox-gfm) ;; Local variables: ;; generated-autoload-file: "org-loaddefs.el" ;; End: ;;; ox-gfm.el ends here --=-=-=--