-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ccpgames/feature/file_embed
Version 0.2.0 - Embedding Extension & Base64 Filter update
- Loading branch information
Showing
38 changed files
with
662 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__version__ = '0.1.0' | ||
__version__ = '0.2.0' | ||
|
||
__author__ = 'Thordur Matthiasson <[email protected]>' | ||
__license__ = 'MIT License' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from ._embed import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
__all__ = [ | ||
'EmbedExtension', | ||
] | ||
|
||
import os | ||
from jinja2 import nodes, TemplateSyntaxError | ||
from jinja2.ext import Extension | ||
from ccptools.structs import * | ||
|
||
class EmbedExtension(Extension): | ||
tags = {'embed'} | ||
|
||
def __init__(self, environment): | ||
super().__init__(environment) | ||
environment.extend(stencil_renderer=None) | ||
|
||
def parse(self, parser): | ||
lineno = next(parser.stream).lineno | ||
args = [parser.parse_expression()] | ||
source_file = parser.filename | ||
kwargs = [nodes.Keyword('source_file', nodes.Const(source_file))] | ||
while parser.stream.skip_if('comma'): | ||
key = parser.stream.expect('name').value | ||
parser.stream.expect('assign') | ||
value = parser.parse_expression() | ||
kwargs.append(nodes.Keyword(key, value)) | ||
|
||
return nodes.CallBlock(self.call_method('_render_embed', args, kwargs), [], [], []).set_lineno(lineno) | ||
|
||
def _render_embed(self, file_path, source_file: Optional[str] = None, indent: int = 0, | ||
alviss: bool = False, env: bool = False, caller=None, **kwargs): | ||
# file_path = os.path.abspath(file_path) | ||
|
||
c = caller() | ||
|
||
caller_source = c.strip() | ||
|
||
# Check if file_path is a variable in the context | ||
if hasattr(file_path, '__call__'): | ||
file_path = file_path() | ||
|
||
content = self.environment.stencil_renderer.get_embed(file_path, alviss=alviss, env=env) | ||
|
||
# Detect the current line's indentation level | ||
if indent: | ||
indent_str = ' '*indent | ||
else: | ||
indent_str = '' | ||
|
||
joiner = f'{indent_str}' | ||
|
||
return f'{indent_str}{joiner.join(content.splitlines(True))}\n' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from ._base64 import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
__all__ = [ | ||
'base64', | ||
] | ||
|
||
import base64 as _base64 | ||
|
||
|
||
def base64(value: str) -> str: | ||
return _base64.encodebytes(value.encode('utf-8')).decode('utf-8').strip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from ._render_stencil import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
__all__ = [ | ||
'render_stencil', | ||
] | ||
|
||
|
||
from ccpstencil.stencils import * | ||
from ccpstencil.utils import * | ||
from ccpstencil.structs import * | ||
|
||
|
||
def render_stencil(template: T_PATH, context: Union[Dict, T_PATH]) -> str: | ||
renderer = StringRenderer() | ||
template = guess_template_by_argument(template, renderer) | ||
context = guess_context_by_argument(context) | ||
renderer.template = template | ||
renderer.context = context | ||
return renderer.render() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from .context import * | ||
from .template import * | ||
from .renderer import * | ||
from .shortcuts import * | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from ._base import * | ||
from ._aliases import * | ||
from .interfaces import * | ||
from ._errors import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
__all__ = [ | ||
'T_PATH', | ||
] | ||
from typing import Union | ||
from pathlib import Path | ||
|
||
T_PATH = Union[str, Path] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from ._guessers import * |
Oops, something went wrong.