2

我正在尝试编写一个 elisp 函数来从 org 文件中提取文本。具体来说,我希望能够将 org-table 中的值转换为列表列表,以便我可以在启动时从我的 .emacs 之外的文件中填充 org-feeds-alist。

我的功能如下所示:

(defun·org-config-parse-get-table-rows·(file)
"Return·table·rows·minus·header"
(with-temp-buffer
··(insert-file-contents·file)
··(cddr·(org-element-map·(org-element-parse-buffer)·'(table-row)·'identity))))

(defun·org-config-parse-get-table-cells·(file)
··(org-element-map·(org-config-parse-get-table-rows·file)·'(table-cell)·'identity))

我正在使用下表对其进行测试:

|·Name···········|·Fav·Color·|·Age·|·Sex····|
|----------------+-----------+-----+--------|
|·Jim············|·Blue······|··19·|·Male···|
|·Jane···········|·Green·····|··18·|·Female·|
|·Ort'hlrothl'gr·|·Unkown····|·-29·|·???····|

我能够检索表中单个单元格的文本的最接近的是以下内容:

(car·(last·(car·(org-config-parse-get-table-cells·"test.org"))))

评估为:

#("Jim" 0 3 
  (:parent 
    ...

给定 org-element-parse-buffer 返回的 org 元素列表,将这些元素的文本提取为字符串的正确方法是什么?

提前致谢。

4

1 回答 1

1

据我了解,您需要 org-mode 表中的列表列表。以下是相关位org-table-export

(defun orgtbl->lists ()
  (unless (org-at-table-p) (user-error "No table at point"))
  (org-table-align)
  (let* ((beg (org-table-begin))
     (end (org-table-end))
     (txt (buffer-substring-no-properties beg end))
         (skip nil)
         (lines (nthcdr 0 (org-split-string txt "[ \t]*\n[ \t]*")))
         (lines (org-table-clean-before-export lines))
         (i0 (if org-table-clean-did-remove-column 2 1))
         (table (mapcar
                 (lambda (x)
                   (if (string-match org-table-hline-regexp x)
                       'hline
                     (org-remove-by-index
                      (org-split-string (org-trim x) "\\s-*|\\s-*")
                      nil i0)))
                 lines)))
    table))

如果仍然缺少某些内容,您可以查看原始功能。

于 2013-10-05T12:07:12.127 回答