组织模式导出为 html:链接在新选项卡中打开?

问题描述 投票:0回答:4

在从 org-mode 创建的 html 中,如果指定为

,您可以在新选项卡中打开链接
#+ATTR_HTML: target="_blank" 
[[http://cnn.com][CNN]]

我在这里找到的。

但是,如果

[[http://cnn.com][CNN]]
是项目符号项目,则此方法不起作用。例如,

#+ATTR_HTML: target="_blank" 
- [[http://cnn.com][CNN]]

或者

- #+ATTR_HTML: target="_blank" 
  [[http://cnn.com][CNN]]

1)我怎样才能做到这一点,2)我可以通过在顶部指定此选项的某种形式(可能是

#+OPTIONS:
的一些参数)来为特定页面上的所有链接设置此html属性吗?

org-mode
4个回答
4
投票

我发现添加了以下作品:

#+HTML_HEAD: <base target="_blank">

2
投票

简短回答:替换函数中的字符串

org-export-attach-captions-and-attributes

diff -u -L /home/eab/.emacs.d/el-get/org-mode/lisp/org-exp.el -L \#\<buffer\ el-get/org-exp.el\> /home/eab/.emacs.d/el-get/org-mode/lisp/org-exp.el /tmp/buffer-content-8644Ge2
--- /home/eab/.emacs.d/el-get/org-mode/lisp/org-exp.el
+++ #<buffer el-get/org-exp.el>
@@ -1935,7 +1935,7 @@
            "\\|"
            "^[ \t]*\\(|[^-]\\)"
            "\\|"
-           "^[ \t]*\\[\\[.*\\]\\][ \t]*$"))
+           "^.*\\[\\[.*\\]\\][ \t]*$"))
    cap shortn attr label end)
     (while (re-search-forward re nil t)
       (cond

关于麻烦的长评论。 让我们看一下函数的源代码,它将

#+ATTR_BACKEND
解析为文本属性。

(defun org-export-attach-captions-and-attributes (target-alist)
  "Move #+CAPTION, #+ATTR_BACKEND, and #+LABEL text into text properties.
If the next thing following is a table, add the text properties to the first
table line.  If it is a link, add it to the line containing the link."
  (goto-char (point-min))
  (remove-text-properties (point-min) (point-max)
              '(org-caption nil org-attributes nil))
  (let ((case-fold-search t)
    (re (concat "^[ \t]*#\\+caption:[ \t]+\\(.*\\)"
            "\\|"
            "^[ \t]*#\\+attr_" (symbol-name org-export-current-backend) ":[ \t]+\\(.*\\)"
            "\\|"
            "^[ \t]*#\\+label:[ \t]+\\(.*\\)"
            "\\|"
            "^[ \t]*\\(|[^-]\\)"
            "\\|"
            "^[ \t]*\\[\\[.*\\]\\][ \t]*$"))
...)))
在这种情况下,

org-export-current-backend
HTML
。 它适用于这样的文本

#+ATTR_HTML: target="_blank" 
[[http://cnn.com][CNN]]

像这样:

1)通过正则表达式解析整行

#+ATTR_HTML: target="_blank"
"^[ \t]*#\\+attr_"...

2)通过正则表达式解析整行

[[http://cnn.com][CNN]]
"^[ \t]*\\[\\[.*\\]\\][ \t]*$"

3) 在导出到 html 之前删除字符串

#+ATTR_HTML: target="_blank"

4) 设置线

target="_blank"

 的属性 
[[http://cnn.com][CNN]]

然后 org-mode 准备 html 链接以使用此属性导出。

如果我用

"^[ \t]*\\[\\[.*\\]\\][ \t]*$"

 替换字符串 
"^.*\\[\\[.*\\]\\][ \t]*$"
 那么这个修补函数适用于

#+ATTR_HTML: target="_blank" - [[http://cnn.com][CNN]]
也是。但是列表有问题

- [[http://cnn.com][CNN]] - [[http://cnn.com][CNN]] - some text
如果我在每个链接之前添加

ATTR_HTML



#+ATTR_HTML: target="_blank" - [[http://cnn.com][CNN]] #+ATTR_HTML: target="_blank" - [[http://cnn.com][CNN]] - some text
然后我得到这样的输出html

* CNN * CNN * some text
列表中有额外的空白。所以,我无法得到这样的输出

* CNN * CNN * some text

* CNN * CNN * some text
这个例子说明了 org-mode 在某些情况下并不灵活。我可以编写 lisp 函数,为导出文本中的所有链接设置此 html 属性,并将此功能添加到 

#+OPTIONS:

 或其他内容。但我不能以这种方式使越来越多的组织模式导出系统变得复杂,因为存在一些组织模式语法限制 - 它很简单。

如果我在 org-publish 上遇到这样的问题,我想:除了 org-mode 之外,我可能还需要其他东西来弥补吗? )


1
投票
如果您有几个链接,则可以将 html 包含到模板中,如下所示

My projects - @@html:<a href="https://example.com" target="_blank">Example</a>@@
    

0
投票
我正在努力解决同样的问题,并通过为链接创建过滤器来解决它,如

组织模式文档的高级导出配置中所述。

(defun link-filter-external (text backend info) "External links that start with https should have attribute _target set to blank." (when (org-export-derived-backend-p backend 'html) (replace-regexp-in-string "<a href=\"https://" "<a target=\"_blank\" href=\"https://" text))) (add-to-list 'org-export-filter-link-functions 'link-filter-external)
    
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.