This repository was archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathr2wiki.py
66 lines (57 loc) · 2.64 KB
/
r2wiki.py
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
"""
Directions on how to setup is in the wiki itself.
"""
import os
import re
import sys
import pydoc
from argparse import ArgumentParser
from pygments import highlight
from pygments.lexers import MarkdownLexer
from pygments.formatters import TerminalFormatter
def arg_parse():
parse = ArgumentParser()
parse.add_argument('what_to_search_for', metavar='what_to_search_for',
help='What to search for. Supports regex . Triple escape escapes. \\\ ')
parse.add_argument('-a', action='store_true', dest='match_any',
help='Match exactly. Will ignore case.')
a = parse.parse_args()
return a
args = arg_parse()
if args.match_any:
pattern = re.compile('|'.join(args.what_to_search_for.split()), flags=re.IGNORECASE)
else:
pattern = re.compile(''.join('(?=.*{})'.format(arg) for arg in args.what_to_search_for.split()),
flags=re.IGNORECASE)
black_list = ['<p hidden>', '<!--', '<img src']
src_dir = os.path.dirname(os.path.realpath(__file__))
try:
found = ''
for path, subdirs, files in os.walk(src_dir):
for name in files:
md_path = os.path.join(path, name)
if not '.git' in md_path and md_path.endswith('.md'):
with open(md_path, 'r') as f:
for lines in f.readlines():
if re.search(pattern, lines):
if not any(filter in lines for filter in black_list):
match = ((re.sub('\*\*', '', lines)).lstrip() + '\n') # .replace('`', "'")
if match.startswith('- ['):
split = match.split(']')
found += max(split, key=len).replace('[', '') + \
' [.](https://radare2.securisec.com%s' % split[-1].strip('(')
else:
if match.startswith('>'):
match = ''.join(list(match)[1:])
if match.startswith('#'):
match = ''.join(list(match)[1:])
if match.startswith(' _'):
match = re.sub('_', '', ''.join(list(match)[1:]))
found += match
pydoc.pipepager(highlight(found, MarkdownLexer(), TerminalFormatter()), cmd='less -R')
except IndexError:
print 'Usage: %s search_param' % sys.argv[0]
except NameError:
print 'pip install Pygments'
except ImportError:
print 'Pygments dependency not met. pip install Pygments'