Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include meta tags in Pandoc-generated HTML?

Tags:

pandoc

I am using Pandoc to write the contents of a site. How do I include meta tags (specifically, description and keywords tags) on a document, without changing the command line arguments passed to Pandoc?

I mean, can I include meta tags somehow in the document text? I don't want to pass command line options, because there are several different pages, with different keywords, that I'd like to send to Pandoc from within Emacs, and customizing each of them would be a problem.

like image 680
Jay Avatar asked Aug 20 '14 17:08

Jay


People also ask

Are meta tags automatically generated?

Meta-description Tags Although Google does not always display the content that the creator has proposed, sometimes it can be generated automatically through the content of the page if they value that those generated by the user are not of enough "quality" or are not related to the user's query.

How do I add a meta tag to my header?

Add Meta Tags WordPress Setup by Accessing Site FilesYou want to locate the Theme Header file and open that up. Look for header. php and click on it to open the file. You will enter meta tags in the head tag of the header.

What is meta generator tag?

Meta tags are HTML tag content that provide metadata about your website such as description. Meta tags are used by search engines to help index and to provide relevant content in their search results. Site Title. Site Description.


2 Answers

I've found that adding the --self-contained or -s option to the pandoc command allows header contents to be defined per-file in YAML at the top.

For example:

$ cat foo.md
---
title: Foo
header-includes:
    <meta name="keywords" content="Foo,Bar" />
    <meta name="description" content="My description" />
---

# Bar #

Baz
$ pandoc -s -o foo.html foo.md
$ cat foo.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Style-Type" content="text/css" />
  <meta name="generator" content="pandoc" />
  <title>Foo</title>
  <style type="text/css">code{white-space: pre;}</style>
  <meta name="keywords" content="Foo,Bar" /> <meta name="description" content="My description" />
</head>
<body>
<div id="header">
<h1 class="title">Foo</h1>
</div>
<h1 id="bar">Bar</h1>
<p>Baz</p>
</body>
</html>
like image 182
Warbo Avatar answered Oct 12 '22 01:10

Warbo


Ok -- so option 1 suggested by David Cain seems like a reasonably easy solution. My implementation of it is a bit ugly, but works:

First, use YAML headers with a field name ending in underscore to add a header line. The Pandoc manual says that these identifiers will be ignored.

---
head_: <meta name="description" content="x is super cool">
head_: <meta name="keywords"    content="cool,cold,temperature,super things">
---

Make Emacs search for it in the current buffer and save the line to a file.

(defvar my-markdown-header-file "head.html")

(defun my-markdown-add-headers ()
  (if (file-exists-p my-markdown-header-file)
      (delete-file   my-markdown-header-file))
  (append-to-file "" nil my-markdown-header-file)
  (save-excursion
    (goto-char 1)
    (while (re-search-forward "head_:" nil t)
      ;; get the first and last positions:
      (let ((start (point))
            (end   (progn (end-of-line) (point))))
      ;; include this line, and a newline after it:
      (append-to-file start end my-markdown-header-file)
      (append-to-file "\n"  nil my-markdown-header-file)))))

(add-hook 'markdown-before-export-hook 'my-markdown-add-headers)

(My elisp ability is not that great, so there probably are better ways of writing this)

Finally -- use pandoc -s -H head.html as markdown command in Emacs markdown-mode.

Thanks to David Cain for suggesting the -H option!

edit: as a bonus, we get to include anything in the headers, including favicons!

head_: <link rel="icon" type="image/x-icon" href="favicon.ico" />
like image 31
Jay Avatar answered Oct 12 '22 03:10

Jay