You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.3 KiB
Python

#!/usr/bin/env python
import cgitb; cgitb.enable()
import cgi, os, sys, subprocess, shlex, mammoth
from tempfile import NamedTemporaryFile as NTF
from settings import PANDOC
"""
Simple Upload form that saves files to a temp named file,
calls pandoc for conversion to a given format,
& displays results in browser.
Uses a temp file because formats like docx are considered "archives"
and need to be accessed by pandoc as filenames to actual filenames
(not piped directly in)
"""
method = os.environ.get("REQUEST_METHOD")
_types = {
'markdown': { 'mime': "text/plain;charset=utf-8", 'ext': 'markdown' },
# 'icml': { 'mime': "application/xml;charset=utf-8", 'ext': 'icml' },
'icml': { 'mime': "text/plain;charset=utf-8", 'ext': 'icml' },
'docx': { 'mime': "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8", 'ext': 'docx' },
'html': { 'mime': "text/html;charset=utf-8", 'ext': 'html' }
}
DEFAULT_TYPE = { 'mime': "text/plain;charset=utf-8", 'ext': 'txt' }
def guess_format_from_filename (fn):
if fn.endswith(".docx"):
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")
to_format = fs.getvalue("to", "markdown")
to_type = _types.get(to_format, DEFAULT_TYPE)
submit = fs.getvalue("_submit", "submit")
download = submit == "download"
download_filename = None
if download:
download_filename = "hype_conversion." + to_type['ext']
try:
f = fs["file"]
except KeyError:
f = None
###################################
# FILE INPUT
###################################
if f != None and f.file:
if from_format == None:
from_format = guess_format_from_filename(f.filename)
tmp = NTF(suffix=".docx", delete=False)
bytes = 0
while True:
data = f.file.read()
if not data:
break
bytes += len(data)
tmp.write(data)
# print "saved to '{0}'<br>".format(tmp.name)
# print "read {0} bytes<br>".format(bytes)
tmp.close()
os.chmod(tmp.name, 0666)
try:
print "Content-type: {0}".format(to_type['mime'])
if download:
print "Content-Disposition: attachment;filename=\"hype_conversion.{0}\"".format(to_type['ext'])
print
docx_file = open(tmp.name, "rb")
result = mammoth.convert_to_markdown(docx_file)
out = (result.value).encode('utf-8')
err = result.messages
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
###################################
# FORM/TEXT INPUT
###################################
else:
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('echo "{2}"|pandoc --from {0} --to {1} '.format(from_format, to_format, '#foo'), shell=True, stderr=subprocess.STDOUT)
#print 'to_format', to_format
if to_format == 'icml': # icml needs solo flag: -s
cmd = "{} --from {} --to {} -s".format(PANDOC, from_format, to_format)
else:
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
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).encode("utf-8")
else:
print "Content-type: text/html;charset=utf-8"
print
print u"<span style='font-family:monospace'>Format unspecified</span>".encode("utf-8")
sys.exit(0)
'''
'''
# 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>
# """
'''
p = subprocess.Popen([PANDOC, '--from', from_format, '--to', to_format, tmp.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print out
# print_template(data)
'''