【问题标题】:How to run org-mode commands from shell?如何从 shell 运行 org-mode 命令?
【发布时间】:2017-09-19 08:27:36
【问题描述】:

我想通过 bash 脚本运行 org-mode 命令。例如,在我定义的项目下,我有几个 org 模式文件要导出到 html。然后我将文件同步到网络服务器等。因此,无需打开 emacs 并将文件导出为 html 文件,我想从 bash 脚本中进行。

如何运行 org-mode 命令,例如

M-x org-publish-project

然后输入项目名称让我们说,trial_project。如何在 shell 脚本中执行此操作?

【问题讨论】:

    标签: shell emacs


    【解决方案1】:

    我发布了一个这样的项目:

    emacs -batch --load publish-doc.el --eval '(org-publish "publish-doc")'
    

    其中publish-doc.el 包含“publish-doc”项目的定义。如果你可以 ssh 到你的服务器,你甚至可以让publishing-directory 远程:org 将使用tramp 来复制文件。

    publish-doc.el 应该在必要时初始化 org-mode 并定义项目 - 详细信息可能取决于您是使用 emacs 附带的 org-mode,还是使用 MELPA 或 git repo 的上游版本。但是你应该能够模仿你在正常初始化中所做的事情并让它继续下去。在我的情况下,我必须修改我的加载路径以查找 org,因为我从上游 git repo 克隆:你可能根本不需要它,但如果你确实需要它,你需要修改路径到 where 你的组织是从加载的。以下最小的publish-doc.el~/org 中的一组组织文件发布到~/public_html - 这是改编自example in the manual,除了我必须添加发布功能:

    (add-to-list 'load-path "~/src/emacs/org/org-mode/lisp/")
    
    (require 'org)
    (require 'ox-html)
    
    (setq org-publish-project-alist
      '(("org"
         :base-directory "~/org/"
         :publishing-directory "~/public_html"
         :publishing-function org-html-publish-to-html
         :section-numbers nil
         :with-toc nil
         :html-head "<link rel=\"stylesheet\"
                 href=\"../other/mystyle.css\"
                 type=\"text/css\"/>")))
    

    然后是命令行调用

     emacs -batch -l publish-doc.el --eval '(org-publish "org")'
    

    HTH。

    编辑:如 cmets 中所述,在使用 -batch 时,您必须加载初始化文件:-batch 暗示 -q,因此您的 .emacs 或等效项不是 已加载。您可以使用-l ~/.emacs,但它通常包含很多无关的东西,因此在准备要从命令行运行的脚本时,大多数人更喜欢使用修剪过的初始化文件,其中只包含对手头的任务(参见上面的publish-doc.el 文件)。

    【讨论】:

    • 我想先导出到本地文件夹,然后通过重新同步将文件同步到服务器。但是,当我从 shell emacs --batch --eval '(org-publish-project "trial_project")' 运行时,我收到以下错误“符号的函数定义无效:org-publish”。你能详细解释一下 publish-doc.el 吗?
    • 编辑了答案以添加示例。希望对您有所帮助。
    • 感谢您提供详细信息,但我仍有问题。您在 publish-doc.el 中提到的函数在我的 .emacs 文件中定义。因此,在不加载函数的情况下,我运行 emacs --batch --eval '(org-publish-project "org")' 但我仍然收到错误代码“符号的函数定义无效:org-publish”
    • 你运行的是什么版本的组织模式?是否包含 (require ...) 行?
    • 当我在 emacs 中运行命令时,一切正常。例如在 emacs 中,M-x org-publish-project RET org 和所有 org 文件都转换为定义文件夹中的 html 文件。唯一的问题是我无法从 shell 命令执行此操作。我打算将我的 .emacs 文件的相关部分发送给您,但我不能,因为评论太长了。版本是 (elpa) org-20170925/ 。我在您发表评论后添加了 (require 'org),但仍然收到相同的警告。
    【解决方案2】:

    您可以编写一个 elisp 函数来打开必要的缓冲区,然后进行导出。然后,如果您的 emacs 正在运行,您可以使用 emacsclient -e ... 来运行该功能。如果您的 emacs 没有运行并且您想直接从命令行运行您的函数,您可以使用 emacs --batch --eval ... 来运行您的函数。

    【讨论】:

      猜你喜欢
      • 2016-07-10
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多