Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix relative includes in Jinja extension #79

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions pypugjs/ext/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def attrs(attrs, terse=False):


class Compiler(_Compiler):
def __init__(self, node, **options):
self.filename = options.pop('_filename')
super().__init__(node, **options)

def visitCodeBlock(self, block):
if self.mixing > 0:
if self.mixing > 1:
Expand Down Expand Up @@ -110,18 +114,34 @@ def visitEach(self, each):
self.buf.append('{% endfor %}')

def visitInclude(self, node):
path = os.path.join(
self.options.get("basedir", '.'), self.format_path(node.path)
)
if os.path.exists(path):
src = open(path, 'r').read()
path = self.format_path(node.path)
if path.startswith('/'):
if not self.options.get('basedir'):
raise Exception("Include path '{}' requires basedir option to resolve.".format(path))

else:
path = os.path.join(self.options["basedir"], path)
else:
raise Exception("Include path doesn't exists ({})".format(path))
if not self.filename:
raise Exception("Include path '{}' requires filename to resolve.".format(path))

else:
path = os.path.join(os.path.dirname(self.filename), path)

parser = pypugjs.parser.Parser(src)
block = parser.parse()
self.visit(block)
if not os.path.exists(path):
raise Exception("Include path '{}' does not exist.".format(path))

with open(path, 'r') as fd:
src = fd.read()

if not path.endswith(self.extension):
self.buf.append(src)

else:
parser = pypugjs.parser.Parser(src)
block = parser.parse()
self.visit(block)

def attributes(self, attrs):
return "%s%s(%s)%s" % (
self.variable_start_string,
Expand Down Expand Up @@ -154,9 +174,14 @@ def preprocess(self, source, name, filename=None):
loader = loader.app.jinja_loader
except AttributeError:
pass

if hasattr(loader, 'searchpath') and len(loader.searchpath):
self.options["basedir"] = loader.searchpath[0]

if not name or (name and not os.path.splitext(name)[1] in self.file_extensions):
if filename:
self.options["_filename"] = filename

if (not name) or (os.path.splitext(name)[1] not in self.file_extensions):
return source
return process(source, filename=name, compiler=Compiler, **self.options)
else:
return process(source, filename=name, compiler=Compiler, **self.options)