2014-10-30 09:01:32 +00:00
|
|
|
|
;;; ox-gfm.el --- Github Flavored Markdown Back-End for Org Export Engine
|
|
|
|
|
|
|
|
|
|
;; Copyright (C) 2014 Lars Tveito
|
|
|
|
|
|
|
|
|
|
;; 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; This library implements a Markdown back-end (github flavor) for Org
|
|
|
|
|
;; exporter, based on the `md' back-end.
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
|
|
(require 'ox-md)
|
2016-09-05 17:39:43 +00:00
|
|
|
|
(require 'ox-publish)
|
2014-10-30 09:01:32 +00:00
|
|
|
|
|
|
|
|
|
;;; 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"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Define Back-End
|
|
|
|
|
|
|
|
|
|
(org-export-define-derived-backend 'gfm 'md
|
|
|
|
|
: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)
|
2016-08-04 16:02:01 +00:00
|
|
|
|
(paragraph . org-gfm-paragraph)
|
2014-10-30 09:01:32 +00:00
|
|
|
|
(strike-through . org-gfm-strike-through)
|
2016-01-02 14:11:27 +00:00
|
|
|
|
(src-block . org-gfm-src-block)
|
|
|
|
|
(table-cell . org-gfm-table-cell)
|
|
|
|
|
(table-row . org-gfm-table-row)
|
|
|
|
|
(table . org-gfm-table)))
|
2014-10-30 09:01:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Transcode Functions
|
|
|
|
|
|
2016-03-10 11:44:00 +00:00
|
|
|
|
;;;; Paragraph
|
|
|
|
|
|
2016-05-20 20:13:26 +00:00
|
|
|
|
(defun org-gfm-paragraph (paragraph contents info)
|
2016-03-10 11:44:00 +00:00
|
|
|
|
"Transcode PARAGRAPH element into Github Flavoured Markdown format.
|
|
|
|
|
CONTENTS is the paragraph contents. INFO is a plist used as a
|
|
|
|
|
communication channel."
|
2016-05-20 20:13:26 +00:00
|
|
|
|
(unless (plist-get info :preserve-breaks)
|
2016-08-04 16:02:01 +00:00
|
|
|
|
(setq contents (concat (mapconcat 'identity (split-string contents) " ") "\n")))
|
2016-05-20 18:56:56 +00:00
|
|
|
|
(let ((first-object (car (org-element-contents paragraph))))
|
|
|
|
|
;; If paragraph starts with a #, protect it.
|
|
|
|
|
(if (and (stringp first-object) (string-match "\\`#" first-object))
|
2016-08-04 16:02:01 +00:00
|
|
|
|
(replace-regexp-in-string "\\`#" "\\#" contents nil t)
|
2016-05-20 18:56:56 +00:00
|
|
|
|
contents)))
|
2016-03-10 11:44:00 +00:00
|
|
|
|
|
|
|
|
|
|
2014-10-30 09:01:32 +00:00
|
|
|
|
;;;; 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))
|
|
|
|
|
(code (org-export-format-code-default src-block info))
|
|
|
|
|
(prefix (concat "```" lang "\n"))
|
|
|
|
|
(suffix "```"))
|
|
|
|
|
(concat prefix code suffix)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;; Strike-Through
|
|
|
|
|
|
2014-12-08 11:06:23 +00:00
|
|
|
|
(defun org-gfm-strike-through (strike-through contents info)
|
2014-10-30 09:01:32 +00:00
|
|
|
|
"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))
|
|
|
|
|
|
2016-01-02 14:11:27 +00:00
|
|
|
|
|
2016-01-03 16:15:31 +00:00
|
|
|
|
;;;; Table-Common
|
2016-01-02 14:11:27 +00:00
|
|
|
|
|
|
|
|
|
(defvar width-cookies nil)
|
|
|
|
|
(defvar width-cookies-table nil)
|
|
|
|
|
|
2016-02-28 10:31:27 +00:00
|
|
|
|
(defconst gfm-table-left-border "|")
|
2016-01-03 16:15:31 +00:00
|
|
|
|
(defconst gfm-table-right-border " |")
|
2016-02-28 10:31:27 +00:00
|
|
|
|
(defconst gfm-table-separator " |")
|
2016-01-03 16:15:31 +00:00
|
|
|
|
|
2016-01-02 14:11:27 +00:00
|
|
|
|
(defun org-gfm-table-col-width (table column info)
|
|
|
|
|
"Return width of TABLE at given COLUMN. INFO is a plist used as
|
|
|
|
|
communication channel. Width of a column is determined either by
|
|
|
|
|
inquerying `width-cookies' in the column, or by the maximum cell with in
|
|
|
|
|
the column."
|
|
|
|
|
(let ((cookie (when (hash-table-p width-cookies)
|
|
|
|
|
(gethash column width-cookies))))
|
|
|
|
|
(if (and (eq table width-cookies-table)
|
|
|
|
|
(not (eq nil cookie)))
|
|
|
|
|
cookie
|
|
|
|
|
(progn
|
|
|
|
|
(unless (and (eq table width-cookies-table)
|
|
|
|
|
(hash-table-p width-cookies))
|
|
|
|
|
(setq width-cookies (make-hash-table))
|
|
|
|
|
(setq width-cookies-table table))
|
|
|
|
|
(let ((max-width 0)
|
|
|
|
|
(specialp (org-export-table-has-special-column-p table)))
|
|
|
|
|
(org-element-map
|
|
|
|
|
table
|
|
|
|
|
'table-row
|
|
|
|
|
(lambda (row)
|
|
|
|
|
(setq max-width
|
|
|
|
|
(max (length
|
|
|
|
|
(org-export-data
|
|
|
|
|
(org-element-contents
|
2016-01-03 16:15:31 +00:00
|
|
|
|
(elt (if specialp (car (org-element-contents row))
|
|
|
|
|
(org-element-contents row))
|
2016-01-02 14:11:27 +00:00
|
|
|
|
column))
|
|
|
|
|
info))
|
|
|
|
|
max-width)))
|
|
|
|
|
info)
|
|
|
|
|
(puthash column max-width width-cookies))))))
|
|
|
|
|
|
|
|
|
|
|
2016-01-03 16:15:31 +00:00
|
|
|
|
(defun org-gfm-make-hline-builder (table info char)
|
|
|
|
|
"Return a function to build horizontal line in TABLE with given
|
|
|
|
|
CHAR. INFO is a plist used as a communication channel."
|
|
|
|
|
`(lambda (col)
|
2016-02-28 10:31:27 +00:00
|
|
|
|
(let ((max-width (max 3 (org-gfm-table-col-width table col info))))
|
2016-01-03 16:15:31 +00:00
|
|
|
|
(when (< max-width 1)
|
|
|
|
|
(setq max-width 1))
|
|
|
|
|
(make-string max-width ,char))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;; Table-Cell
|
|
|
|
|
|
2016-01-02 14:11:27 +00:00
|
|
|
|
(defun org-gfm-table-cell (table-cell contents info)
|
|
|
|
|
"Transcode TABLE-CELL element from Org into GFM. CONTENTS is content
|
|
|
|
|
of the cell. INFO is a plist used as a communication channel."
|
|
|
|
|
(let* ((table (org-export-get-parent-table table-cell))
|
|
|
|
|
(column (cdr (org-export-table-cell-address table-cell info)))
|
|
|
|
|
(width (org-gfm-table-col-width table column info))
|
|
|
|
|
(left-border (if (org-export-table-cell-starts-colgroup-p table-cell info) "| " " "))
|
|
|
|
|
(right-border " |")
|
|
|
|
|
(data (or contents "")))
|
|
|
|
|
(setq contents
|
|
|
|
|
(concat data
|
2016-02-28 10:31:27 +00:00
|
|
|
|
(make-string (max 0 (- width (string-width data)))
|
2016-01-02 14:11:27 +00:00
|
|
|
|
?\s)))
|
|
|
|
|
(concat left-border contents right-border)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;; Table-Row
|
|
|
|
|
|
|
|
|
|
(defun org-gfm-table-row (table-row contents info)
|
|
|
|
|
"Transcode TABLE-ROW element from Org into GFM. CONTENTS is cell
|
|
|
|
|
contents of TABLE-ROW. INFO is a plist used as a communication
|
|
|
|
|
channel."
|
2016-01-03 16:15:31 +00:00
|
|
|
|
(let ((table (org-export-get-parent-table table-row)))
|
|
|
|
|
(when (and (eq 'rule (org-element-property :type table-row))
|
|
|
|
|
;; In GFM, rule is valid only at second row.
|
|
|
|
|
(eq 1 (cl-position
|
|
|
|
|
table-row
|
|
|
|
|
(org-element-map table 'table-row 'identity info))))
|
|
|
|
|
(let* ((table (org-export-get-parent-table table-row))
|
|
|
|
|
(header-p (org-export-table-row-starts-header-p table-row info))
|
|
|
|
|
(build-rule (org-gfm-make-hline-builder table info ?-))
|
|
|
|
|
(cols (cdr (org-export-table-dimensions table info))))
|
|
|
|
|
(setq contents
|
|
|
|
|
(concat gfm-table-left-border
|
|
|
|
|
(mapconcat (lambda (col) (funcall build-rule col))
|
|
|
|
|
(number-sequence 0 (- cols 1))
|
|
|
|
|
gfm-table-separator)
|
|
|
|
|
gfm-table-right-border))))
|
|
|
|
|
contents))
|
2016-01-02 14:11:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;; Table
|
|
|
|
|
|
|
|
|
|
(defun org-gfm-table (table contents info)
|
|
|
|
|
"Transcode TABLE element into Github Flavored Markdown table.
|
|
|
|
|
CONTENTS is the contents of the table. INFO is a plist holding
|
|
|
|
|
contextual information."
|
2016-01-03 16:15:31 +00:00
|
|
|
|
(let* ((rows (org-element-map table 'table-row 'identity info))
|
2016-02-28 10:33:21 +00:00
|
|
|
|
(no-header (or (<= (length rows) 1)))
|
2016-01-03 16:15:31 +00:00
|
|
|
|
(cols (cdr (org-export-table-dimensions table info)))
|
|
|
|
|
(build-dummy-header
|
|
|
|
|
(function
|
|
|
|
|
(lambda ()
|
|
|
|
|
(let ((build-empty-cell (org-gfm-make-hline-builder table info ?\s))
|
|
|
|
|
(build-rule (org-gfm-make-hline-builder table info ?-))
|
|
|
|
|
(columns (number-sequence 0 (- cols 1))))
|
|
|
|
|
(concat gfm-table-left-border
|
|
|
|
|
(mapconcat (lambda (col) (funcall build-empty-cell col))
|
|
|
|
|
columns
|
|
|
|
|
gfm-table-separator)
|
|
|
|
|
gfm-table-right-border "\n" gfm-table-left-border
|
|
|
|
|
(mapconcat (lambda (col) (funcall build-rule col))
|
|
|
|
|
columns
|
|
|
|
|
gfm-table-separator)
|
|
|
|
|
gfm-table-right-border "\n"))))))
|
2016-08-04 16:02:01 +00:00
|
|
|
|
(concat (when no-header (funcall build-dummy-header))
|
|
|
|
|
(replace-regexp-in-string "\n\n" "\n" contents))))
|
2016-01-02 14:11:27 +00:00
|
|
|
|
|
|
|
|
|
|
2014-10-30 09:01:32 +00:00
|
|
|
|
;;;; 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) ? )))
|
2014-12-08 11:06:23 +00:00
|
|
|
|
(anchor (or (org-element-property :custom_id headline)
|
2016-08-04 16:09:14 +00:00
|
|
|
|
(org-export-get-reference headline info))))
|
2014-12-08 11:06:23 +00:00
|
|
|
|
(concat indent "- [" title "]" "(#" anchor ")")))
|
|
|
|
|
|
|
|
|
|
|
2016-08-05 16:28:28 +00:00
|
|
|
|
;;;; Footnote section
|
|
|
|
|
|
|
|
|
|
(defun org-gfm-footnote-section (info)
|
|
|
|
|
"Format the footnote section.
|
|
|
|
|
INFO is a plist used as a communication channel."
|
|
|
|
|
(let* ((fn-alist (org-export-collect-footnote-definitions info))
|
|
|
|
|
(fn-alist
|
|
|
|
|
(loop for (n type raw) in fn-alist collect
|
|
|
|
|
(cons n (org-trim (org-export-data raw info))))))
|
|
|
|
|
(when fn-alist
|
|
|
|
|
(format
|
|
|
|
|
"## %s\n%s"
|
|
|
|
|
"Footnotes"
|
|
|
|
|
(format
|
|
|
|
|
"\n%s\n"
|
|
|
|
|
(mapconcat
|
|
|
|
|
(lambda (fn)
|
|
|
|
|
(let ((n (car fn)) (def (cdr fn)))
|
|
|
|
|
(format
|
|
|
|
|
"%s %s\n"
|
|
|
|
|
(format
|
|
|
|
|
(plist-get info :html-footnote-format)
|
|
|
|
|
(org-html--anchor
|
|
|
|
|
(format "fn.%d" n)
|
|
|
|
|
n
|
|
|
|
|
(format " class=\"footnum\" href=\"#fnr.%d\"" n)
|
|
|
|
|
info))
|
|
|
|
|
def)))
|
|
|
|
|
fn-alist
|
|
|
|
|
"\n"))))))
|
2014-10-30 09:01:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;; 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" "")))
|
2016-08-05 16:28:28 +00:00
|
|
|
|
(org-trim (concat toc-string toc-tail contents "\n" (org-gfm-footnote-section info)))))
|
2016-08-04 18:12:59 +00:00
|
|
|
|
|
|
|
|
|
|
2014-10-30 09:01:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; 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)))
|
|
|
|
|
|
2016-09-05 17:39:43 +00:00
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun org-gfm-publish-to-gfm (plist filename pub-dir)
|
|
|
|
|
"Publish an org file to Markdown.
|
|
|
|
|
FILENAME is the filename of the Org file to be published. PLIST
|
|
|
|
|
is the property list for the given project. PUB-DIR is the
|
|
|
|
|
publishing directory.
|
|
|
|
|
Return output file name."
|
|
|
|
|
(org-publish-org-to 'gfm filename ".md" plist pub-dir))
|
|
|
|
|
|
2014-10-30 09:01:32 +00:00
|
|
|
|
(provide 'ox-gfm)
|
|
|
|
|
|
|
|
|
|
;;; ox-gfm.el ends here
|