Added cmless.py which can serve a basic page and pass it through markup
[cmless] / bin / cmless.py
CommitLineData
acd291ea
AT
1#!/usr/bin/python3
2
3# (c) 2017 Aaron Taylor <ataylor at subgeniuskitty dot com>
4# See LICENSE file for copyright and license details.
5
6# Python imports
7import os, sys, subprocess, cgi
8
9# CMless imports
10import debug, config
11
12# Accepts a string containing a filesystem path to a text file.
13# Returns a string containing the contents of that file.
14def load_file(path):
15 try:
16 with open(path) as f:
17 contents = f.read()
18 except:
19 if debug.print_to_browser: print("Unable to open " + path + " for reading.")
20 sys.exit("Unable to open " + path + " for reading.")
21 return contents
22
23# Accepts a string containing markup'ed text
24# Returns a string containing HTML'ed text
25def process_markup(text):
26 try:
27 p = subprocess.Popen(config.markup_processor,stdout=subprocess.PIPE,stdin=subprocess.PIPE)
28 except:
29 if debug.print_to_browser: print("Unable to open markup processor: " + config.markup_processor)
30 sys.exit("Unable to open markup processor: " + config.markup_processor)
31 text = p.communicate(text.encode('UTF-8'))[0]
32 return(text.decode('UTF-8'))
33
34def main():
35 print("Content-Type: text/html;charset=utf-8\n")
36
37 text = load_file(config.site_path_prefix + os.environ['REQUEST_URI'] + config.markup_file_extension)
38 print(process_markup(text))
39
40if __name__ == "__main__":
41 sys.exit(main())