@@ -116,6 +116,8 @@ class DjangoTemplatePlugin(
116116 def __init__ (self , options ):
117117 extensions = options .get ("template_extensions" , "html,htm,txt" )
118118 self .extensions = [e .strip () for e in extensions .split ("," )]
119+
120+ self .exclude_blocks = options .get ("exclude_blocks" )
119121
120122 self .debug_checked = False
121123
@@ -151,7 +153,7 @@ def file_tracer(self, filename):
151153 return None
152154
153155 def file_reporter (self , filename ):
154- return FileReporter (filename )
156+ return FileReporter (filename , self . exclude_blocks )
155157
156158 def find_executable_files (self , src_dir ):
157159 # We're only interested in files that look like reasonable HTML
@@ -259,10 +261,16 @@ def get_line_map(self, filename):
259261
260262
261263class FileReporter (coverage .plugin .FileReporter ):
262- def __init__ (self , filename ):
264+ def __init__ (self , filename , exclude_blocks ):
263265 super ().__init__ (filename )
264266 # TODO: html filenames are absolute.
265267
268+ if exclude_blocks :
269+ self .exclude_blocks_regex = re .compile (join_regex (exclude_blocks ))
270+ else :
271+ self .exclude_blocks_regex = None
272+ self ._excluded = set ()
273+
266274 self ._source = None
267275
268276 def source (self ):
@@ -318,6 +326,12 @@ def lines(self):
318326 # In an inheriting template, ignore all tags outside of
319327 # blocks.
320328 continue
329+
330+ # Ignore any block token content that has been explcitly
331+ # excluded in config
332+ if self .exclude_block_token (token ):
333+ self ._excluded .add (token .lineno )
334+ continue
321335
322336 if token .contents == "comment" :
323337 comment = True
@@ -356,6 +370,12 @@ def lines(self):
356370
357371 return source_lines
358372
373+ def excluded_lines (self ):
374+ return self ._excluded
375+
376+ def exclude_block_token (self , token ):
377+ if self .exclude_blocks_regex :
378+ return self .exclude_blocks_regex .search (token .contents )
359379
360380def running_sum (seq ):
361381 total = 0
0 commit comments