-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrmds.py
86 lines (73 loc) · 2.28 KB
/
rmds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python3
# Copyright @ Balz Guenat 2018
import http.server as srv
import json
import subprocess
import os
VERSION = '0.2'
PORT = 8001
WRITE_DEBUG_FILES = False
cp = subprocess.run(['pandoc', '--version'], stdout=subprocess.PIPE)
pandocVersion = cp.stdout.decode().split('\n')[0].split(' ')[1]
def readLine(bytestream):
bs = bytestream.read(2)
while not bs.endswith(b'\r\n'):
bs = bs + bytestream.read(1)
return bs
def readChunks(bytestream):
while True:
length = int(readLine(bytestream), 16)
if length == 0:
return
yield bytestream.read(length)
bytestream.read(2) # consume \r\n after chunk
def readChunked(bytestream):
s = b''
for chunk in readChunks(bytestream):
s = s + chunk
return s
class MarkdownServer(srv.BaseHTTPRequestHandler):
def do_POST(self):
if 'Transfer-Encoding' in self.headers \
and self.headers['Transfer-Encoding'] \
and encodingHeader.contains('chunked'):
print('content is chunked')
postData = readChunked(self.rfile)
else:
contentLength = int(self.headers['Content-length'])
postData = self.rfile.read(contentLength)
if self.headers['Content-type'] and 'text/markdown' in self.headers['Content-type']:
text = postData
title = 'Untitled'
else:
jsonData = json.loads(postData)
text = jsonData['text'].encode()
title = jsonData['filename'] if 'filename' in jsonData else 'Untitled'
if WRITE_DEBUG_FILES:
with open('rmdsin.md', 'bw') as f:
f.write(text)
cp = subprocess.run([
'pandoc',
'-c', os.path.realpath('github.css'), '--self-contained',
'--metadata', 'pagetitle=\"{}\"'.format(title)
], input=text, stdout=subprocess.PIPE)
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.send_header('Content-length', len(cp.stdout))
self.send_header('Pandoc-version', pandocVersion)
self.end_headers()
if WRITE_DEBUG_FILES:
with open('rmdsout.html', 'bw') as f:
f.write(cp.stdout)
self.wfile.write(cp.stdout)
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b'Try POST.')
def run():
httpd = srv.HTTPServer(('localhost', PORT), MarkdownServer)
httpd.serve_forever()
print('Starting Markdown Server...')
print('pandoc version = {}'.format(pandocVersion))
run()