Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandoc - Inserting pages before generated Table of Contents

Tags:

I've been playing around with Pandoc.

Is there anyway to insert pages before the generated Table of Contents?

For example:

  • Title Page
  • Insert Custom Page 001
  • Insert Custom Page 002
  • (Generated) Table of Contents

Many thanks in advance!

like image 607
Andrew Vink Avatar asked Aug 31 '14 11:08

Andrew Vink


People also ask

Can pandoc convert HTML to markdown?

Pandoc can convert between numerous markup and word processing formats, including, but not limited to, various flavors of Markdown, HTML, LaTeX and Word docx.

Can pandoc convert PDF to markdown?

You can use the program pandoc on the SCF Linux and Mac machines (via the terminal window) to convert from formats such as HTML, LaTeX and Markdown to formats such as HTML, LaTeX, Word, OpenOffice, and PDF, among others.

What is a pandoc template?

Pandoc is a command line tool that you can use to automatically convert files from markup format to another. With Pandoc, you can write in something easy like Markdown, Microsoft Word, or LibreOffice, and convert it something hard like: HTML. Ebook formats.

How do I convert Word to markdown in pandoc?

As you can see, first, write pandoc to execute any pandoc command, and then write the file name of a file with extension which you want to convert to a markdown file. The -o stands for output file name and provide name of the file with extension to which it should be converted.


2 Answers

I am guessing you want to create a HTML File with Title Page or Header Information. The solution is to do it in more than one Step.

First you compile the Title Page together any other pages to one html.

$ pandoc .\01_HEADER.markdown -o header.html 

Then you include that header.html before the body:

$ pandoc -s -S --toc -B .\header.html .\02_Document.markdown -o complete_doc.html 

Done.


The pandoc help states it's possibilities to make multi-part documents possible, both -B and -H work, but i think -B is more correct in my case.

  -H FILENAME           --include-in-header=FILENAME                       -B FILENAME           --include-before-body=FILENAME                     -A FILENAME           --include-after-body=FILENAME                   

In my case i do it with a stylesheet and get a perfect document:

$ pandoc -s -S -c .\res\github-pandoc.css .\01_HEADER.markdown -o header.html $ pandoc -s -S --toc --toc-depth=2 -c .\res\github-pandoc.css -B .\header.html .\02_Document.markdown -o complete_document.html 
like image 102
muck Avatar answered Sep 27 '22 19:09

muck


For this answer, I'm going to assume you are generating markdown, though the process is the same for other file formats as well.

The key insight is that Pandoc uses templates to determine the final placement. It has default templates and if you modify them, you can change the order of sections.

  1. Find the default template

    > pandoc -D markdown $if(titleblock)$ $titleblock$  $endif$ $for(header-includes)$ $header-includes$  $endfor$ $for(include-before)$ $include-before$  $endfor$ $if(toc)$ $toc$  $endif$ $body$ $for(include-after)$  $include-after$ $endfor$ 

    You won't need this for the next step, but if you are curious where these files live, asking for a nonsense file type works (though I'm sure there is a better way):

    > pandoc -D nonsense pandoc: Could not find data file /usr/local/.../templates/default.nonsense 
  2. Copy and modify the default template

    > pandoc -D markdown > modified.markdown 

    Using your favorite text editor, you can open modified.markdown to put the $body$ before the $toc$.

    $body$ $if(toc)$ $toc$ $endif$ 
  3. Use the modified template

    > pandoc --template modified.markdown <... rest of your command ...> 
like image 43
Liyan Chang Avatar answered Sep 27 '22 18:09

Liyan Chang