-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhighlightText.py
More file actions
66 lines (55 loc) · 1.89 KB
/
Copy pathhighlightText.py
File metadata and controls
66 lines (55 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python3
# (c) Matt Postiff, 2020
# This script finds text prints occurrences of it highlighted.
#
# Make sure you have python3 installed, and
# To run this script on Ubuntu or WSL (Bash on Ubuntu on Windows = Windows Subsystem on Linux), do this:
# sudo apt install python3-pip
# pip3 install 'click'
# pip3 install regex
# Usage:
# python3 highlightText.py "nang" test2.usfm
# Test cases: test2.usfm test7.usfm (n, and ,n examples)
# In AndroidApps/usfm/neao, say:
# python3 highlightText.py "n," *.SFM
# and it should print all lines that have the phrase "n," in them.
# View output with Firefox or other web browser
import copy
import click
import regex
from operator import itemgetter
import functools
import sys
DEBUG = 1
def debug(msg:str, lineEnd=''):
if (DEBUG):
print(msg) # end=lineEnd)
def error(msg:str):
#print(f"ERROR: {msg}")
sys.exit(f"ERROR: {msg}")
def findText(searchstring:str, filename:str):
"""Scans the entire USFM finds relevant strings"""
file = open(filename,'r')
debug(f"Processing file {filename}")
for line in file:
line = line.rstrip()
# Corresponds to grep $'\u2011\,'
#pattern = r'\u2011\,'
pattern = searchstring
#debug(f"Pattern = {pattern}")
patternCompiled = regex.compile(pattern)
patternMatch = patternCompiled.search(line)
if (patternMatch != None):
# Replace all occurrences (count=0) of pattern in line with <span...>
#line = regex.sub(pattern, "JUNK", line, count=0)
line = patternCompiled.sub('<span style="color:red">'+pattern+'</span>', line, count=0)
print(f"<p>{line}</p>");
file.close()
@click.command()
@click.argument('searchstring')
@click.argument('files', nargs=-1)
def main(searchstring:str, files):
for filename in files:
findText(searchstring, filename)
if __name__ == '__main__':
main()