Skip to content

Commit

Permalink
github: create(): convert links and images to markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Feb 25, 2018
1 parent 6124dce commit 0b94583
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
5 changes: 5 additions & 0 deletions granary/github.py
Expand Up @@ -141,6 +141,11 @@ class GitHub(source.Source):
POST_ID_RE = re.compile('^[0-9]+$')
# https://github.com/moby/moby/issues/679#issuecomment-18307522
REPO_NAME_RE = re.compile('^[A-Za-z0-9_.-]+$')
# https://github.com/Alir3z4/html2text/blob/master/docs/usage.md#available-options
HTML2TEXT_OPTIONS = {
'ignore_images': False,
'ignore_links': False,
}

def __init__(self, access_token=None):
"""Constructor.
Expand Down
18 changes: 13 additions & 5 deletions granary/source.py
Expand Up @@ -66,14 +66,19 @@ def __init__(self, *args, **kwargs):
super(RateLimited, self).__init__(*args, **kwargs)


def html_to_text(html):
"""Converts string html to string text with html2text."""
def html_to_text(html, **kwargs):
"""Converts string html to string text with html2text.
Args:
**kwargs: html2text options
https://github.com/Alir3z4/html2text/blob/master/docs/usage.md#available-options
"""
if html:
h = html2text.HTML2Text()
h.unicode_snob = True
h.body_width = 0 # don't wrap lines
h.ignore_links = True
h.ignore_images = True
h.ignore_links = kwargs.get('ignore_links', True)
h.ignore_images = kwargs.get('ignore_images', True)

# hacky monkey patch fix for html2text escaping sequences that are
# significant in markdown syntax. the X\\Y replacement depends on knowledge
Expand Down Expand Up @@ -169,10 +174,13 @@ class Source(object):
placeholder for the post URL and (optionally) a %(content)s placeholder
for the post content.
* POST_ID_RE: regexp, optional, matches valid post ids. Used in post_id().
* HTML2TEXT_OPTIONS: dict mapping string html2text option names to values
https://github.com/Alir3z4/html2text/blob/master/docs/usage.md#available-options
"""
__metaclass__ = SourceMeta

POST_ID_RE = None
HTML2TEXT_OPTIONS = {}

def user_url(self, user_id):
"""Returns the URL for a user's profile."""
Expand Down Expand Up @@ -917,7 +925,7 @@ def _content_for_create(self, obj, ignore_formatting=False, prefer_name=False,
is_html = (bool(BeautifulSoup(content, 'html.parser').find()) or
HTML_ENTITY_RE.search(content))
if is_html and not ignore_formatting:
content = html_to_text(content)
content = html_to_text(content, **self.HTML2TEXT_OPTIONS)
elif not is_html and ignore_formatting:
content = re.sub(r'\s+', ' ', content)

Expand Down
23 changes: 23 additions & 0 deletions granary/test/test_github.py
Expand Up @@ -616,6 +616,29 @@ def _test_create_issue(self, in_reply_to):
'url': 'https://github.com/foo/bar/issues/123',
}, result.content)

def test_create_with_image_and_link(self):
self.expect_requests_post(github.REST_API_CREATE_ISSUE % ('foo', 'bar'), json={
'title': 'an issue title',
'body': '[bar](http://foo/) ![](https://baz/)',
}, response={
'html_url': 'https://github.com/foo/bar/issues/123',
}, headers=EXPECTED_HEADERS)
self.mox.ReplayAll()

result = self.gh.create({
'url': 'https://github.com/foo/bar/issues/333',
'title': 'an issue title',
'content': """
<a href="http://foo/">bar</a>
<img src="https://baz/" />
""",
'inReplyTo': [{'url': 'https://github.com/foo/bar/issues'}],
})
self.assertIsNone(result.error_plain, result)
self.assert_equals({
'url': 'https://github.com/foo/bar/issues/123',
}, result.content)

def test_preview_issue(self):
for i in range(2):
rendered = self.expect_markdown_render(ISSUE_OBJ['content'].strip())
Expand Down
2 changes: 1 addition & 1 deletion requirements.freeze.txt
Expand Up @@ -5,7 +5,7 @@ chardet==3.0.4
coverage==4.0.3
-e git+https://github.com/snarfed/gdata-python-client-1.git@1df4e1efea7e5cf2754bc7eec6c1ab48ab09e3b1#egg=gdata
google-api-python-client==1.6.3
html2text==2016.9.19
html2text==2018.1.9
html5lib==0.9999999
httplib2==0.10.3
humanize==0.5.1
Expand Down

0 comments on commit 0b94583

Please sign in to comment.