Pandoc Mode Manual
Pandoc Mode Manual
Pandoc Mode Manual
1 Introduction
pandoc-mode is an Emacs mode for interacting with Pandoc. Pandoc is a program (plus libraries) created by John MacFarlane that can convert a text written in one markup language into another markup language. Supported input formats are markdown and (subsets of) reStructuredText, HTML, and LaTeX. Supported output formats are, among others, markdown, HTML(5), LaTeX, PDF, OpenOffice.org text document (odt), MS Word (docx), GNU Texinfo, MediaWiki markup, groff man pages, and several slide show formats. pandoc-mode is implemented as a minor mode that can be activated alongside the major mode for any of the supported input formats. It provides facilities to set the various options that Pandoc accepts and to run Pandoc on the input file. It is possible to create different output profiles for a single input file, so that you can, for example, write your text in markdown and then translate it to HTML for online reading, PDF for offline reading and Texinfo for reading in Emacs. The current version of pandoc-mode is 2.1.1 and is compatible with Pandoc version 1.9.
2 Installation
Installing pandoc-mode is easy: just put pandoc-mode.el into Emacs loadpath (you can byte-compile it if you wish) and add the following line to : (load "pandoc-mode") This command simply loads pandoc-mode, it obviously does not activate it. In order to activate it in a buffer, you need to type M-x pandoc-mode. If you want to start pandoc-mode automatically when you load e.g., a markdown file, you can add a hook to your : 1
(add-hook 'markdown-mode-hook 'turn-on-pandoc) However, if you do not want to start pandoc-mode every time you work on a markdown document, you can use a different function in markdown-mode-hook: instead of using turn-on-pandoc, you can use conditionally-turn-on-pandoc. This function checks if a default settings file exists for the file youre opening and only turns on pandoc-mode if it finds one. (For more info on the settings file, see the section Settings Files.) Additionally, if you want to automatically load a pandoc-mode settings file for the file youre opening, you can add the following to your : (add-hook 'pandoc-mode-hook 'pandoc-load-default-settings) The function pandoc-load-default-settings checks if a default settings file exists for the file being loaded and reads its settings if it finds one.
3 Usage
When you start pandoc-mode, a menu appears through which all of Pandocs options can be set and through which you can run Pandoc on your current document, or load or save settings files. The most important functions provided by pandoc-mode can also be accessed through the keyboard, but setting options can only be done through the menu (although it is possible to bind your own keys to the functions to set options; see the section Using The Keyboard). The menu is divided into two parts. The upper half contains items related to running Pandoc, the lower half is the part where the various settings can be set and changed.
name of the output format and the string .pandoc. (See the section Settings Files for details.) A single document can have a settings file for each output format that Pandoc supports. For example, for this manual, which is written in markdown, I have three settings files, one for HTML output, one for LaTeX output and one for Texinfo output. These can simply be created by setting all options the way you want them for the first output format, save them, then choose another output format, set the required options, save again, etc. Because the name of a settings file contains the output format for which it was created, the different settings files wont interfere with each other. On systems that have symbolic links, it is also possible to specify a default output format. By selecting Set As Default Format from the Pandoc menu, a symbolic link is created to the settings file of the current output format (a settings file is created if one doesnt exist yet). This symbolic link has default as format in its name. The file it points to is read by the function pandoc-load-default-settings, making it possible to automatically load a specific settings file when pandoc-mode is invoked. When you switch output formats, either through the menu or with the keyboard (with C-c / w), Emacs checks if a corresponding settings file exists and loads it if one is found. That is, you can load a different settings file by simply switching output formats. Note that the current output format is always visible in the mode line: the lighter for pandoc-mode in the mode line has the form Pandoc/<format>, where <format> is the current output format.
to a server, where it will most likely be in a different directory than on the computer youre generating your .html-files on. Under Options, you can set various other options. Some of these require user input, others can only be toggled on or off. Furthermore, the menu contains an option to get a list of all the settings that you have defined (this function is also available by typing C-c / S). This displays all settings in the *Pandoc output* buffer in the same manner in which they appear in a settings file, i.e., with the following format: <option>::<value>
settings in it will be passed to Pandoc instead of the settings in the current buffer. If there is no settings file, Pandoc will be called with just the output format and no other options. Note that specifying an output format this way does not change the output format or any of the settings in the buffer, it just changes the output profile used for calling Pandoc. This can be useful if you use different output formats but dont want to keep switching between profiles when creating the different output files.
If your current output format is not LaTeX and no LaTeX settings file is found, Emacs calls Pandoc with only the input and output formats. All other options are unset. (Note that you can force Emacs to check for a LaTeX settings file by calling C-c / p with a prefix argument.)
4 Projects
If you have more than one file in a single directory for which you want to apply the same Pandoc settings, it is rather cumbersome to create a settings file for each of them, and even more cumbersome if you want to change something in those settings. To deal with such cases, pandoc-mode allows you to create a project file. A project file is called Project.<format>.pandoc and is essentially a normal settings file. The difference is that it defines settings that apply to all files in the directory. In order to distinguish settings from a project file and settings from a file-specific settings file, the former are called project settings and the latter local settings. You can create a project file by selecting Project | Save Project File from the menu (or with the key sequence C-c / P s). Emacs then simply saves the settings of the current file to the project file. This means that all the settings for the current input file become project settings. Just like a local settings file, a project file also contains an output format in its filename. This means that you can have different project files for different output formats. Furthermore, it is also possible to define a default project format: when you set a default format through the menu, Emacs makes both the project file and the local settings file for the current output format the default. If there is no project file for the current output format, however, a default will not be created. (This differs from local settings files: if you set a default output format, Emacs will create a local settings file if none exists). When Emacs loads Pandoc settings for a file, it first looks for and loads the project file for the selected output format, and then reads the files local settings file, if one exists. This means that the local settings may actually override the project settings: if both files contain a value for a specific option, the one in the local settings file overrides the one in the project file. This means that if you open a file and a project settings file is loaded for it, you can make changes to the options and save those to a local settings file (with the menu option Save File Settings or the key sequence C-c / s). If you want to undo all filespecific settings and return to the settings defined in the project file, you can select the menu item Project | Undo File Settings (C-c / P u). This erases the local options, but only for the current session. If you want to make the change permanent, you need to save the local settings file (which will then be empty) or just delete it completely.
6 Using @@-directives
pandoc-mode includes a facility to make specific, automatic changes to the text before sending it to Pandoc. This is done with so-called @@-directives (double-at directives), which trigger an Elisp function and are then replaced with the output of that function. A @@-directive takes the form @@directive, where directive can be any user-defined string. Before Pandoc is called, Emacs searches the text for these directives and replaces them with the output of the functions they call. So suppose you define (e.g., in ) a function pandoc-current-date: (defun pandoc-current-date (output-format) (format-time-string "%d %b %Y")) Now you can define a directive @@date that calls this function. The effect is that every time you write @@date in your document, it is replaced with the current date. Note that the function that the directive calls must have one argument. which is used to pass the output format to the function (as a string). This way you can have your directives do different things depending on the output format. @@-directives can also take the form @@directive{...}. Here, the text between curly braces is an argument, which is passed to the function that the directive calls as its second argument. Note that there should be no space between the directive and the left brace. If there is, Emacs wont see the argument and will treat it as normal text.
It is possible to define a directive that can take an optional argument. This is simply done by defining the argument that the directives function takes as optional. Suppose you define pandoc-current-date as follows: (defun pandoc-current-date (output-format &optional text) (format "%s%s" (if text (concat text ", ") "") (format-time-string "%d %b %Y"))) This way, you could write @@date to get just the date, and @@date{Cologne} to get Cologne, 28 Feb 2012. Two directives have been predefined: @@lisp and @@include. Both of these take an argument. @@lisp can be used to include Elisp code in the document which is then executed and replaced by the result (which should be a string). For example, another way to put the current date in your document, without defining a special function for it, is to write the following: @@lisp{(format-time-string "%d %b %Y")} Emacs takes the Elisp code between the curly braces, executes it, and replaces the directive with the result of the code. @@include can be used to include another file into the current document (which must of course have the same input format): @@include{copyright.text} This directive reads the file copyright.text and replaces the @@include directive with its contents. Processing @@-directives works everywhere in the document, including in code and code blocks, and also in the %-header block. So by putting the above @@lisp directive in the third line of the %-header block, the meta data for your documents will always show the date on which the file was created by Pandoc. If it should ever happen that you need to write a literal @@lisp in your document, you can simply put a backslash \ before the first @: \@@lisp. Emacs removes the backslash (which is necessary in case the string \@@lisp is contained in a code block) and then continues searching for the next directive. The directives are processed in the order in which they appear in the customization buffer (and hence in the variable pandoc-directives). So in the default case, @@include directives are processed before @@lisp directives, which means that any @@lisp directive in a file included by @@include gets processed, but if a @@lisp directive produces an @@include, it does not get processed. (If this should ever be a problem, you can always create a directive @@include2 and have it processed after @@lisp.) 8
After Emacs has processed a directive and inserted the text it produced in the buffer, processing of directives is resumed from the start of the inserted text. That means that if an @@include directive produces another @@include directive, the newly inserted @@include directive gets processed as well.
C-u - (or M--) to unset the relevant option, and with any other prefix key to set the default value (if the option has one, of course). The option setting functions are all called pandoc-set-<option> (with the exception of the option --read, i.e., the input format, which is determined automatically and rarely needs to be set by the user). <option> corresponds to the long name of the relevant Pandoc switch. Functions can be bound in the following manner: (define-key 'pandoc-mode-map "\C-c/o" 'pandoc-set-output) The following table lists the keys defined by default: C-c/r Run pandoc on the document C-c/p Run markdown2pdf on the document C-c/s Save the settings file C-c/Ps Save the project file C-c/Pu Remove file-specific settings C-c/w Set the output format C-c/v Set a template variable C-c/V View the output buffer C-c/S View the current settings C-c/c Insert a new (@)-item C-c/C Select and insert a (@)-label
8 Settings Files
As explained above, there are two types of settings files: project files, which apply to all input files in a single directory, and local settings files, which apply only to single input files. Both types of settings files are specific to a single output format, which is specified in the name. Local settings files are hidden (on Unix-like OSes, anyway) and consist of the name of the input file, plus a string indicating the output format and the suffix .pandoc. Project files are not hidden files and consist of the string Project plus the output format and the suffix .pandoc. The format of a settings file is very straightforward. It contains lines of the form: <option>::<value>
10
<option> is one of Pandocs long options without the two dashes. (For the input and output formats, the forms read and write are used, not the alternative forms from and to. Additionally, the option output-dir is valid.) <value> is a string or, for binary switches, either t or nil, which correspond to on or off, respectively. Lines that do not correspond to this format are simply ignored (and can be used for comments). If for some reason you end up writing your own settings file by hand, make sure all your options have the right form, that is, that they contain no spaces and a double colon.
11