#+TITLE: Colnames handling #+DATE: {{{modification-time(%Y-%m-%d)}}} #+AUTHOR: Rick Frankel #+EMAIL: ut0598@rtasdv12 #+OPTIONS: ':nil *:t -:t ::t <:t H:3 \n:nil ^:t arch:headline #+OPTIONS: author:t c:nil creator:comment d:(not LOGBOOK) date:t #+OPTIONS: e:t email:nil f:t inline:t num:nil p:nil pri:nil stat:t #+OPTIONS: tags:t tasks:t tex:t timestamp:nil toc:t todo:t |:t #+DESCRIPTION: #+EXCLUDE_TAGS: noexport #+KEYWORDS: #+LANGUAGE: en #+SELECT_TAGS: export Evaluate the subtree [[Test generator]] with =org-babel-execute-subtree= (=C-c C-v C-s=). It will: 1. Run [[generate-colnames-and-hlines-tests]] to create the [[colnames and hlines]] tests. 2. Run the tests. Note that it will automatically require the file "ob-{lang}" for each language block specified in [[Language functions]] below. * Language functions :PROPERTIES: :results: silent :exports: code :var: table=table :ID: LANGUAGES :END: This function should modify each cell of the input table by appending /-o/ to the value of the cell and convert ='hline= rows in the input to the literal /hline/, so it appears in the output table. Create one for each babel language to be tested. #+CAPTION: emacs-lisp #+BEGIN_SRC emacs-lisp (mapcar (lambda (r) (if (sequencep r) (mapcar (lambda (c) (if (integerp c) (format "%d-o" c) (concat c "-o"))) r) (list r))) table) #+END_SRC #+CAPTION: perl #+BEGIN_SRC perl :results value return [map { ref $_ ? [map { $_ . "-o" } @$_] : $_ } @$table]; #+END_SRC #+CAPTION: python #+name: python #+BEGIN_SRC python return [isinstance(r,list) and [str(c)+"-o" for c in r] or [r] for r in table] #+END_SRC #+CAPTION: ruby #+BEGIN_SRC ruby table.collect do |r| r.instance_of?(Array) ? r.collect { |c| "#{c}-o" } : [r] end #+END_SRC * Test generator #+CAPTION: Input table #+name: table | a | b | c | |---+---+---| | 1 | 2 | 3 | | 4 | 5 | 6 | |---+---+---| | 7 | 8 | 9 | #+CAPTION: Function to list default header args by language #+name: list-defaults #+HEADER: :var val='org-babel-default-header-args :eval never :exports code #+BEGIN_SRC emacs-lisp :colnames '(option value) (or (mapcar (lambda (x) (list (car x) (cdr x))) (eval val)) '(("" ""))) #+END_SRC #+name: generate-colnames-and-hlines-tests #+BEGIN_SRC emacs-lisp :results raw :exports results (condition-case nil (progn (widen) (org-id-goto "COLNAMES-AND-HLINES") (org-cut-subtree)) (error t)) (concat "* Test results\n" ":PROPERTIES:\n" ":ID: COLNAMES-AND-HLINES\n" ":END:\n" (let ((data ()) (defaults '("org-babel-default-header-args"))) (save-excursion (save-restriction (widen) (org-id-goto "LANGUAGES") (org-narrow-to-subtree) (org-babel-map-executables nil (let* ((info (org-babel-get-src-block-info 'light)) (lang (nth 0 info))) (push (format "org-babel-default-header-args:%s" lang) defaults) (require (intern (format "ob-%s" lang))) (push (concat (format "#+name: %s-%%s\n" lang) "#+HEADER: :results value :colnames %s :hlines %s\n" (format "#+BEGIN_SRC %s :var table=table\n" lang) (replace-regexp-in-string "^" " " (replace-regexp-in-string "%" "%%" (nth 1 info))) "\n#+END_SRC\n" "#+ATTR_LATEX: :placement [H]" (format "\n#+CAPTION: %s" lang) (format "\n#+RESULTS: %s-%%s\n" lang)) data))))) (concat "** Default header args\n" ":PROPERTIES:\n" ":colnames: yes\n" ":END:\n" "#+LaTeX: \\begin{multicols}{3}\n" (mapconcat (lambda (arg) (format "#+call: list-defaults[:eval yes]('%s)\n\n#+CAPTION: %s\n#+RESULTS:\n" arg arg)) (reverse defaults) "\n") "#+LaTeX: \\end{multicols}\n" (mapconcat 'identity (mapcar (lambda (arg) (let* ((colname (car arg)) (hlines (nth 1 arg)) (argname (format "%s-%s" (if (symbolp colname) (symbol-name colname) (replace-regexp-in-string "[' ()]" "" colname)) hlines))) (concat (format "** colnames %s, hlines %s\n" colname hlines) "#+LaTeX: \\begin{multicols}{3}\n" (mapconcat (lambda (block) (format block argname colname hlines argname)) (reverse data) "\n") "#+LaTeX: \\end{multicols}\n"))) (let ((l ())) (mapc (lambda (hline) (mapc (lambda (colname) (setq l (cons (list colname hline) l))) (list "'nil" 'yes 'no "'t" "'(d e f)"))) '(yes no)) (reverse l))) "\n")))) #+END_SRC #+RESULTS: generate-colnames-and-hlines-tests