Skip to content

Instantly share code, notes, and snippets.

@mahmoudimus
Forked from JayKickliter/emacs-gfm.md
Last active November 13, 2022 18:51
Show Gist options
  • Save mahmoudimus/3033de41a9ca89e9102f37200ecad3db to your computer and use it in GitHub Desktop.
Save mahmoudimus/3033de41a9ca89e9102f37200ecad3db to your computer and use it in GitHub Desktop.
Render github-flavored markdown (GFM) in emacs using `pandoc`

Command line usage

pandoc pandoc --quiet -f gfm -s somefile.md

What the options mean:

  1. --quiet: supress "[WARNING] This document format requires a nonempty <title> element."
  2. -f gfm: input format is Github Flavored Markdown
  3. -s: create standalone HTML5 with the formatting magic embedded in it
  4. somefile.md: the actual document you want to render

Emacs integration

In your init file

Using custom

  1. M-x customize-variable RET markdown-command RET
  2. Enter pandoc --quiet -f gfm -s in box like so:
For help using this buffer, see [Easy Customization] in the [Emacs manual].

                                         [ Search ]

Operate on all settings in this buffer:
[ Revert... ] [ Apply ] [ Apply and Save ]

Hide Markdown Command: pandoc --quiet -f gfm -s
   [ State ]: SAVED and set.
   Command to run markdown.
Groups: [Markdown]
"""
A simple preprocessor to use with Marked2 to get paths to citation files right based on relative files in pandoc metadata.
e.g., if you have a markdown file like this::
---
title: "My Awesome Research Paper"
bibliography: bib-csl.json
---
Yadd a yadda yadda [@somecite]. Yadda. Yeah.
where ``bib-csl.json`` has your references (could also be a bibtex file or whev, but I like CSL json), and you're using ``pandoc`` with ``pandoc-citeproc`` as a custom processor, and ``bib-csl.json`` is in the same directory as your markdown file.
USUALLY, Marked2 can't actually work with this, because it doesn't run from the same working directory as the one the file is in, so you'd have to write in an absolute path to your citation file.
That's bad and annoying.
Then adding this python script in as a preprocessor in Marked will fix the paths, so that pandoc can find the bibliography.
Originally contributed by: https://github.com/paultopia on this issue https://github.com/kotfu/marked-bonus-pack/issues/11
"""
#!/usr/bin/env python
import sys, os, re
path = os.path.dirname(os.environ['MARKED_PATH'])
def normalize_path(directory, relative_file):
relpath = directory + "/" + relative_file
return os.path.abspath(relpath)
def normalize_bibliography(match):
m1 = match.group(1)
correct_path = normalize_path(path, m1)
return r'bibliography: {}'.format(correct_path)
outlines = []
for line in sys.stdin:
outlines.append(line)
text = "".join(outlines)
pattern = re.compile(r"bibliography: (.*?.json)")
out = pattern.sub(normalize_bibliography, text, count=1)
print(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment