Function: orgtbl-to-generic
orgtbl-to-generic is an autoloaded and byte-compiled function defined
in org-table.el.gz.
Signature
(orgtbl-to-generic TABLE PARAMS)
Documentation
Convert the orgtbl-mode(var)/orgtbl-mode(fun) TABLE to some other format.
This generic routine can be used for many standard cases.
TABLE is a list, each entry either the symbol hline for
a horizontal separator line, or a list of fields for that
line. PARAMS is a property list of parameters that can
influence the conversion.
Valid parameters are all the export options understood by the export backend and also:
:backend, :raw
Export backend used as a basis to transcode elements of the
table, when no specific parameter applies to it. It is also
used to translate cells contents. You can prevent this by
setting :raw property to a non-nil value.
:splice
When non-nil, only convert rows, not the table itself. This is
equivalent to setting to the empty string both :tstart
and :tend, which see.
:skip
When set to an integer N, skip the first N lines of the table.
Horizontal separation lines do count for this parameter!
:skipcols
List of columns that should be skipped. If the table has
a column with calculation marks, that column is automatically
discarded beforehand.
:hline
String to be inserted on horizontal separation lines. May be
nil to ignore these lines altogether.
:sep
Separator between two fields, as a string.
Each in the following group may be either a string or a function of no arguments returning a string:
:tstart, :tend
Strings to start and end the table. Ignored when :splice is t.
:lstart, :lend
Strings to start and end a new table line.
:llstart, :llend
Strings to start and end the last table line. Default,
respectively, to :lstart and :lend.
Each in the following group may be a string or a function of one argument (either the cells in the current row, as a list of strings, or the current cell) returning a string:
:lfmt
Format string for an entire row, with enough %s to capture all
fields. When non-nil, :lstart, :lend, and :sep are ignored.
:llfmt
Format for the entire last line, defaults to :lfmt.
:fmt
A format to be used to wrap the field, should contain %s for
the original field value. For example, to wrap everything in
dollars, you could use :fmt "$%s$". This may also be
a property list with column numbers and format strings, or
functions, e.g.,
(:fmt (2 "$%s$" 4 (lambda (c) (format "$%s$" c))))
The format is ignored for empty fields. Use :raw t with non-nil
:backend option to force formatting empty fields.
:hlstart :hllstart :hlend :hllend :hsep :hlfmt :hllfmt :hfmt
Same as above, specific for the header lines in the table.
All lines before the first hline are treated as header. If
any of these is not present, the data line value is used.
This may be either a string or a function of two arguments:
:efmt
Use this format to print numbers with exponential. The format
should have %s twice for inserting mantissa and exponent, for
example "%s\\\\times10^{%s}". This may also be a property
list with column numbers and format strings or functions.
:fmt will still be applied after :efmt.
Source Code
;; Defined in /usr/src/emacs/lisp/org/org-table.el.gz
;;;###autoload
(defun orgtbl-to-generic (table params)
"Convert the `orgtbl-mode' TABLE to some other format.
This generic routine can be used for many standard cases.
TABLE is a list, each entry either the symbol `hline' for
a horizontal separator line, or a list of fields for that
line. PARAMS is a property list of parameters that can
influence the conversion.
Valid parameters are all the export options understood by the export
backend and also:
:backend, :raw
Export backend used as a basis to transcode elements of the
table, when no specific parameter applies to it. It is also
used to translate cells contents. You can prevent this by
setting :raw property to a non-nil value.
:splice
When non-nil, only convert rows, not the table itself. This is
equivalent to setting to the empty string both :tstart
and :tend, which see.
:skip
When set to an integer N, skip the first N lines of the table.
Horizontal separation lines do count for this parameter!
:skipcols
List of columns that should be skipped. If the table has
a column with calculation marks, that column is automatically
discarded beforehand.
:hline
String to be inserted on horizontal separation lines. May be
nil to ignore these lines altogether.
:sep
Separator between two fields, as a string.
Each in the following group may be either a string or a function
of no arguments returning a string:
:tstart, :tend
Strings to start and end the table. Ignored when :splice is t.
:lstart, :lend
Strings to start and end a new table line.
:llstart, :llend
Strings to start and end the last table line. Default,
respectively, to :lstart and :lend.
Each in the following group may be a string or a function of one
argument (either the cells in the current row, as a list of
strings, or the current cell) returning a string:
:lfmt
Format string for an entire row, with enough %s to capture all
fields. When non-nil, :lstart, :lend, and :sep are ignored.
:llfmt
Format for the entire last line, defaults to :lfmt.
:fmt
A format to be used to wrap the field, should contain %s for
the original field value. For example, to wrap everything in
dollars, you could use :fmt \"$%s$\". This may also be
a property list with column numbers and format strings, or
functions, e.g.,
(:fmt (2 \"$%s$\" 4 (lambda (c) (format \"$%s$\" c))))
The format is ignored for empty fields. Use :raw t with non-nil
:backend option to force formatting empty fields.
:hlstart :hllstart :hlend :hllend :hsep :hlfmt :hllfmt :hfmt
Same as above, specific for the header lines in the table.
All lines before the first hline are treated as header. If
any of these is not present, the data line value is used.
This may be either a string or a function of two arguments:
:efmt
Use this format to print numbers with exponential. The format
should have %s twice for inserting mantissa and exponent, for
example \"%s\\\\times10^{%s}\". This may also be a property
list with column numbers and format strings or functions.
:fmt will still be applied after :efmt."
;; Make sure `org-export-create-backend' is available.
(require 'ox)
(let* ((backend (plist-get params :backend))
(custom-backend
;; Build a custom backend according to PARAMS. Before
;; defining a translator, check if there is anything to do.
;; When there isn't, let BACKEND handle the element.
(org-export-create-backend
:parent (or backend 'org)
:transcoders
`((table . ,(org-table--to-generic-table params))
(table-row . ,(org-table--to-generic-row params))
(table-cell . ,(org-table--to-generic-cell params))
;; Macros are not going to be expanded. However, no
;; regular backend has a transcoder for them. We
;; provide one so they are not ignored, but displayed
;; as-is instead.
(macro . (lambda (m c i) (org-element-macro-interpreter m nil)))
;; Only export the actual table. Do nothing with the
;; containing section regardless what backend think about
;; it. (It is somewhat like BODY-ONLY argument in
;; `org-export-as', but skips not only transcoding the
;; full document, but also section containing the table.
(section . (lambda (_ contents _) contents))))))
;; Store TABLE as Org syntax in DATA. Tolerate non-string cells.
;; Initialize communication channel in INFO.
(with-temp-buffer
(let ((standard-output (current-buffer)))
(dolist (e table)
(cond ((eq e 'hline) (princ "|--\n"))
((consp e)
(princ "| ") (dolist (c e) (princ c) (princ " |"))
(princ "\n")))))
(let ((org-inhibit-startup t)) (org-mode))
(defvar org-export-before-processing-functions) ; ox.el
(defvar org-export-process-citations) ; ox.el
(defvar org-export-expand-links) ; ox.el
(defvar org-export-filter-parse-tree-functions) ; ox.el
(defvar org-export-filters-alist) ; ox.el
(defvar org-export-replace-macros) ; ox.el
(declare-function
org-export-as "ox"
(backend &optional subtreep visible-only body-only ext-plist))
;; We disable the usual pre-processing and post-processing,
;; i.e., hooks, Babel code evaluation, and macro expansion.
;; Only backend specific filters are retained.
;; We _do not_ disable `org-export-filter-parse-tree-functions'
;; (historically).
(let ((org-export-before-processing-functions nil)
(org-export-replace-macros nil)
(org-export-use-babel nil)
(org-export-before-parsing-functions nil)
(org-export-process-citations nil)
(org-export-expand-links nil)
(org-export-filter-parse-tree-functions
(append
'(orgtbl--skip orgtbl--skipcols)
org-export-filter-parse-tree-functions))
(org-export-filters-alist
'((:filter-parse-tree . org-export-filter-parse-tree-functions))))
(when (or (not backend) (plist-get params :raw)) (require 'ox-org))
(when (and backend (symbolp backend) (not (org-export-get-backend backend)))
(user-error "Unknown :backend value: %S" backend))
(let ((output (org-export-as custom-backend nil nil 'body-only params)))
;; Remove final newline.
(if (org-string-nw-p output) (substring-no-properties output 0 -1) ""))))))