changeset 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 c62d83e17abf
children 89ca8cdab020 5f552aedd918 71309814e88f
files piecrust/page.py
diffstat 1 files changed, 19 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/page.py	Sat Apr 16 22:50:07 2016 -0700
+++ b/piecrust/page.py	Sat Apr 16 23:12:06 2016 -0700
@@ -303,12 +303,30 @@
     return len(s.split('\n'))
 
 
+def _string_needs_parsing(txt, offset):
+    txtlen = len(txt)
+    index = txt.find('-', offset)
+    while index >= 0 and index < txtlen - 8:
+        if txt[index + 1] == '-' and txt[index + 2] == '-':
+            return True
+        index = txt.find('-', index + 1)
+    return False
+
+
 def parse_segments(raw, offset=0):
     # Get the number of lines in the header.
     header_lines = _count_lines(raw[:offset].rstrip())
     current_line = header_lines
 
-    # Start parsing.
+    # Figure out if we need any parsing.
+    do_parse = _string_needs_parsing(raw, offset)
+    if not do_parse:
+        seg = ContentSegment()
+        seg.parts = [
+                ContentSegmentPart(raw[offset:], None, offset, current_line)]
+        return {'content': seg}
+
+    # Start parsing segments and parts.
     matches = list(segment_pattern.finditer(raw, offset))
     num_matches = len(matches)
     if num_matches > 0: