| 1 | # (c) 2017 Aaron Taylor <ataylor at subgeniuskitty dot com> |
| 2 | # See LICENSE file for copyright and license details. |
| 3 | |
| 4 | # Python imports |
| 5 | import os, sys, subprocess, configparser |
| 6 | |
| 7 | # CMless imports |
| 8 | import debug, config, template |
| 9 | |
| 10 | # Accepts a string containing a filesystem path to a text file. |
| 11 | # Returns a string containing the contents of that file. |
| 12 | def load_file(path): |
| 13 | try: |
| 14 | with open(path) as f: |
| 15 | contents = f.read() |
| 16 | except: |
| 17 | if debug.print_to_browser: print("Unable to open " + path + " for reading.") |
| 18 | sys.exit("Unable to open " + path + " for reading.") |
| 19 | return contents |
| 20 | |
| 21 | # Accepts a string containing markup'ed text |
| 22 | # Returns a string containing HTML'ed text |
| 23 | def process_markup(text): |
| 24 | try: |
| 25 | p = subprocess.Popen(config.markup_processor, |
| 26 | stdout=subprocess.PIPE,stdin=subprocess.PIPE) |
| 27 | except: |
| 28 | if debug.print_to_browser: print("Unable to open markup processor: " + |
| 29 | 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 | |
| 34 | def strip_page_metadata(content): |
| 35 | keyword = template.add_delimiter("END_PAGE_METADATA") |
| 36 | index = content.find(keyword) |
| 37 | if index != -1: |
| 38 | return content[index+len(keyword):] |
| 39 | else: |
| 40 | return content |
| 41 | |
| 42 | def lookup_page_metadata(key): |
| 43 | page_file = load_file(config.site_data_prefix + os.environ['REQUEST_URI'] + |
| 44 | config.markup_file_extension) |
| 45 | |
| 46 | keyword_begin = template.add_delimiter("BEGIN_PAGE_METADATA") |
| 47 | keyword_end = template.add_delimiter("END_PAGE_METADATA") |
| 48 | index = page_file.find(keyword_end) |
| 49 | if index != -1: |
| 50 | page_file = page_file[len(keyword_begin):index] |
| 51 | else: |
| 52 | page_file = "" |
| 53 | |
| 54 | page_metadata = configparser.ConfigParser() |
| 55 | page_metadata.read_string(page_file) |
| 56 | |
| 57 | if 'DEFAULT' in page_metadata and key in page_metadata['DEFAULT']: |
| 58 | return page_metadata['DEFAULT'][key] |
| 59 | else: |
| 60 | return "" |