Difference between revisions of "Convert text documents with Pandoc"

From Parallel Library Services
Jump to navigation Jump to search
Line 3: Line 3:
Pandoc is a "universal document converter" which converts from one markup language (e.g. [[HTML]], [[Markdown]] to another. Here are some basic recipes for converting documents.
Pandoc is a "universal document converter" which converts from one markup language (e.g. [[HTML]], [[Markdown]] to another. Here are some basic recipes for converting documents.


You can find instructions for installation on [https://pandoc.org/installing.html the Pandoc website] for your particular operating system. Once you have pandoc installed, open a terminal session to use its command line interface.
You can find instructions for installation on [https://pandoc.org/installing.html the Pandoc website] for your particular operating system. Once you have pandoc installed, open a terminal session to use its [[command line]] interface.


More extensive documentation is available in [https://pandoc.org/MANUAL.html the official Pandoc manual] or through the [[command line]] by typing  
More extensive documentation is available in [https://pandoc.org/MANUAL.html the official Pandoc manual] or through the [[command line]] by typing  
Line 15: Line 15:
<code>-t</code> or <code>--to</code> Option which is followed by the output format;
<code>-t</code> or <code>--to</code> Option which is followed by the output format;


<code>-s</code> or <code>--standalone</code> Option produces output with an appropriate header and footer;
<code>-o</code> or <code>--output</code> Option for file output


<code>-o</code> or <code>--output</code> Option for file output
For example, using the command tells pandoc to use the file example.md, change it from markdown to html, with the file output to be example.html:
 
<pre>pandoc example.md -f markdown -t html -o example.html</pre>
 
This will save a new version of the file, in html in the same directory the command is run from.


== Convert plain text files (.txt) that are structured with [[Markdown]] to PDF ==
== Convert plain text files (.txt) that are structured with [[Markdown]] to PDF ==
Line 24: Line 28:
<pre>brew install BasicTex</pre>
<pre>brew install BasicTex</pre>


Then, do:
This may take a while, and after installing BasicTex, you should close the terminal session and then begin a new one.
 
Then, once you are in a directory that contains a .txt file (e.g. MANUAL.txt) you want to convert,


<pre>pandoc MANUAL.txt --pdf-engine=xelatex -o example13.pdf</pre>
<pre>pandoc MANUAL.txt --pdf-engine=xelatex -o example13.pdf</pre>

Revision as of 14:39, 11 October 2021

https://pandoc.org

Pandoc is a "universal document converter" which converts from one markup language (e.g. HTML, Markdown to another. Here are some basic recipes for converting documents.

You can find instructions for installation on the Pandoc website for your particular operating system. Once you have pandoc installed, open a terminal session to use its command line interface.

More extensive documentation is available in the official Pandoc manual or through the command line by typing

man pandoc

Common pandoc arguments

-f or --from Option which is followed by the input format;

-t or --to Option which is followed by the output format;

-o or --output Option for file output

For example, using the command tells pandoc to use the file example.md, change it from markdown to html, with the file output to be example.html:

pandoc example.md -f markdown -t html -o example.html

This will save a new version of the file, in html in the same directory the command is run from.

Convert plain text files (.txt) that are structured with Markdown to PDF

For this, you need to have an up-to-date version of BasicTeX installed. On Mac, first install with homebrew:

brew install BasicTex

This may take a while, and after installing BasicTex, you should close the terminal session and then begin a new one.

Then, once you are in a directory that contains a .txt file (e.g. MANUAL.txt) you want to convert,

pandoc MANUAL.txt --pdf-engine=xelatex -o example13.pdf

Convert downloaded wiki pages (plain text in the .wiki format) to HTML files

Example 1: Convert an HTML string to Markdown

Enter a string of HTML and pipe it to pandoc:

echo "<h1>Hello Pandoc</h1><p>from html to markdown</p>" | pandoc -f html -t markdown

Example 2: Convert a MediaWiki file to HTML

  1. Save the content of a wiki page on to a plain-text file, example: page.wiki
  2. Convert:
pandoc page.wiki -f mediawiki -t html -o page.html