hatmatrix
hatmatrix

Reputation: 44892

org-mode export to html: links open in new tabs?

In html created from org-mode, you can have links open in new tabs if specified as

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

which I found here.

However, this doesn't work if [[http://cnn.com][CNN]] is a bulleted item. For instance,

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

Or

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

1) How can I make this work, and 2) can I set this html attribute for all links on a particular page by specifying some form of this option at the top (possibly some argument to #+OPTIONS:)?

Upvotes: 3

Views: 1484

Answers (4)

user188432
user188432

Reputation: 39

I was struggling with the same problem and solved it by creating a filter for links as described in Advanced Export Configuration of Org Mode docs.

(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)

Upvotes: 1

joon
joon

Reputation: 4017

I found adding the following works:

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

Upvotes: 5

achempion
achempion

Reputation: 814

You can include html into your template if you have couple of links, like this

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

Upvotes: 2

artscan
artscan

Reputation: 2350

Short answer: replace string in function 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

Long comment about troubles. Let see source code of function, which parses #+ATTR_BACKEND into text properties.

(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 is HTML in this case. It works for such text

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

like this:

1) parse whole line #+ATTR_HTML: target="_blank" by regexp "^[ \t]*#\\+attr_"...

2) parse whole line [[http://cnn.com][CNN]] by regexp "^[ \t]*\\[\\[.*\\]\\][ \t]*$"

3) delete string #+ATTR_HTML: target="_blank" before export to html

4) set property target="_blank" for line [[http://cnn.com][CNN]]

And then org-mode prepares html link for export with this property.

If I replace string "^[ \t]*\\[\\[.*\\]\\][ \t]*$" by "^.*\\[\\[.*\\]\\][ \t]*$" then this patched function works for

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

too. But there is a problem for list

  - [[http://cnn.com][CNN]] 
  - [[http://cnn.com][CNN]]
  - some text

If I put ATTR_HTML before each link

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

then I get such output html

* CNN

* CNN
* some text

There is a extra gap in list. So, I can't get output like this

* CNN
* CNN
* some text

only

* CNN

* CNN

* some text

This example demonstrates that org-mode isn't flexible in some cases. I can write lisp function, which sets this html attribute for all links in exported text, and add this feature to #+OPTIONS: or something. But I can't complicate more and more org-mode exporting system in this way, because there are some org-mode syntax limitations - it is simple.

If I have problems with org-publish like these, I think: may be I need something else for make-up except org-mode? )

Upvotes: 2

Related Questions