python : R markdown 格式轉換為 html
阿新 • • 發佈:2018-12-12
R語言 rmarkdown包 會呼叫 pandoc-1.19.2.1 ,
library(rmarkdown)
render("test.Rmd", "html_document")
試用了,發現轉換後的 HTML 視覺效果很差。
決定自己用 python 寫一個 R markdown 格式轉換為 html
Rmd2htm.py
import os, sys if len(sys.argv) ==2: f1 = sys.argv[1] else: print 'usage: Rmd2htm.py file1.Rmd ' sys.exit(1) if not os.path.exists(f1): print 'Error: %s not found\n' % f1 sys.exit(1) fn,ext =os.path.splitext(f1) if ext != '.Rmd' and ext !='.rmd': print 'Error: %s is not .Rmd' % ext sys.exit(1) f2 = fn +'.htm' fp = open(f1, 'r') fp2 =open(f2,'w') iscode = False for line in fp: aline = line.strip() if len(aline) ==0: fp2.write("<p>\n") elif aline.startswith("---"): fp2.write("<hr>\n") elif aline.startswith("```{r"): fp2.write("<code><pre>\n");iscode=True elif aline.startswith("```"): fp2.write("</pre></code>\n");iscode=False elif aline.startswith("######"): fp2.write("<h6>"+aline[6:]+"</h6>\n") elif aline.startswith("#####"): fp2.write("<h5>"+aline[5:]+"</h5>\n") elif aline.startswith("####"): fp2.write("<h4>"+aline[4:]+"</h4>\n") elif aline.startswith("###"): fp2.write("<h3>"+aline[3:]+"</h3>\n") elif aline.startswith("##"): fp2.write("<h2>"+aline[2:]+"</h2>\n") elif aline.startswith("#"): fp2.write("<h1>"+aline[1:]+"</h1>\n") elif aline.startswith("**") and not iscode: fp2.write("<strong>"+aline[2:].replace("**","</strong>")+"<br>\n") elif not iscode : fp2.write(aline+"<br>\n") else: fp2.write(line) # fp.close() fp2.close() print f2
執行 cmd
Rmd2htm.py text.Rmd