【问题标题】:Getting text of org-mode elements using org-element-map使用 org-element-map 获取 org-mode 元素的文本
【发布时间】:2013-10-05 04:02:24
【问题描述】:

我正在尝试编写一个 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 元素列表,将这些元素的文本提取为字符串的正确方法是什么?

提前致谢。

【问题讨论】:

    标签: emacs lisp elisp org-mode


    【解决方案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))
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多