3

I have an org-file, which I've wrapped in a div for html export. This div is necessary because it is targeted by some custom css.

#+html: <div id="wrapper-div">

* Section One
* Section Two
* Etc.

#+html: </div>

The problem is that the </div> always gets folded into the last section. This wouldn't be a huge deal, but it's annoying when I add a new section to the end because this misplaces the </div> tag (being folded into the previous section).

Can I somehow mark the line, #+html: </div>, as non-collapsible?

1

You can not add something to the top-level after introducing a heading.

However, there might be another solution for you by advising the export functions. I prepared two examples on how to achieve what you want, since I do not know whether you want to cover the content including the table of content or not. The first (org-html-template-around) covers also the table of content, the second (org-html-inner-template-around) only the content.

(defun org-html-template-around (org-fun &rest args)
  (let ((content (car args))
        (info (cadr args)))
    (apply org-fun (list (concat "<div id=\"wrapper-div\">"
                             content
                             "</div>")
                         info))))
(advice-add 'org-html-template :around #'org-html-template-around)

(defun org-html-inner-template-around (org-fun &rest args)
  (let ((content (car args))
        (info (cadr args)))
    (apply org-fun (list (concat "<div id=\"inner-wrapper-div\">"
                             content
                             "</div>")
                         info))))
(advice-add 'org-html-inner-template :around #'org-html-inner-template-around)

You may want to set a customization variable, probably buffer local, to enable and disable your addition on a per file basis.

(defvar-local org-html-inner-template-around-id nil
  "If non nil: wrap html export in div with id given by this var.")
(defun org-html-inner-template-around (org-fun &rest args)
  (let ((content (car args))
        (info (cadr args)))
    (when org-html-inner-template-around-id
      (setq content (concat (format "<div id=\"%s\">" org-html-inner-template-around-id)
                             content
                             "</div>")))
    (apply org-fun (list content info))))
(advice-add 'org-html-inner-template :around #'org-html-inner-template-around-id)

Then you can set the variable in your org file, e.g. with the following first line:

# -*- org-html-inner-template-around-id: findme-as-div; -*-

See here for more information on specifying file variables.

If you use ox-twbs then advice org-twbs-template or org-twbs-inner-template respectively.

  • This is more complicated than I had hoped, but I appreciate the generality of your solution and your time in writing it out. Stupid question: where would the first two code blocks be defined? In the init file? – Chester Aug 23 '16 at 22:38
  • Yes, you can put the function definitions in your init.el. Be sure to have (require 'ox-html) before you try to advice-add. – theldoria Aug 24 '16 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.