| 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 | def load_file(path): |
| 11 | """Open the text file at 'path' and return the contents as a string.""" |
| 12 | try: |
| 13 | with open(path) as f: |
| 14 | contents = f.read() |
| 15 | except: |
| 16 | if debug.print_to_browser: print("Unable to open " + path + " for reading.") |
| 17 | sys.exit("Unable to open " + path + " for reading.") |
| 18 | return contents |
| 19 | |
| 20 | def process_markup(text): |
| 21 | """Execute external markup processor and return processed 'text'. |
| 22 | |
| 23 | Markup processor is specified by config.markup_processor variable |
| 24 | and should accept markup on stdin while returning HTML on stdout. |
| 25 | """ |
| 26 | try: |
| 27 | p = subprocess.Popen(config.markup_processor, |
| 28 | stdout=subprocess.PIPE,stdin=subprocess.PIPE) |
| 29 | except: |
| 30 | if debug.print_to_browser: print("Unable to open markup processor: " + |
| 31 | config.markup_processor) |
| 32 | sys.exit("Unable to open markup processor: " + config.markup_processor) |
| 33 | text = p.communicate(text.encode('UTF-8'))[0] |
| 34 | return(text.decode('UTF-8')) |
| 35 | |
| 36 | def strip_page_metadata(content): |
| 37 | """Strip metadata from the beginning of 'content' and return whatever remains.""" |
| 38 | keyword = template.add_delimiter("END_PAGE_METADATA") |
| 39 | index = content.find(keyword) |
| 40 | if index != -1: |
| 41 | return content[index+len(keyword):] |
| 42 | else: |
| 43 | return content |
| 44 | |
| 45 | def lookup_current_metadata(key): |
| 46 | """Wrapper for lookup_file_metadata() supplying the current REQUEST_URI as 'file'.""" |
| 47 | file = os.path.join(config.site_data_prefix + os.environ['REQUEST_URI'] + |
| 48 | config.markup_file_extension) |
| 49 | return lookup_file_metadata(key,file) |
| 50 | |
| 51 | def lookup_file_metadata(key,file): |
| 52 | """Read 'key' metadata in 'file' and return corresponding value. |
| 53 | |
| 54 | Assumes key is contained in the [DEFAULT] category. |
| 55 | Returns empty string if key is not present. |
| 56 | """ |
| 57 | page_file = load_file(file) |
| 58 | |
| 59 | # Extract just the metadata block so we can pass it to ConfigParser. |
| 60 | keyword_begin = template.add_delimiter("BEGIN_PAGE_METADATA") |
| 61 | keyword_end = template.add_delimiter("END_PAGE_METADATA") |
| 62 | index = page_file.find(keyword_end) |
| 63 | if index != -1: |
| 64 | page_file = page_file[len(keyword_begin):index] |
| 65 | else: |
| 66 | # ConfigParser will accept an empty string so use that as fallback. |
| 67 | page_file = "" |
| 68 | |
| 69 | page_metadata = configparser.ConfigParser() |
| 70 | page_metadata.read_string(page_file) |
| 71 | |
| 72 | if 'DEFAULT' in page_metadata and key in page_metadata['DEFAULT']: |
| 73 | return page_metadata['DEFAULT'][key] |
| 74 | else: |
| 75 | # An empty string is technically a valid value for a key but we will |
| 76 | # overload it as also indicating 'key' wasn't present since a key |
| 77 | # without a value is functionally equivalent to no key at all for our |
| 78 | # purposes. |
| 79 | return "" |