Skip to content

Instantly share code, notes, and snippets.

@paultopia
Forked from aaronwolen/slides.md
Last active May 3, 2017 20:41
Show Gist options
  • Save paultopia/0c57899407aed81f6932 to your computer and use it in GitHub Desktop.
Save paultopia/0c57899407aed81f6932 to your computer and use it in GitHub Desktop.
Pandoc template to generate reveal.js slideshows. (corrected from original to be compatible with 3.2.0 release of reveal.js); also added a quick python script to generate with less command-line ugliness.
import argparse
# this first bit is to enable multiline help text. apparently this is a known problem with argparse.
# Solution jacked from http://stackoverflow.com/questions/3853722/python-argparse-how-to-insert-newline-in-the-help-text
import textwrap as _textwrap
class MultilineFormatter(argparse.HelpFormatter):
def _fill_text(self, text, width, indent):
text = self._whitespace_matcher.sub(' ', text).strip()
paragraphs = text.split('|n ')
multiline_text = ''
for paragraph in paragraphs:
formatted_paragraph = _textwrap.fill(paragraph, width, initial_indent=indent, subsequent_indent=indent) + '\n\n'
multiline_text = multiline_text + formatted_paragraph
return multiline_text
parser = argparse.ArgumentParser(description="""This is a utility to make a reveal.js slideshow from a markdown file given the template at:
|n
http://gist.github.com/paultopia/0c57899407aed81f6932
|n
Commandline flags and such are given below. In addition, the following arguments are valid:
|n
Themes: black, white, league (default), sky, beige, simple, serif, blood, night, moon, solarized
|n
Transitions: none (default), fade, slide, convex, concave, zoom
""", formatter_class=MultilineFormatter)
parser.add_argument("infile", help="the markdown file containing the slideshow")
parser.add_argument("-t", "--theme", nargs='?', default="league", help="name of theme")
parser.add_argument("-r", "--transition", nargs='?', default="none", help="name of transition")
parser.add_argument("-o", "--outfile", nargs='?', default=" ", help="filename of outfile")
parser.add_argument("-u", "--unsafe", action="store_true", help="UNSAFE mode: overwrite output file. Off by default.")
args = parser.parse_args()
from os.path import isfile
print(' ')
print(' ')
print("CONVERTING MARKDOWN FILE TO REVEAL JS SLIDES USING PANDOC")
# validate input file
if args.infile[-3:] != '.md':
print(' ')
print("You gave me an input filename without a .md extension. Appending to file.")
theinfile = args.infile + '.md'
else:
theinfile = args.infile
if not isfile(theinfile):
raise IOError("input %s file not found" % theinfile)
# validate output file
if args.outfile == ' ':
print(' ')
print("You didn't give me an output filename. Using input filename with extension changed to html.")
theoutfile = theinfile[0:-3] + '.html'
elif args.outfile[-5:] != '.html':
print(' ')
print("You gave me an output filename without a .html extension. Appending to file.")
theoutfile = args.outfile + '.html'
else:
theoutfile = args.outfile
def lazyiter():
number = 0
while True:
yield number
number += 1
mycounter = lazyiter()
# prevent overwriting of output file
if not args.unsafe:
while isfile(theoutfile):
print(' ')
print("You gave me an output file (%s) that already exists. Appending a number to the front to avoid overwriting." % theoutfile)
theoutfile = str(next(mycounter)) + theoutfile
print(' ')
print('New file is: %s' % theoutfile)
# check and validate themes and transitions
validthemes = ["black", "white", "league", "sky", "beige", "simple", "serif", "blood", "night", "moon", "solarized"]
validtransitions = ["none", "fade", "slide", "convex", "concave", "zoom"]
thetheme = args.theme.lower()
thetransition = args.transition.lower()
if thetheme not in validthemes:
raise ValueError("No theme called %s found. Valid themes are: %s." % (thetheme, ', '.join(validthemes)))
if thetransition not in validtransitions:
raise ValueError("No theme called %s found. Valid themes are: %s." % (thetransition, ', '.join(validtransitions)))
commandstring = 'pandoc -t html5 --template=revealjs.html --standalone --section-divs --variable theme="%s" --variable transition="%s" %s -o %s' % (thetheme, thetransition, theinfile, theoutfile)
print(' ')
print('Converting %s to %s using theme %s and transition %s.' % (theinfile, theoutfile, thetheme, thetransition))
print(' ')
print(' ')
from subprocess import call
call(commandstring, shell=True)
import argparse
parser = argparse.ArgumentParser(description="Quick and easy commandline wrapper for converting markdown to word, html, and pdf formats.")
parser.add_argument("infile", help="the markdown file containing the document")
parser.add_argument("type", help="the type of output: html, word, or pdf")
parser.add_argument("-o", "--outfile", nargs='?', default=" ", help="filename of outfile (optional, will use same name as infile with new extension otherwise)")
parser.add_argument("-u", "--unsafe", action="store_true", help="UNSAFE mode: overwrite output file. Off by default, will append numbers to front of duplicates.")
parser.add_argument("-a", "--append", nargs='?', default=" ", help="Optional file to append to header file, for HTML (to add CSS or JS). Ignored otherwise.")
args = parser.parse_args()
validtypes = ['html', 'word', 'pdf']
outputtype = args.type.lower()
if outputtype not in validtypes:
raise ValueError("No output type called %s found. Valid output types are are: %s." % (outputtype, ', '.join(validtypes)))
from os.path import isfile
print(' ')
print(' ')
print("CONVERTING MARKDOWN FILE TO %s USING PANDOC" % outputtype.upper())
# validate input file
if args.infile[-3:] != '.md':
print(' ')
print("You gave me an input filename without a .md extension. Appending to file.")
theinfile = args.infile + '.md'
else:
theinfile = args.infile
if not isfile(theinfile):
raise IOError("input %s file not found" % theinfile)
# validate output file
if args.outfile == ' ':
print(' ')
print("You didn't give me an output filename. Using input filename with extension changed to appropriate type.")
if outputtype != 'word':
theoutfile = theinfile[0:-3] + '.' + outputtype
else:
theoutfile = theinfile[0:-3] + '.docx'
elif (args.outfile[-5:] != '.html') and (outputtype == "html"):
print(' ')
print("You gave me an output filename without a .html extension. Appending to file.")
theoutfile = args.outfile + '.html'
elif (args.outfile[-5:] != '.docx') and (outputtype == "word"):
print(' ')
print("You gave me an output filename without a .docx extension. Appending to file.")
theoutfile = args.outfile + '.docx'
elif (args.outfile[-4:] != '.pdf') and (outputtype == "pdf"):
print(' ')
print("You gave me an output filename without a .pdf extension. Appending to file.")
theoutfile = args.outfile + '.docx'
else:
theoutfile = args.outfile
def lazyiter():
number = 0
while True:
yield number
number += 1
mycounter = lazyiter()
# prevent overwriting of output file
if not args.unsafe:
while isfile(theoutfile):
print(' ')
print("You gave me an output file (%s) that already exists. Appending a number to the front to avoid overwriting." % theoutfile)
theoutfile = str(next(mycounter)) + theoutfile
print(' ')
print('New file is: %s' % theoutfile)
if (outputtype == "html") and (args.append != ' '):
commandstring = 'pandoc -s -H %s %s -o %s' % (args.append, theinfile, theoutfile)
else:
commandstring = 'pandoc -s %s -o %s' % (theinfile, theoutfile)
print(' ')
print('Converting %s to %s in %s format' % (theinfile, theoutfile, outputtype))
print(' ')
print(' ')
from subprocess import call
call(commandstring, shell=True)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>$title$</title>
<meta name="description" content="$title$">
$for(author)$
<meta name="author" content="$author$" />
$endfor$
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.css">
$if(theme)$
<link rel="stylesheet" href="css/theme/$theme$.css" id="theme">
$else$
<link rel="stylesheet" href="css/theme/default.css" id="theme">
$endif$
<!-- For syntax highlighting -->
$if(highlight-style)$
<link rel="stylesheet" href="lib/css/$highlight-style$.css">
$else$
<link rel="stylesheet" href="lib/css/zenburn.css">
$endif$
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' +
( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) +
'.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
$for(header-includes)$
$header-includes$
$endfor$
</head>
<body>
$for(include-before)$
$include-before$
$endfor$
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>$title$</h1>
$for(author)$<h3>$author$</h3>$endfor$
<p>
<h4>$date$</h4>
</p>
</section>
$if(toc)$
<section>
<h2>Outline</h2>
<nav id="$idprefix$TOC">
$toc$
</nav>
</section>
$endif$
$body$
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: false,
// available themes are in /css/theme
$if(theme)$
theme: Reveal.getQueryHash().theme || '$theme$',
$else$
theme: Reveal.getQueryHash().theme || 'default',
$endif$
// default/cube/page/concave/zoom/linear/fade/none
$if(transition)$
transition: Reveal.getQueryHash().transition || '$transition$',
$else$
transition: Reveal.getQueryHash().transition || 'default',
$endif$
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>

% Title % Name % Date

My first slide

List

  • this
  • is
  • cool

Variables

The following variables can be defined from the command line:

  • theme
  • transition
pandoc -t html5 --template=revealjs.html \
	--standalone --section-divs \
  --variable theme="beige" \
  --variable transition="linear" \
  slides.md -o slides.html
python makeslide.py slides -r slide -t beige
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment