Added basic template functionality.
authorAaron Taylor <ataylor@subgeniuskitty.com>
Sun, 7 Jan 2018 02:26:46 +0000 (18:26 -0800)
committerAaron Taylor <ataylor@subgeniuskitty.com>
Sun, 7 Jan 2018 02:26:46 +0000 (18:26 -0800)
Refactored code to separate template micropasses into their own file.
Refactored code to keep main() clean and separate for ease of extension
  by new users.
Added Apache RewriteCond to allow passthrough of files like stylesheet,
  images, etc.

README.md
bin/cmless.py
bin/config.py
bin/misc.py [new file with mode: 0644]
bin/template.py [new file with mode: 0644]

index de0b9c0..de8a653 100644 (file)
--- a/README.md
+++ b/README.md
@@ -15,7 +15,7 @@ CMless is not yet functional. The stages required to reach basic functionality a
 1. ~~Figure out deployment scheme~~
 2. ~~Interpret URL and locate/serve appropriate page~~
 3. ~~Feed pages through a document processor~~
 1. ~~Figure out deployment scheme~~
 2. ~~Interpret URL and locate/serve appropriate page~~
 3. ~~Feed pages through a document processor~~
-4. Feed pages through a template engine
+4. ~~Feed pages through a template engine~~
 5. Generate menu based on directory structure
 
 Installation Example - Debian Linux
 5. Generate menu based on directory structure
 
 Installation Example - Debian Linux
@@ -55,6 +55,7 @@ vi /etc/apache2/sites-available/cmless.subgeniuskitty.com
         </Directory>
         RewriteEngine On
         RewriteRule (.*) /mnt/data/apache_vhosts/cmless.subgeniuskitty.com/site/$1
         </Directory>
         RewriteEngine On
         RewriteRule (.*) /mnt/data/apache_vhosts/cmless.subgeniuskitty.com/site/$1
+        RewriteCond %{REQUEST_FILENAME} !-f
         RewriteRule .* /mnt/data/apache_vhosts/cmless.subgeniuskitty.com/bin/cmless.py
 </VirtualHost>
 ```
         RewriteRule .* /mnt/data/apache_vhosts/cmless.subgeniuskitty.com/bin/cmless.py
 </VirtualHost>
 ```
index ece2313..ddba963 100755 (executable)
@@ -4,38 +4,18 @@
 # See LICENSE file for copyright and license details.
 
 # Python imports
 # See LICENSE file for copyright and license details.
 
 # Python imports
-import os, sys, subprocess, cgi
+import sys
 
 # CMless imports
 
 # CMless imports
-import debug, config
-
-# Accepts a string containing a filesystem path to a text file.
-# Returns a string containing the contents of that file.
-def load_file(path):
-    try:
-        with open(path) as f:
-            contents = f.read()
-    except:
-        if debug.print_to_browser: print("Unable to open " + path + " for reading.")
-        sys.exit("Unable to open " + path + " for reading.")
-    return contents
-
-# Accepts a string containing markup'ed text
-# Returns a string containing HTML'ed text
-def process_markup(text):
-    try:
-        p = subprocess.Popen(config.markup_processor,stdout=subprocess.PIPE,stdin=subprocess.PIPE)
-    except:
-        if debug.print_to_browser: print("Unable to open markup processor: " + config.markup_processor)
-        sys.exit("Unable to open markup processor: " + config.markup_processor)
-    text = p.communicate(text.encode('UTF-8'))[0]
-    return(text.decode('UTF-8'))
+import config, misc, template
 
 def main():
     print("Content-Type: text/html;charset=utf-8\n")
 
 
 def main():
     print("Content-Type: text/html;charset=utf-8\n")
 
-    text = load_file(config.site_path_prefix + os.environ['REQUEST_URI'] + config.markup_file_extension)
-    print(process_markup(text))
+    content = misc.load_file(config.site_template_prefix + "/" + config.template_file)
+    content = template.tpl_title(content)
+    content = template.tpl_body(content)
+    print(content)
 
 if __name__ == "__main__":
     sys.exit(main())
 
 if __name__ == "__main__":
     sys.exit(main())
index 3c196a1..6d9e90b 100644 (file)
@@ -7,7 +7,17 @@
 
 # The prefix to the website content relative to the bin/ folder which contains
 # the CMless code.
 
 # The prefix to the website content relative to the bin/ folder which contains
 # the CMless code.
-site_path_prefix = "../site"
+site_data_prefix = "../site/data"
+
+# The prefix to the website template relative to the bin/ folder which contains
+# the CMless code.
+site_template_prefix = "../site/template"
+
+# Document that contains the main page template for the website.
+template_file = "template.tpl"
+
+# Specifies a string to mark the start and end of template specific commands.
+template_delimiter = "%%"
 
 # Full path to the markup processor which can interpret the data files in
 # site_path_prefix and output HTML. It should accept markup data on stdin and
 
 # Full path to the markup processor which can interpret the data files in
 # site_path_prefix and output HTML. It should accept markup data on stdin and
diff --git a/bin/misc.py b/bin/misc.py
new file mode 100644 (file)
index 0000000..821ef34
--- /dev/null
@@ -0,0 +1,32 @@
+# (c) 2017 Aaron Taylor <ataylor at subgeniuskitty dot com>
+# See LICENSE file for copyright and license details.
+
+# Python imports
+import sys, subprocess
+
+# CMless imports
+import debug, config
+
+# Accepts a string containing a filesystem path to a text file.
+# Returns a string containing the contents of that file.
+def load_file(path):
+    try:
+        with open(path) as f:
+            contents = f.read()
+    except:
+        if debug.print_to_browser: print("Unable to open " + path + " for reading.")
+        sys.exit("Unable to open " + path + " for reading.")
+    return contents
+
+# Accepts a string containing markup'ed text
+# Returns a string containing HTML'ed text
+def process_markup(text):
+    try:
+        p = subprocess.Popen(config.markup_processor,
+                stdout=subprocess.PIPE,stdin=subprocess.PIPE)
+    except:
+        if debug.print_to_browser: print("Unable to open markup processor: " +
+                config.markup_processor)
+        sys.exit("Unable to open markup processor: " + config.markup_processor)
+    text = p.communicate(text.encode('UTF-8'))[0]
+    return(text.decode('UTF-8'))
diff --git a/bin/template.py b/bin/template.py
new file mode 100644 (file)
index 0000000..c5b1d19
--- /dev/null
@@ -0,0 +1,21 @@
+# (c) 2017 Aaron Taylor <ataylor at subgeniuskitty dot com>
+# See LICENSE file for copyright and license details.
+
+# Python imports
+import os
+
+# CMless imports
+import config, misc
+
+def tpl_title(template):
+    keyword = "page_title"
+    return template.replace(config.template_delimiter + keyword +
+            config.template_delimiter, os.environ['REQUEST_URI'])
+
+def tpl_body(template):
+    keyword = "page_content"
+    body = misc.load_file(config.site_data_prefix + os.environ['REQUEST_URI'] +
+            config.markup_file_extension)
+    body = misc.process_markup(body)
+    return template.replace(config.template_delimiter + keyword +
+            config.template_delimiter, body)