|
|
|
@ -1,7 +1,7 @@
|
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
import cgitb; cgitb.enable()
|
|
|
|
|
import cgi, os, sys, subprocess
|
|
|
|
|
import cgi, os, sys, subprocess, shlex
|
|
|
|
|
from tempfile import NamedTemporaryFile as NTF
|
|
|
|
|
from settings import PANDOC
|
|
|
|
|
|
|
|
|
@ -33,6 +33,13 @@ def guess_format_from_filename (fn):
|
|
|
|
|
return "docx"
|
|
|
|
|
return "txt"
|
|
|
|
|
|
|
|
|
|
def print_template(txt):
|
|
|
|
|
print "Content-type: text/html;charset=utf-8"
|
|
|
|
|
print
|
|
|
|
|
print txt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if method == "POST":
|
|
|
|
|
fs = cgi.FieldStorage()
|
|
|
|
|
from_format = fs.getvalue("from")
|
|
|
|
@ -57,8 +64,7 @@ if method == "POST":
|
|
|
|
|
if from_format == None:
|
|
|
|
|
from_format = guess_format_from_filename(f.filename)
|
|
|
|
|
|
|
|
|
|
# tmp = NTF(delete=False)
|
|
|
|
|
tmp = open('pandoc.tmp','wb')
|
|
|
|
|
tmp = NTF(delete=False)
|
|
|
|
|
bytes = 0
|
|
|
|
|
while True:
|
|
|
|
|
data = f.file.read()
|
|
|
|
@ -77,10 +83,7 @@ if method == "POST":
|
|
|
|
|
print
|
|
|
|
|
# p = subprocess.check_output('pandoc --from {0} --to {1} "{2}"'.format(ffrom, to, tmp.name), shell=True, stderr=subprocess.STDOUT)
|
|
|
|
|
# print p
|
|
|
|
|
if to_format is 'icml':
|
|
|
|
|
p = subprocess.Popen([PANDOC, '--from', 'docx', '--to', to_format,'-s', '/home/andre/public_html/oooooooooo.io/cgi-bin/pandoc.tmp'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
else:
|
|
|
|
|
p = subprocess.Popen([PANDOC, '--from', 'docx', '--to', to_format, '/home/andre/public_html/oooooooooo.io/cgi-bin/pandoc.tmp', '--atx-headers'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
p = subprocess.Popen([PANDOC, '--from', from_format, '--to', to_format, tmp.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
|
|
out, err = p.communicate()
|
|
|
|
|
print out
|
|
|
|
@ -90,7 +93,7 @@ if method == "POST":
|
|
|
|
|
print
|
|
|
|
|
print u"<span style='font-family:monospace'>An error occurred, pandoc said: {0}</span>".format(e.output).format("utf-8")
|
|
|
|
|
|
|
|
|
|
# tmp.unlink(tmp.name)
|
|
|
|
|
tmp.unlink(tmp.name)
|
|
|
|
|
|
|
|
|
|
###################################
|
|
|
|
|
# FORM/TEXT INPUT
|
|
|
|
@ -99,20 +102,22 @@ if method == "POST":
|
|
|
|
|
text = fs.getvalue("text")
|
|
|
|
|
if from_format and to_format:
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
|
|
print "Content-type: {0}".format(to_type['mime'])
|
|
|
|
|
if download:
|
|
|
|
|
print "Content-Disposition: attachment;filename=\"hype_conversion.{0}\"".format(to_type['ext'])
|
|
|
|
|
print
|
|
|
|
|
# p = subprocess.check_output('pandoc --from {0} --to {1} "{2}"'.format(ffrom, to, tmp.name), shell=True, stderr=subprocess.STDOUT)
|
|
|
|
|
# print p
|
|
|
|
|
|
|
|
|
|
if to_format == 'icml':
|
|
|
|
|
p = subprocess.Popen([PANDOC, '--from', from_format, '--to', to_format , '-s'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
else:
|
|
|
|
|
p = subprocess.Popen([PANDOC, '--from', from_format, '--to', to_format, '--atx-headers' ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
#p = subprocess.check_output('echo "{2}"|pandoc --from {0} --to {1} '.format(from_format, to_format, '#foo'), shell=True, stderr=subprocess.STDOUT)
|
|
|
|
|
#print p
|
|
|
|
|
cmd = "{} --from {} --to {}".format(PANDOC, from_format, to_format)
|
|
|
|
|
cmd_list = shlex.split( cmd )
|
|
|
|
|
|
|
|
|
|
p = subprocess.Popen(cmd_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out, err = p.communicate(text)
|
|
|
|
|
print out
|
|
|
|
|
print cmd+' '+ out
|
|
|
|
|
|
|
|
|
|
except subprocess.CalledProcessError, e:
|
|
|
|
|
print "Content-type: text/html;charset=utf-8"
|
|
|
|
@ -125,22 +130,22 @@ if method == "POST":
|
|
|
|
|
print u"<span style='font-family:monospace'>Format unspecified</span>".encode("utf-8")
|
|
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
print "Content-type: text/html;charset=utf-8"
|
|
|
|
|
print
|
|
|
|
|
print """<!DOCTYPE html>
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<form enctype="multipart/form-data" action="" method="post">
|
|
|
|
|
<p>Convert: <input type="file" name="file"></p>
|
|
|
|
|
to: <select name="to">
|
|
|
|
|
<option>markdown</option>
|
|
|
|
|
<option>html</option>
|
|
|
|
|
</select>
|
|
|
|
|
<p><input type="submit" value="OK"></p>
|
|
|
|
|
</form>
|
|
|
|
|
</body></html>
|
|
|
|
|
"""
|
|
|
|
|
'''
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
# print """<!DOCTYPE html>
|
|
|
|
|
# <html>
|
|
|
|
|
# <head>
|
|
|
|
|
# <meta charset="utf-8" />
|
|
|
|
|
# </head>
|
|
|
|
|
# <body>
|
|
|
|
|
# <form enctype="multipart/form-data" action="" method="post">
|
|
|
|
|
# <p>Convert: <input type="file" name="file"></p>
|
|
|
|
|
# to: <select name="to">
|
|
|
|
|
# <option>markdown</option>
|
|
|
|
|
# <option>html</option>
|
|
|
|
|
# </select>
|
|
|
|
|
# <p><input type="submit" value="OK"></p>
|
|
|
|
|
# </form>
|
|
|
|
|
# </body></html>
|
|
|
|
|
# """
|
|
|
|
|