Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39558
1
#!/usr/bin/python
2
3
# rmd2html.py - used by rmd edit mode
4
5
import os, sys, subprocess, errno
6
from subprocess import PIPE
7
8
def rmd2html(path):
9
if not os.path.exists(path):
10
raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), path)
11
12
absp = os.path.abspath(path)
13
(head,tail) = os.path.split(absp)
14
os.chdir(head)
15
16
(root, ext) = os.path.splitext(tail)
17
if ext.lower() != ".rmd":
18
raise ValueError('Rmd input file required, got {}'.format(path))
19
20
# knitr always writes something to stderr
21
# only pass that to outer program if there is an error
22
cmd = '''Rscript -e "library(knitr); knit('{}')" >/dev/null'''.format(tail)
23
p0 = subprocess.Popen(cmd, shell=True, stderr=PIPE)
24
(stdoutdata, stderrdata) = p0.communicate()
25
if p0.returncode == 0:
26
cmd2 = "pandoc -s {}.md -t html".format(root)
27
subprocess.call(cmd2, shell=True)
28
else:
29
sys.stderr.write(stderrdata)
30
sys.stderr.flush()
31
32
def main():
33
if len(sys.argv) != 2:
34
raise ValueError('Usage: {} path/to/file.Rmd'.format(sys.argv[0]))
35
36
rmd2html(sys.argv[1])
37
38
if __name__ == "__main__":
39
main()
40
41
42
43
44