0

When exporting an Org buffer to HTML, how can I inject a <style>-tag with many lines of contents into the head of the HTML document?

I know #+HTML_HEAD: and #+HTML_HEAD_EXTRA:. But that prefix must be added to all contents lines.

I also know about #+BEGIN_EXPORT html...#+END_EXPORT. But the contents of those blocks end up in the body of the HTML document.

  • Why have the style sheet explicitly in the org file? Why not create a style sheet file and then override/add to the default style sheet the way the documentation suggests: #+HTML_HEAD: <link rel="stylesheet" type="text/css" href="style1.css" /> #+HTML_HEAD_EXTRA: <link rel="alternate stylesheet" type="text/css" href="style2.css" />? – NickD Mar 27 at 19:26
  • @NickD Sometimes that is just the way it is and one has to live with it. In my case the HTML file is the embedded documentation of a Modelica type and that type can be moved around within MODELICAPATH. – Tobias Mar 27 at 19:48
0

I did not yet find a standard way.

So I added an attribute :head to HTML export blocks. If that attribute is set to t or yes the contents of the export block ends up in the <head> of the HTML document.

(defun org+-html-export-block (fun export-block contents info)
  "Take the \"head\" export attribute for EXPORT-BLOCK into account.
This is an around advice for `org-html-export-block'.
If the EXPORT-BLOCK has the head attribute
put its contents into the :html-head property of INFO."
  (when (string= (org-element-property :type export-block) "HTML")
    (let ((attributes (org-export-read-attribute :attr_html export-block)))
      (if (member (plist-get attributes :head) '("t" "yes"))
      (progn
        (plist-put info :html-head
               (org-remove-indentation (org-element-property :value export-block)))
        nil)
    (funcall fun export-block contents info)))))

(advice-add 'org-html-export-block :around #'org+-html-export-block)

Example Org document:

#+OPTIONS: html-style:nil html-scripts:nil
#+TITLE: Test for Org export blocks

* This is a test for Org Export Blocks.
Some block that ends up in the body:
#+BEGIN_EXPORT html
<p>This paragraph comes from the export block.</p>
#+END_EXPORT

Another block that ends up in the head:
#+ATTR_HTML: :head t
#+BEGIN_EXPORT html
<style>
p {
  color: red;
}
</style>
#+END_EXPORT

The generated HTML buffer:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2020-03-27 Fri 16:05 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Test for Org export blocks</title>
<meta name="generator" content="Org mode" />
<style>
p {
  color: red;
}
</style>
</head>
<body>
<div id="content">
<h1 class="title">Test for Org export blocks</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org27bee3e">1. This is a test for Org Export Blocks.</a></li>
</ul>
</div>
</div>

<div id="outline-container-org27bee3e" class="outline-2">
<h2 id="org27bee3e"><span class="section-number-2">1</span> This is a test for Org Export Blocks.</h2>
<div class="outline-text-2" id="text-1">
<p>
Some block that ends up in the body:
</p>
<p>This paragraph comes from the export block.</p>

<p>
Another block that ends up in the head:
</p>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="date">Created: 2020-03-27 Fri 16:05</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>
</html>
| improve this answer | |

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.