def post(self, rpath): (rdir, fname) = os.path.split(rpath) fullpath = os.path.join(args.path, rpath) # generate latex fid = open(fullpath, 'r') text = fid.read() (latex, images) = parser.convert_latex(text) # create unique directory comp_dir = os.path.join(tmp_dir, rand_hex()) os.mkdir(comp_dir) # copy over images for img in images: ret = re.search(r'(^|:)//(.*)', img) if ret: (rloc, ) = ret.groups() (_, rname) = os.path.split(rloc) urllib.urlretrieve(url, os.path.join(comp_dir, rname)) else: if img[0] == '/': ipath = img[1:] else: ipath = os.path.join(rdir, img) shutil.copy(os.path.join(args.path, ipath), comp_dir) # find new name ret = re.match(r'(.*)\.md', fname) if ret: fname_new = ret.group(1) else: fname_new = fname # write latex file fname_tex = '%s.tex' % fname_new ftex = open(os.path.join(comp_dir, fname_tex), 'w+') ftex.write(latex) ftex.close() # compile latex file cwd = os.getcwd() os.chdir(comp_dir) call(['pdflatex', '-interaction=nonstopmode', fname_tex]) call(['pdflatex', '-interaction=nonstopmode', fname_tex]) # to resolve references os.chdir(cwd) # read latex file fname_pdf = '%s.pdf' % fname_new fpdf = open(os.path.join(comp_dir, fname_pdf), 'rb') data = fpdf.read() # remove compilation directory shutil.rmtree(comp_dir) # post output self.set_header('Content-Type', 'application/pdf') self.set_header('Content-Disposition', 'attachment; filename=%s' % fname_pdf) self.write(data)
def post(self, rpath): (curdir, fname) = os.path.split(rpath) fullpath = os.path.join(args.path, rpath) # generate latex fid = open(fullpath, 'r') text = fid.read() (latex, images) = parser.convert_latex(text) # find new name ret = re.match(r'(.*)\.md', fname) if ret: fname_new = ret.group(1) else: fname_new = fname # post output self.set_header('Content-Type', 'text/latex') self.set_header('Content-Disposition', 'attachment; filename=%s.tex' % fname_new) self.write(latex)