Skip to content

Commit

Permalink
added colorization flag. updated tests. -a flag now shows link at the…
Browse files Browse the repository at this point in the history
… bottom
  • Loading branch information
gleitz committed Feb 16, 2013
2 parents 8103cea + 8c11994 commit 664387b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Usage
-p POS, --pos POS select answer in specified position (default: 1)
-a, --all display the full text of the answer
-l, --link display only the answer link
-c, --color enable colorized output
-n NUM_ANSWERS, --num-answers NUM_ANSWERS
number of answers to return

Expand Down
54 changes: 50 additions & 4 deletions howdoi/howdoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
except ImportError:
from urllib import quote as url_quote

from pygments import highlight
from pygments.lexers import guess_lexer, get_lexer_by_name
from pygments.formatters import TerminalFormatter
from pygments.util import ClassNotFound

from pyquery import PyQuery as pq
from requests.exceptions import ConnectionError

Expand Down Expand Up @@ -51,19 +56,58 @@ def get_link_at_pos(links, pos):
return link


def format_output(code, args):
if not args['color']:
return code
lexer = None

# try to find a lexer using the StackOverflow tags
# or the query arguments
for keyword in args['query'].split() + args['tags']:
try:
lexer = get_lexer_by_name(keyword)
break
except ClassNotFound:
pass

# no lexer found above, use the guesser
if not lexer:
lexer = guess_lexer(code)

return highlight(
code,
lexer,
TerminalFormatter(bg='dark')
)


def get_answer(args, links):
link = get_link_at_pos(links, args['pos'])
if args.get('link'):
return link
link = link + '?answertab=votes'
page = get_result(link)
page = get_result(link + '?answertab=votes')
html = pq(page)

first_answer = html('.answer').eq(0)
instructions = first_answer.find('pre') or first_answer.find('code')
if args['all'] or not instructions:
args['tags'] = [t.text for t in html('.post-tag')]

if not instructions and not args['all']:
text = first_answer.find('.post-text').eq(0).text()
elif args['all']:
texts = []
for html_tag in first_answer.items('.post-text > *'):
current_text = html_tag.text()
if current_text:
if html_tag[0].tag in ['pre', 'code']:
texts.append(format_output(current_text, args))
else:
texts.append(current_text)
texts.append(u'\n---\nAnswer from {0}'.format(link))
text = u'\n'.join(texts)
else:
text = instructions.eq(0).text()
text = format_output(instructions.eq(0).text(), args)
text = text.strip()
return text


Expand Down Expand Up @@ -104,6 +148,8 @@ def get_parser():
action='store_true')
parser.add_argument('-l','--link', help='display only the answer link',
action='store_true')
parser.add_argument('-c', '--color', help='enable colorized output',
action='store_true')
parser.add_argument('-n','--num-answers', help='number of answers to return', default=1, type=int)
return parser

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ lxml==3.0.1
pyquery==1.2.2
requests==1.1.0
wsgiref==0.1.2
pygments==1.5
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def read(*names):
},
install_requires=[
'pyquery',
'pygments',
'requests'
] + extra_dependencies(),
)
2 changes: 2 additions & 0 deletions test_howdoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def test_all_text(self):
first_answer = self.call_howdoi(query)
second_answer = self.call_howdoi(query + ' -a')
self.assertNotEqual(first_answer, second_answer)
self.assertIn("Answer from http://stackoverflow.com",
second_answer)

def test_multiple_answers(self):
query = self.queries[0]
Expand Down

0 comments on commit 664387b

Please sign in to comment.