import os, sys, logging, juliet
from juliet.pageprocessor import PageProcessor

def _load_from_file(processor, folder, path):
    logging.debug("Loading file " + path)

    element = {}
    with open(os.path.join(folder, path), 'r') as stream:
        element = processor.process(stream.read(), path)

    return element

def get_from_folder(folder, config):
    """ Load files contained in passed folder, pre-process them using fileParser
    and return them as a list sorted in inverse alphabetical order.

    Files in passed folder should have a valid Page format (see FileParser). """

    elements = []
    entries = sorted(os.listdir(folder), reverse=True)

    file_naming_var = ""
    if ("file_naming_variable" in config["site"].keys()):
        file_naming_var = config["site"]["file_naming_variable"]
    else:
        file_naming_var = juliet.defaults.DEFAULT_FILE_NAMING_VARIABLE

    processor = PageProcessor(config["site"]["baseurl"], file_naming_var)
    for source_file in entries:
        elements.append(_load_from_file(processor, folder, source_file))

    return elements

@@ -896,182 +896,846 @@
     
-processor = PageProcessor(config%5B%22site%22%5D%5B%22baseurl%22%5D, file_naming_var)%0A    for source_file in entries:%0A        elements.append(_load_from_file(processor, folder, source_file))
+theme_headers = juliet.defaults.DEFAULT_THEME_HEADERS%0A    theme_headers_file = os.path.join(config%5B%22site%22%5D%5B%22baseurl%22%5D, juliet.paths.THEMES_PATH,%0A        config%5B%22site%22%5D%5B%22theme%22%5D, juliet.paths.THEME_HEADERS_FILE)%0A    if (os.path.isfile(theme_headers_file)):%0A        tmp = juliet.configurator.get_yaml(theme_headers_file)%0A        # theme headers file might only define entries for posts/pages%0A        if (tmp%5Bfolder%5D):%0A            theme_headers = tmp%0A%0A    processor = PageProcessor(config%5B%22site%22%5D%5B%22baseurl%22%5D, file_naming_var)%0A    for source_file in entries:%0A        elements.append(_load_from_file(processor, folder, source_file))%0A%0A        for key, value in theme_headers.items():%0A            if (key not in elements%5B-1%5D.keys() and value):%0A                # Fix missing entries with theme's defaults%0A                elements%5B-1%5D%5Bkey%5D = value
 %0A%0A  

support headers.yml