def generate_url_contents(self, url):
"""Generate the formatted contents of the given item's url.
Converts the HTML to text using HTML2Text, colors it, then displays
the output in a pager.
:type url: str
:param url: The url whose contents to fetch.
:rtype: str
:return: The string representation of the formatted url contents.
"""
try:
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} # NOQA
raw_response = requests.get(url, headers=headers,
verify=self.config.verify_ssl)
except (requests.exceptions.SSLError,
requests.exceptions.ConnectionError) as e:
contents = 'Error: ' + str(e) + '\n'
contents += 'Try running gh view # with the --browser/-b flag\n'
return contents
contents = self.html_to_text.handle(raw_response.text)
# Strip out Unicode, which seems to have issues when html2txt is
# coupled with click.echo_via_pager.
contents = re.sub(r'[^\x00-\x7F]+', '', contents)
contents = self.format_markdown(contents)
return contents
评论列表
文章目录