comparison piecrust/page.py @ 703:dab26ab3d533

internal: Don't run regexes for the 99% case of pages with no segments.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 16 Apr 2016 23:12:06 -0700
parents 683be25cbdb2
children ab5c6a8ae90a 71309814e88f
comparison
equal deleted inserted replaced
702:c62d83e17abf 703:dab26ab3d533
301 301
302 def _count_lines(s): 302 def _count_lines(s):
303 return len(s.split('\n')) 303 return len(s.split('\n'))
304 304
305 305
306 def _string_needs_parsing(txt, offset):
307 txtlen = len(txt)
308 index = txt.find('-', offset)
309 while index >= 0 and index < txtlen - 8:
310 if txt[index + 1] == '-' and txt[index + 2] == '-':
311 return True
312 index = txt.find('-', index + 1)
313 return False
314
315
306 def parse_segments(raw, offset=0): 316 def parse_segments(raw, offset=0):
307 # Get the number of lines in the header. 317 # Get the number of lines in the header.
308 header_lines = _count_lines(raw[:offset].rstrip()) 318 header_lines = _count_lines(raw[:offset].rstrip())
309 current_line = header_lines 319 current_line = header_lines
310 320
311 # Start parsing. 321 # Figure out if we need any parsing.
322 do_parse = _string_needs_parsing(raw, offset)
323 if not do_parse:
324 seg = ContentSegment()
325 seg.parts = [
326 ContentSegmentPart(raw[offset:], None, offset, current_line)]
327 return {'content': seg}
328
329 # Start parsing segments and parts.
312 matches = list(segment_pattern.finditer(raw, offset)) 330 matches = list(segment_pattern.finditer(raw, offset))
313 num_matches = len(matches) 331 num_matches = len(matches)
314 if num_matches > 0: 332 if num_matches > 0:
315 contents = {} 333 contents = {}
316 334