# (c) 2017 Aaron Taylor <ataylor at subgeniuskitty dot com>
# See LICENSE file for copyright and license details.
import os
, sys
, subprocess
, configparser
import debug
, config
, template
"""Open the text file at 'path' and return the contents as a string."""
if debug
.print_to_browser
: print("Unable to open " + path
+ " for reading.")
sys
.exit("Unable to open " + path
+ " for reading.")
def process_markup(text
):
"""Execute external markup processor and return processed 'text'.
Markup processor is specified by config.markup_processor variable
and should accept markup on stdin while returning HTML on stdout.
p
= subprocess
.Popen(config
.markup_processor
,
stdout
=subprocess
.PIPE
,stdin
=subprocess
.PIPE
)
if debug
.print_to_browser
: print("Unable to open 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'))
def strip_page_metadata(content
):
"""Strip metadata from the beginning of 'content' and return whatever remains."""
keyword
= template
.add_delimiter("END_PAGE_METADATA")
index
= content
.find(keyword
)
return content
[index
+len(keyword
):]
def lookup_current_metadata(key
):
"""Wrapper for lookup_file_metadata() supplying the current REQUEST_URI as 'file'."""
file = config
.site_data_prefix
+ os
.environ
['REQUEST_URI'] + config
.markup_file_extension
return lookup_file_metadata(key
,file)
def lookup_file_metadata(key
,file):
"""Read 'key' metadata in 'file' and return corresponding value.
Assumes key is contained in the [DEFAULT] category.
Returns empty string if key is not present.
page_file
= load_file(file)
# Extract just the metadata block so we can pass it to ConfigParser.
keyword_begin
= template
.add_delimiter("BEGIN_PAGE_METADATA")
keyword_end
= template
.add_delimiter("END_PAGE_METADATA")
index
= page_file
.find(keyword_end
)
page_file
= page_file
[len(keyword_begin
):index
]
# ConfigParser will accept an empty string so use that as fallback.
page_metadata
= configparser
.ConfigParser()
page_metadata
.read_string(page_file
)
if 'DEFAULT' in page_metadata
and key
in page_metadata
['DEFAULT']:
return page_metadata
['DEFAULT'][key
]
# An empty string is technically a valid value for a key but we will
# overload it as also indicating 'key' wasn't present since a key
# without a value is functionally equivalent to no key at all for our