Skip to content

Instantly share code, notes, and snippets.

@sigma
Created June 23, 2014 00:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sigma/1bff5456162c6b74676b to your computer and use it in GitHub Desktop.
Save sigma/1bff5456162c6b74676b to your computer and use it in GitHub Desktop.
#emacs ReST support for #org tables
(defun yh/orgtbl-to-rst-paddings (table)
(let* ((pruned-table (remove 'hline table))
(size-table (mapcar (lambda (row)
(mapcar #'length row))
pruned-table)))
(apply #'mapcar* #'max size-table)))
(defun yh/orgtbl-padded-hline (paddings &optional chr)
(let ((chr (or chr ?-)))
(concat (format "+%c" chr)
(mapconcat (lambda (size)
(make-string size chr)) paddings
(format "%c+%c" chr chr))
(format "%c+" chr))))
(defun yh/orgtbl-to-rst (table params)
"Convert the Orgtbl mode TABLE to ReST."
(let* ((indent (make-string (or (plist-get params :indent) 0) ?\ ))
(paddings (yh/orgtbl-to-rst-paddings table))
(hline (concat indent (yh/orgtbl-padded-hline paddings)))
(hlend (concat indent (yh/orgtbl-padded-hline paddings ?=)))
(lfmt (concat indent "| "
(mapconcat (lambda (size)
(format "%%-%ds" size)) paddings
" | ") " |"))
(hlfmt (concat lfmt "\n" hlend))
(params2
(list
:tstart (concat "\n" hline) :tend (concat hline "\n") :hline hline
:lfmt lfmt :hlfmt hlfmt :skipheadrule t)))
(orgtbl-to-generic table (org-combine-plists params2 params))))
(push `(rst-mode ,(concat
".. BEGIN RECEIVE ORGTBL %n\n"
"\n"
".. END RECEIVE ORGTBL %n\n"
"\n"
"..\n"
" #+ORGTBL: SEND %n yh/orgtbl-to-rst :splice nil :skip 0\n"
" | | |\n"))
orgtbl-radio-table-templates)

Just a trivial test

Header row, column 1 Header 2 Header 3 Header 4
row 1, column1 column 2 column 3 column 4
body row 2 ... ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment