def set_storage(storage_instance: SubtemplateStorage):
    global __storage
    if not storage_instance or not isinstance(storage_instance,
                                              SubtemplateStorage):
        raise ArgumentExpectedException('storage_instance')

    __storage = storage_instance
Esempio n. 2
0
def get_folder(path_parts: List[str]) -> str:
    if not path_parts:
        raise ArgumentExpectedException('path_parts')

    path_parts = [p.strip().strip('/').strip('\\').lower() for p in path_parts]
    parent_folder = os.path.abspath(template_folder)
    folder = os.path.join(parent_folder, *path_parts)
    return folder
Esempio n. 3
0
def get_shared_markdown(import_name: str) -> Optional[str]:
    if not import_name or not import_name.strip():
        raise ArgumentExpectedException('import_name')

    folder = get_folder(['_shared'])
    file = os.path.join(folder, import_name.strip().lower() + '.md')

    if not os.path.exists(file):
        raise TemplateNotFoundException(file)

    with open(file, 'r', encoding='utf-8') as fin:
        return fin.read()
    def get_folder(path_parts: List[str]) -> str:
        if not path_parts:
            raise ArgumentExpectedException('path_parts')

        if not FileStore.__template_folder:
            raise InvalidOperationException(
                "You must set the template folder before calling this method.")

        parts = [p.strip().strip('/').strip('\\').lower() for p in path_parts]
        parent_folder = os.path.abspath(FileStore.__template_folder)
        folder = os.path.join(parent_folder, *parts)

        return folder
Esempio n. 5
0
def get_page(template_path: str, data: Dict[str, Any]) -> str:
    if not template_path or not template_path.strip():
        raise ArgumentExpectedException('template_path')

    template_path = template_path.strip().lower()

    cache = __caching.get_cache()
    log = __logging.get_log()

    key = f'html: {template_path}'
    entry = cache.get_html(key)
    if entry:
        log.trace(f"CACHE HIT: Reusing {template_path} from HTML cache.")
        contents = entry.contents

        # Is there data that needs to be folded in? Process it.
        if data:
            contents = process_variables(contents, data)

        # Return the cached data, no need to transform for variables.
        return contents

    t0 = datetime.datetime.now()

    # Get the markdown with imports and substitutions
    markdown = get_markdown(template_path)
    inline_variables = {}
    markdown = get_inline_variables(markdown, inline_variables, log)
    # Convert markdown to HTML
    html = get_html(markdown)

    # Cache inline variables, but not the passed in data as that varies per request (query string, etc).
    html = process_variables(html, inline_variables)
    cache.add_html(key, key, html)

    # Replace the passed variables each time.
    html = process_variables(html, data)

    dt = datetime.datetime.now() - t0

    msg = f"Created contents for {template_path}:{data} in {int(dt.total_seconds() * 1000):,} ms."
    log.info(f"GENERATING HTML: {msg}")

    return html
Esempio n. 6
0
def get_shared_markdown(import_name: str) -> Optional[str]:
    if not import_name or not import_name.strip():
        raise ArgumentExpectedException('import_name')

    store: SubtemplateStorage = __storage.get_storage()
    return store.get_shared_markdown(import_name)