sourcing/sourcing/text.py

48 lines
1.3 KiB
Python

import re
import random
re_newline = re.compile('\r?\n')
def find_newlines(text):
return (m.end(0) for m in re_newline.finditer(text))
def iter_lines(text):
start = 0
for m in re_newline.finditer(text):
end = m.end(0)
yield (start, text[start:end])
start = m.end(0)
if start < len(text) - 1:
yield (start, text[start:])
def censor_text(text):
def random_chr():
return chr(random.randint(9728, 9983))
return ''.join(random_chr() if c.isalnum() else c for c in text)
def add_highlight(text, span_start, span_length):
span_end = span_start + span_length
for start, line in iter_lines(text):
end = start + len(line)
if not ((end > span_start) and (start < span_end)):
yield start, [{'text': line}]
continue
within_line = span_start - start
cur = []
if within_line > 0:
before = line[:within_line]
cur.append({'text': before})
cur.append({'highlight': line[max(within_line, 0):within_line + span_length]})
after = line[within_line + span_length:]
if after:
cur.append({'text': after})
yield start, cur
def first_non_empty_line(text):
for start, cur in iter_lines(text):
tidy = cur.strip()
if tidy:
return tidy