forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html-grep
executable file
·82 lines (62 loc) · 2.55 KB
/
html-grep
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
from lib.html_grep import grep
import argparse
import sys
from typing import List
# check for the venv
from lib import sanity_check
sanity_check.check_venv(__file__)
from zulint import lister
USAGE = '''
This file greps HTML files for keywords in a context-sensitive manner.
Example:
$ ./tools/html-grep '#group-pms' '.filters'
templates/zerver/right_sidebar.html 45
div.right-sidebar#right-sidebar
div#group-pm-list
ul.filters.scrolling_list#group-pms
Keyword syntax:
When supplying keywords, they must correspond to HTML elements,
classes, or ids. Typical keywords are:
li // search for "<li>"
'.modal' // search for '<foo class="modal">'
'#intro' // search for '<bar id="intro">'
Searches are context-sensitive in the sense that the keyword
may be found among parent tags of a leaf tag, not just the leaf
tag itself. (Think of this as being similar to the way a
browser applies nested CSS styles; it looks at the whole HTML
tree.)
Finding files to search:
The tool currently searches your git repo for files with
the extension .html or .handlebars., and then it does
the grep against all those files.
TODO: allow specific files to be searched.'''
def check_our_files():
# type: () -> None
parser = argparse.ArgumentParser(description=USAGE,
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--modified', '-m',
action='store_true', default=False,
help='Only check modified files')
parser.add_argument('--no-filter', '-a',
dest='show_all',
action='store_true', default=False,
help='Show all HTML files (no filtering)')
parser.add_argument("keyword", nargs="*", help='keyword to be searched')
options = parser.parse_args()
if options.show_all:
keywords = [] # type: List[str]
else:
if not options.keyword:
print('No keywords specified...try --help or --no-filter')
sys.exit(1)
else:
keywords = options.keyword
files = lister.list_files(
modified_only=options.modified,
ftypes=['handlebars', 'html'],
)
fns = [fn for fn in files if ('casperjs' not in fn) and ('puppet/zulip' not in fn)]
grep(fns, set(keywords))
if __name__ == '__main__':
check_our_files()