STDIN conversion is working.

while preparing hybrideditor for pzi server
master
Castro0o 7 years ago
parent 45f151af79
commit 152b4896f2

2
.gitignore vendored

@ -1,2 +0,0 @@
*.pyc
*~

@ -20,6 +20,14 @@ http://localhost:8000/cgi-bin/start.cgi
or or
http://localhost:8000/cgi-bin/select.cgi http://localhost:8000/cgi-bin/select.cgi
## install using cgi-bin
```bash
mkdir /var/www/cgi-bin/hybrideditor/
ln cgi-bin/* /var/www/cgi-bin/hybrideditor/
chmod +x cgi-bin/
```
## License ## License
This software is released under a [GPLv3 license](http://www.gnu.org/copyleft/gpl.html). See LICENSE. This software is released under a [GPLv3 license](http://www.gnu.org/copyleft/gpl.html). See LICENSE.

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

Binary file not shown.

@ -4,6 +4,6 @@ MAKE = "make"
MAKEFILE = None MAKEFILE = None
PROJECT_PATH = "./projects" PROJECT_PATH = "./projects"
PROJECT_URL = "/projects/" # must end with / PROJECT_URL = "/projects/" # must end with /
EDITOR_URL = "/cgi-bin/hype.cgi" EDITOR_URL = "/cgi-bin/pandoc-cgi/hype.cgi"
SAMPLE_PROJECT_PATH = "projects/sample" SAMPLE_PROJECT_PATH = "projects/sample"

@ -16,12 +16,12 @@
<span class="label">write markdown</span> / <span class="label">write markdown</span> /
<button id="openbutton">import .docx</button> <button id="openbutton">import .docx</button>
<div id="openmenu"> <div id="openmenu">
<form id="dropzone" action="/cgi-bin/pandoc.cgi" class="dropzone"> <form id="dropzone" action="/cgi-bin/hybrideditor/pandoc.cgi" class="dropzone">
<div class="fallback"> <div class="fallback">
<input name="file" type="file" /> <input name="file" type="file" />
</div> </div>
</form> </form>
<!-- <form id="sideload" style="margin-top: 5px" action="/cgi-bin/pandoc.cgi"> <!-- <form id="sideload" style="margin-top: 5px" action="/cgi-bin/hybrideditor/pandoc.cgi">
<label for="importurl">Upload from URL:</label><br /> <label for="importurl">Upload from URL:</label><br />
<input id="importurl" name="url" type="text" /> <input id="importurl" name="url" type="text" />
<input type="submit" name="_submit" value="import" /> <input type="submit" name="_submit" value="import" />
@ -37,7 +37,7 @@
<div id="preview" class="right"> <div id="preview" class="right">
<div class="header"> <div class="header">
<div class="header-center"> <div class="header-center">
<form id="previewform" method="post" action="/cgi-bin/pandoc.cgi" target="previewiframe"> <form id="previewform" method="post" action="/cgi-bin/hybrideditor/pandoc.cgi" target="previewiframe">
<input type="hidden" name="text" id="previewformtext" /> <input type="hidden" name="text" id="previewformtext" />
<input type="hidden" name="from" value="markdown" /> <input type="hidden" name="from" value="markdown" />
<select id="formatselect" name="to" onchange="this.form.submit()"> <select id="formatselect" name="to" onchange="this.form.submit()">

@ -53,4 +53,5 @@ editor.setHighlightActiveLine(false);
editor.setShowInvisibles(false); editor.setShowInvisibles(false);
editor.getSession().setMode("ace/mode/markdown"); editor.getSession().setMode("ace/mode/markdown");
editor.getSession().setUseWrapMode(true); editor.getSession().setUseWrapMode(true);
editor.setValue(""); // reset the editor

Loading…
Cancel
Save