Skip to content

Commit 86e4fb4

Browse files
Preston-LandersPreston Landers
authored andcommitted
Allow a customizable injection point rather than always the end of the </head> tag.
1 parent df07f7b commit 86e4fb4

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

bowerstatic/core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
class Bower(object):
1313
"""Contains a bunch of bower_components directories.
1414
"""
15-
def __init__(self, publisher_signature='bowerstatic', autoversion=None):
15+
def __init__(self, publisher_signature='bowerstatic', autoversion=None, inject_point=None):
1616
self.publisher_signature = publisher_signature
1717
self._component_collections = {}
1818
self._renderer = Renderer()
1919
self.autoversion = autoversion or filesystem_second_autoversion
20+
self.inject_point = inject_point or b'</head>'
2021

2122
def components(self, name, path):
2223
if name in self._component_collections:

bowerstatic/injector.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ def __call__(self, request):
2222
if inclusions is None:
2323
return response
2424
body = response.body
25+
inject_pt = self.bower.inject_point
26+
head_tag = b'</head>'
27+
if inject_pt != head_tag and body.find(inject_pt) == -1:
28+
inject_pt = head_tag
29+
2530
response.body = b''
26-
rendered_inclusions = (inclusions.render() + '</head>').encode('utf-8')
27-
body = body.replace(b'</head>', rendered_inclusions)
31+
rendered_inclusions = (inclusions.render() + inject_pt).encode('utf-8')
32+
body = body.replace(inject_pt, rendered_inclusions)
2833
response.write(body)
2934
return response
3035

0 commit comments

Comments
 (0)