mirror of
https://github.com/larstvei/ox-gfm.git
synced 2025-04-19 19:10:12 +00:00
Compare commits
No commits in common. "master" and "v1.0" have entirely different histories.
158
ox-gfm.el
158
ox-gfm.el
@ -1,4 +1,4 @@
|
||||
;;; ox-gfm.el --- Github Flavored Markdown Back-End for Org Export Engine -*- lexical-binding: t; -*-
|
||||
;;; ox-gfm.el --- Github Flavored Markdown Back-End for Org Export Engine
|
||||
|
||||
;; Copyright (C) 2014-2017 Lars Tveito
|
||||
|
||||
@ -29,7 +29,6 @@
|
||||
|
||||
(require 'ox-md)
|
||||
(require 'ox-publish)
|
||||
|
||||
|
||||
;;; User-Configurable Variables
|
||||
|
||||
@ -57,12 +56,12 @@
|
||||
:translate-alist '((inner-template . org-gfm-inner-template)
|
||||
(paragraph . org-gfm-paragraph)
|
||||
(strike-through . org-gfm-strike-through)
|
||||
(example-block . org-gfm-example-block)
|
||||
(src-block . org-gfm-src-block)
|
||||
(table-cell . org-gfm-table-cell)
|
||||
(table-row . org-gfm-table-row)
|
||||
(table . org-gfm-table)))
|
||||
|
||||
|
||||
|
||||
;;; Transcode Functions
|
||||
|
||||
@ -80,11 +79,12 @@ communication channel."
|
||||
(replace-regexp-in-string "\\`#" "\\#" contents nil t)
|
||||
contents)))
|
||||
|
||||
|
||||
;;;; 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
|
||||
(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))
|
||||
@ -92,18 +92,16 @@ channel."
|
||||
(suffix "```"))
|
||||
(concat prefix code suffix)))
|
||||
|
||||
;;;; Example Block
|
||||
|
||||
(defalias 'org-gfm-example-block #'org-gfm-src-block)
|
||||
|
||||
;;;; Strike-Through
|
||||
|
||||
(defun org-gfm-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
|
||||
(defun org-gfm-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-Common
|
||||
|
||||
(defvar width-cookies nil)
|
||||
@ -114,10 +112,10 @@ holding contextual information."
|
||||
(defconst gfm-table-separator " |")
|
||||
|
||||
(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."
|
||||
"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)
|
||||
@ -146,21 +144,22 @@ or by the maximum cell with in the column."
|
||||
info)
|
||||
(puthash column max-width width-cookies))))))
|
||||
|
||||
|
||||
(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)
|
||||
(let ((max-width (max 3 (org-gfm-table-col-width table col info))))
|
||||
(when (< max-width 1)
|
||||
(setq max-width 1))
|
||||
(make-string max-width char))))
|
||||
"Return a function to build horizontal line in TABLE with given
|
||||
CHAR. INFO is a plist used as a communication channel."
|
||||
`(lambda (col)
|
||||
(let ((max-width (max 3 (org-gfm-table-col-width table col info))))
|
||||
(when (< max-width 1)
|
||||
(setq max-width 1))
|
||||
(make-string max-width ,char))))
|
||||
|
||||
|
||||
;;;; Table-Cell
|
||||
|
||||
(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."
|
||||
"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))
|
||||
@ -173,12 +172,13 @@ communication channel."
|
||||
?\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."
|
||||
"Transcode TABLE-ROW element from Org into GFM. CONTENTS is cell
|
||||
contents of TABLE-ROW. INFO is a plist used as a communication
|
||||
channel."
|
||||
(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.
|
||||
@ -186,6 +186,7 @@ communication channel."
|
||||
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
|
||||
@ -196,36 +197,41 @@ communication channel."
|
||||
gfm-table-right-border))))
|
||||
contents))
|
||||
|
||||
|
||||
|
||||
;;;; 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
|
||||
CONTENTS is the contents of the table. INFO is a plist holding
|
||||
contextual information."
|
||||
(let* ((rows (org-element-map table 'table-row 'identity info))
|
||||
(no-header (or (<= (length rows) 1)))
|
||||
(cols (cdr (org-export-table-dimensions table info)))
|
||||
(build-dummy-header
|
||||
(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")))))
|
||||
(concat (and no-header (funcall 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"))))))
|
||||
(concat (when no-header (funcall build-dummy-header))
|
||||
(replace-regexp-in-string "\n\n" "\n" contents))))
|
||||
|
||||
|
||||
;;;; Table of contents
|
||||
|
||||
(defun org-gfm-format-toc (headline info)
|
||||
"Return an appropriate table of contents entry for HEADLINE."
|
||||
(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)))
|
||||
@ -234,25 +240,38 @@ contextual information."
|
||||
(org-export-get-reference headline info))))
|
||||
(concat indent "- [" title "]" "(#" anchor ")")))
|
||||
|
||||
|
||||
;;;; Footnote section
|
||||
|
||||
(defun org-gfm-footnote-section (info)
|
||||
"Format the footnote section.
|
||||
INFO is a plist used as a communication channel."
|
||||
(and-let* ((fn-alist (org-export-collect-footnote-definitions info)))
|
||||
(format
|
||||
"## Footnotes\n\n%s\n"
|
||||
(mapconcat (pcase-lambda (`(,n ,_type ,def))
|
||||
(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))
|
||||
(org-trim (org-export-data def info))))
|
||||
fn-alist "\n"))))
|
||||
(let* ((fn-alist (org-export-collect-footnote-definitions info))
|
||||
(fn-alist
|
||||
(cl-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"))))))
|
||||
|
||||
|
||||
;;;; Template
|
||||
|
||||
@ -262,12 +281,12 @@ 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 (lambda (headline)
|
||||
(org-gfm-format-toc headline info))
|
||||
headlines "\n")
|
||||
""))
|
||||
(toc-string (or (mapconcat 'org-gfm-format-toc headlines "\n") ""))
|
||||
(toc-tail (if headlines "\n\n" "")))
|
||||
(org-trim (concat toc-string toc-tail contents "\n" (org-gfm-footnote-section info)))))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
;;; Interactive function
|
||||
@ -299,16 +318,17 @@ non-nil."
|
||||
(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 ()
|
||||
"Convert the region to Github Flavored Markdown.
|
||||
This can be used in any buffer, this function assume that the
|
||||
current region has org-mode syntax. For example, you can write
|
||||
an itemized list in org-mode syntax in a Markdown buffer and use
|
||||
this command to convert it."
|
||||
"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.
|
||||
|
Loading…
Reference in New Issue
Block a user