Skip to content

Commit

Permalink
Allow a customizable injection point rather than always the end of th…
Browse files Browse the repository at this point in the history
…e </head> tag.
  • Loading branch information
Preston-Landers authored and Preston Landers committed Dec 17, 2015
1 parent df07f7b commit 86e4fb4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion bowerstatic/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
class Bower(object):
"""Contains a bunch of bower_components directories.
"""
def __init__(self, publisher_signature='bowerstatic', autoversion=None):
def __init__(self, publisher_signature='bowerstatic', autoversion=None, inject_point=None):
self.publisher_signature = publisher_signature
self._component_collections = {}
self._renderer = Renderer()
self.autoversion = autoversion or filesystem_second_autoversion
self.inject_point = inject_point or b'</head>'

def components(self, name, path):
if name in self._component_collections:
Expand Down
9 changes: 7 additions & 2 deletions bowerstatic/injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ def __call__(self, request):
if inclusions is None:
return response
body = response.body
inject_pt = self.bower.inject_point
head_tag = b'</head>'
if inject_pt != head_tag and body.find(inject_pt) == -1:
inject_pt = head_tag

response.body = b''
rendered_inclusions = (inclusions.render() + '</head>').encode('utf-8')
body = body.replace(b'</head>', rendered_inclusions)
rendered_inclusions = (inclusions.render() + inject_pt).encode('utf-8')
body = body.replace(inject_pt, rendered_inclusions)
response.write(body)
return response

Expand Down

0 comments on commit 86e4fb4

Please sign in to comment.