|
|
|
@ -1,7 +1,7 @@
|
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
import cgitb; cgitb.enable()
|
|
|
|
|
import cgi, os, sys, subprocess, shlex
|
|
|
|
|
import cgi, os, sys, subprocess, shlex, mammoth
|
|
|
|
|
from tempfile import NamedTemporaryFile as NTF
|
|
|
|
|
from settings import PANDOC
|
|
|
|
|
|
|
|
|
@ -83,36 +83,21 @@ if method == "POST":
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
# read content of tmp docx
|
|
|
|
|
cmd_head = "head {}".format(tmp.name)
|
|
|
|
|
cmd_head_list = shlex.split( cmd_head )
|
|
|
|
|
p_head = subprocess.Popen(cmd_head_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
|
|
# convert docx to html: error. No idea why
|
|
|
|
|
cmd_unoconv = "unoconv --stdout -f html {}".format(tmp.name)
|
|
|
|
|
cmd_unoconv_list = shlex.split( cmd_unoconv )
|
|
|
|
|
p_unoconv = subprocess.Popen(cmd_unoconv_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
docx_file = open(tmp.name, "rb")
|
|
|
|
|
result = mammoth.convert_to_markdown(docx_file)
|
|
|
|
|
out = (result.value).encode('utf-8')
|
|
|
|
|
err = result.messages
|
|
|
|
|
|
|
|
|
|
# convert docx to html: depends on most recente versions of pandoc
|
|
|
|
|
# cmd = "{} --from {} --to {} {}".format(PANDOC, from_format, to_format, tmp.name)
|
|
|
|
|
# cmd_list = shlex.split( cmd )
|
|
|
|
|
# p = subprocess.Popen(cmd_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
|
|
# print p
|
|
|
|
|
# p = subprocess.Popen([PANDOC, '--from', from_format, '--to', to_format, tmp.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
|
|
out, err = p_head.communicate() # p_unoconv.communicate()
|
|
|
|
|
#print tmp.name, from_format
|
|
|
|
|
print out, err
|
|
|
|
|
print out#, err
|
|
|
|
|
|
|
|
|
|
except subprocess.CalledProcessError, e:
|
|
|
|
|
print "Content-type: text/html;charset=utf-8"
|
|
|
|
|
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) # remove file
|
|
|
|
|
tmp.unlink(tmp.name) # remove file
|
|
|
|
|
|
|
|
|
|
###################################
|
|
|
|
|
# FORM/TEXT INPUT
|
|
|
|
@ -136,7 +121,7 @@ if method == "POST":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out, err = p.communicate(text)
|
|
|
|
|
print cmd+' '+ out
|
|
|
|
|
print out
|
|
|
|
|
|
|
|
|
|
except subprocess.CalledProcessError, e:
|
|
|
|
|
print "Content-type: text/html;charset=utf-8"
|
|
|
|
|