def test_render_content_link_with_image(self): obj = { 'content': 'foo', 'tags': [{ 'objectType': 'article', 'url': 'http://link', 'displayName': 'name', 'image': { 'url': 'http://image' }, }] } self.assert_equals( """\ foo <a class="tag" href="http://link">name</a> """, microformats2.render_content(obj, render_attachments=False)) self.assert_equals( """\ foo <p> <a class="link" href="http://link"> <img class="u-photo" src="http://image" alt="name" /> <span class="name">name</span> </a> </p>""", microformats2.render_content(obj, render_attachments=True))
def test_render_content_video_audio(self): obj = { 'content': 'foo', 'attachments': [{ 'image': [{ 'url': 'http://im/age' }], 'stream': [{ 'url': 'http://vid/eo' }], 'objectType': 'video', }, { 'stream': [{ 'url': 'http://aud/io' }], 'objectType': 'audio', }], } self.assert_equals('foo', microformats2.render_content(obj)) self.assert_equals( """\ foo <p><video class="u-video" src="http://vid/eo" controls="controls" poster="http://im/age">Your browser does not support the video tag. <a href="http://vid/eo">Click here to view directly. <img src="http://im/age" /></a></video> </p> <p><audio class="u-audio" src="http://aud/io" controls="controls">Your browser does not support the audio tag. <a href="http://aud/io">Click here to listen directly.</a></audio> </p> """, microformats2.render_content(obj, render_attachments=True))
def test_render_content_multiple_image_attachments(self): obj = { 'content': 'foo', 'attachments': [ { 'objectType': 'image', 'image': { 'url': 'http://1' } }, { 'objectType': 'image', 'image': { 'url': 'http://2' } }, ], } self.assert_equals('foo', microformats2.render_content(obj)) self.assert_equals( """\ foo <p> <img class="u-photo" src="http://1" alt="" /> </p> <p> <img class="u-photo" src="http://2" alt="" /> </p>""", microformats2.render_content(obj, render_attachments=True))
def test_render_content_synthesize_content(self): for verb, phrase in ('like', 'likes'), ('share', 'shared'): obj = { 'verb': verb, 'object': {'url': 'http://orig/post'}, } self.assert_equals('<a href="http://orig/post">%s this.</a>' % phrase, microformats2.render_content(obj, synthesize_content=True)) self.assert_equals('', microformats2.render_content(obj, synthesize_content=False)) obj['content'] = 'Message from actor' for val in False, True: self.assert_equals(obj['content'], microformats2.render_content(obj, synthesize_content=val))
def test_escape_html_attribute_values(self): obj = { 'author': { 'image': {'url': 'author-img'}, 'displayName': 'a " b \' c', }, 'attachments': [{ 'objectType': 'image', 'image': {'url': 'att-img'}, 'displayName': 'd & e'}], } # TODO: test that img alt gets displayName 'd & e' once mf2py handles that. # https://github.com/tommorris/mf2py/issues/83 self.assert_multiline_equals("""\ <article class="h-entry"> <span class="p-uid"></span> <span class="p-author h-card"> <span class="p-name">a " b ' c</span> <img class="u-photo" src="author-img" alt="" /> </span> <span class="p-name"></span> <div class=""> </div> <img class="u-photo" src="att-img" alt="" /> </article>""", microformats2.object_to_html(obj), ignore_blanks=True) content = microformats2.render_content(obj, render_attachments=True) self.assert_multiline_equals("""\ <p> <img class="u-photo" src="att-img" alt="d & e" /> <span class="name">d & e</span> </p>""", content, ignore_blanks=True)
def test_render_content_shared_object_attachments(self): share = { 'verb': 'share', 'object': { 'content': 'foo', 'attachments': [{ 'image': [{ 'url': 'http://im/age' }], 'stream': [{ 'url': 'http://vid/eo' }], 'objectType': 'video', }, { 'stream': [{ 'url': 'http://aud/io' }], 'objectType': 'audio', }], }, } out = microformats2.render_content(share, render_attachments=True) self.assert_multiline_equals(""" Shared <a href="#">a post</a> by foo <p><video class="u-video" src="http://vid/eo" controls="controls" poster="http://im/age">Your browser does not support the video tag. <a href="http://vid/eo">Click here to view directly. <img src="http://im/age" /></a></video> </p> <p><audio class="u-audio" src="http://aud/io" controls="controls">Your browser does not support the audio tag. <a href="http://aud/io">Click here to listen directly.</a></audio> </p>""", out, ignore_blanks=True)
def test_tag_multiple_urls(self): expected_urls = ['http://1', 'https://2'] expected_html = """ <a class="tag" href="http://1"></a> <a class="tag" href="https://2"></a> """ for tag in ({ 'url': 'http://1', 'urls': [{ 'value': 'http://1' }, { 'value': 'https://2' }] }, { 'url': 'http://1', 'urls': [{ 'value': 'https://2' }] }, { 'urls': [{ 'value': 'http://1' }, { 'value': 'https://2' }] }): self.assert_equals( expected_urls, microformats2.object_to_json(tag)['properties']['url'], tag) self.assert_equals(expected_html, microformats2.render_content({'tags': [tag]}), tag)
def test_render_content_multiple_image_attachments(self): self.assert_equals( """\ foo <p> <img class="thumbnail" src="http://1" alt="" /> </p> <p> <img class="thumbnail" src="http://2" alt="" /> </p>""", microformats2.render_content({ 'content': 'foo', 'attachments': [ { 'objectType': 'image', 'image': { 'url': 'http://1' } }, { 'objectType': 'image', 'image': { 'url': 'http://2' } }, ] }))
def test_dont_render_images_inside_non_image_attachments(self): self.assert_equals('my content', microformats2.render_content({ 'content': 'my content', 'attachments': [{ 'objectType': 'note', 'image': {'url': 'http://attached/image'}, }], }))
def test_mention_and_hashtag(self): self.assert_equals(""" <a class="p-category" href="http://c"></a> <a class="u-mention" href="http://m">m</a>""", microformats2.render_content({ 'tags': [{'objectType': 'mention', 'url': 'http://m', 'displayName': 'm'}, {'objectType': 'hashtag', 'url': 'http://c'}], }))
def test_render_content_converts_newlines_to_brs(self): self.assert_equals("""\ foo<br /> bar<br /> <a href="http://baz">baz</a> """, microformats2.render_content({ 'content': 'foo\nbar\nbaz', 'tags': [{'url': 'http://baz', 'startIndex': 8, 'length': 3}] }))
def test_render_content_multiple_image_attachments(self): obj = { 'content': 'foo', 'attachments': [ {'objectType': 'image', 'image': {'url': 'http://1'}}, {'objectType': 'image', 'image': {'url': 'http://2'}}, ], } self.assert_equals('foo', microformats2.render_content(obj)) self.assert_equals("""\ foo <p> <img class="u-photo" src="http://1" alt="" /> </p> <p> <img class="u-photo" src="http://2" alt="" /> </p>""", microformats2.render_content(obj, render_attachments=True))
def test_render_content_omits_tags_without_urls(self): self.assert_equals("""\ foo <a class="tag" href="http://baj"></a> <a class="tag" href="http://baz">baz</a> """, microformats2.render_content({ 'content': 'foo', 'tags': [{'displayName': 'bar'}, {'url': 'http://baz', 'displayName': 'baz'}, {'url': 'http://baj'}, ], }))
def test_render_content_video_audio(self): obj = { 'content': 'foo', 'attachments': [{ 'image': [{'url': 'http://im/age'}], 'stream': [{'url': 'http://vid/eo'}], 'objectType': 'video', }, { 'stream': [{'url': 'http://aud/io'}], 'objectType': 'audio', }], } self.assert_equals('foo', microformats2.render_content(obj)) self.assert_equals("""\ foo <p><video class="u-video" src="http://vid/eo" controls="controls" poster="http://im/age">Your browser does not support the video tag. <a href="http://vid/eo">Click here to view directly. <img src="http://im/age" /></a></video> </p> <p><audio class="u-audio" src="http://aud/io" controls="controls">Your browser does not support the audio tag. <a href="http://aud/io">Click here to listen directly.</a></audio> </p> """, microformats2.render_content(obj, render_attachments=True))
def test_render_content_video(self): self.assert_equals("""\ foo <p><video class="thumbnail" src="http://vid/eo" poster="http://im/age" controls="controls">Your browser does not support the video tag. <a href="http://vid/eo">Click here to view directly<img src="http://im/age"/></a></video> </p> """, microformats2.render_content({ 'content': 'foo', 'attachments': [{ 'image': [{'url': 'http://im/age'}], 'stream': [{'url': 'http://vid/eo'}], 'objectType': 'video', }], }))
def test_render_content_location(self): self.assert_equals("""\ foo <span class="p-location h-card"> <a class="p-name u-url" href="http://my/place">My place</a> </span> """, microformats2.render_content({ 'content': 'foo', 'location': { 'displayName': 'My place', 'url': 'http://my/place', } }))
def test_render_content_multiple_image_attachments(self): self.assert_equals("""\ foo <p> <img class="thumbnail" src="http://1" alt="" /> </p> <p> <img class="thumbnail" src="http://2" alt="" /> </p>""", microformats2.render_content({ 'content': 'foo', 'attachments': [ {'objectType': 'image', 'image': {'url': 'http://1'}}, {'objectType': 'image', 'image': {'url': 'http://2'}}, ] }))
def test_render_content_link_with_image(self): obj = { 'content': 'foo', 'tags': [{ 'objectType': 'article', 'url': 'http://link', 'displayName': 'name', 'image': {'url': 'http://image'}, }] } self.assert_equals("""\ foo <a class="tag" href="http://link">name</a> """, microformats2.render_content(obj, render_attachments=False)) self.assert_equals("""\ foo <p> <a class="link" href="http://link"> <img class="u-photo" src="http://image" alt="name" /> <span class="name">name</span> </a> </p>""", microformats2.render_content(obj, render_attachments=True))
def test_render_content_converts_newlines_to_brs(self): self.assert_equals( """\ <div style="white-space: pre">foo bar <a href="http://baz">baz</a></div> """, microformats2.render_content({ 'content': 'foo\nbar\nbaz', 'tags': [{ 'url': 'http://baz', 'startIndex': 8, 'length': 3 }] }))
def test_render_content_link_with_image(self): self.assert_equals("""\ foo <p> <a class="link" href="http://link"> <img class="thumbnail" src="http://image" alt="name" /> <span class="name">name</span> </a> </p>""", microformats2.render_content({ 'content': 'foo', 'tags': [{ 'objectType': 'article', 'url': 'http://link', 'displayName': 'name', 'image': {'url': 'http://image'}, }] }))
def test_tag_multiple_urls(self): expected_urls = ['http://1', 'https://2'] expected_html = """ <a class="tag" href="http://1"></a> <a class="tag" href="https://2"></a> """ for tag in ({'url': 'http://1', 'urls': [{'value': 'http://1'}, {'value': 'https://2'}]}, {'url': 'http://1', 'urls': [{'value': 'https://2'}]}, {'urls': [{'value': 'http://1'}, {'value': 'https://2'}]}): self.assert_equals(expected_urls, microformats2.object_to_json(tag)['properties']['url'], tag) self.assert_equals(expected_html, microformats2.render_content({'tags': [tag]}), tag)
def test_escape_html_attribute_values(self): obj = { 'author': { 'image': { 'url': 'author-img' }, 'displayName': 'a " b \' c', }, 'attachments': [{ 'objectType': 'image', 'image': { 'url': 'att-img' }, 'displayName': 'd & e' }], } # TODO: test that img alt gets displayName 'd & e' once mf2py handles that. # https://github.com/tommorris/mf2py/issues/83 self.assert_multiline_equals("""\ <article class="h-entry"> <span class="p-uid"></span> <span class="p-author h-card"> <span class="p-name">a " b ' c</span> <img class="u-photo" src="author-img" alt="" /> </span> <span class="p-name"></span> <div class=""> </div> <img class="u-photo" src="att-img" alt="" /> </article>""", microformats2.object_to_html(obj), ignore_blanks=True) content = microformats2.render_content(obj, render_attachments=True) self.assert_multiline_equals("""\ <p> <img class="u-photo" src="att-img" alt="d & e" /> <span class="name">d & e</span> </p>""", content, ignore_blanks=True)
def test_render_content_unicode_high_code_points(self): """Test Unicode high code point chars. The first three unicode chars in the content are the '100' emoji, which is a high code point, ie above the Basic Multi-lingual Plane (ie 16 bits). The emacs font i use doesn't render it, so it looks blank. First discovered in https://twitter.com/schnarfed/status/831552681210556416 """ self.assert_equals( '💯💯💯 (by <a href="https://twitter.com/itsmaeril">@itsmaeril</a>)', microformats2.render_content({ 'content': '💯💯💯 (by @itsmaeril)', 'tags': [{ 'displayName': 'Maeril', 'objectType': 'person', 'startIndex': 8, 'length': 10, 'url': 'https://twitter.com/itsmaeril', }]}))
def test_render_content_unicode_high_code_points(self): """Test Unicode high code point chars. The first three unicode chars in the content are the '100' emoji, which is a high code point, ie above the Basic Multi-lingual Plane (ie 16 bits). The emacs font i use doesn't render it, so it looks blank. First discovered in https://twitter.com/schnarfed/status/831552681210556416 """ self.assert_equals( u'💯💯💯 (by <a href="https://twitter.com/itsmaeril">@itsmaeril</a>)', microformats2.render_content({ 'content': u'💯💯💯 (by @itsmaeril)', 'tags': [{ 'displayName': 'Maeril', 'objectType': 'person', 'startIndex': 8, 'length': 10, 'url': 'https://twitter.com/itsmaeril', }]}))
def test_render_content_shared_object_attachments(self): share = { 'verb': 'share', 'object': { 'content': 'foo', 'attachments': [{ 'image': [{'url': 'http://im/age'}], 'stream': [{'url': 'http://vid/eo'}], 'objectType': 'video', }, { 'stream': [{'url': 'http://aud/io'}], 'objectType': 'audio', }], }, } out = microformats2.render_content(share, render_attachments=True) self.assert_multiline_equals(""" Shared <a href="#">a post</a> by foo <p><video class="u-video" src="http://vid/eo" controls="controls" poster="http://im/age">Your browser does not support the video tag. <a href="http://vid/eo">Click here to view directly. <img src="http://im/age" /></a></video> </p> <p><audio class="u-audio" src="http://aud/io" controls="controls">Your browser does not support the audio tag. <a href="http://aud/io">Click here to listen directly.</a></audio> </p>""", out, ignore_blanks=True)
file.write("date: %s\n" % date.strftime('%Y-%m-%d %H:%M:%S')) # now we figure out what kind of Tweet this is: # new post, reply to someone, or retweet. # i don't "like" anything, so i don't need to worry about # that activity here. if decoded['verb'] == 'share': # we'll deal with retweets first. file.write("repost_url: %s\n" % decoded['object']['url']) file.write("repost_name: %s\n" % decoded['object']['author']['username']) if decoded['verb'] == 'post': # this is either a new post, or a reply if 'inReplyTo' in decoded['object']: # okay, this is a rewteet. # but sometimes the user ID is invalid. if 'tags' in decoded['object'] and decoded['object']['tags'][0]['url']: reply_user = "******" % decoded['object']['tags'][0]['url'].replace('https://twitter.com/', '') else: reply_user = '******' file.write("reply_to_user: %s\n" % reply_user) file.write("reply_to_url: %s\n" % decoded['object']['inReplyTo'][0]['url']) file.write("tweet_url: %s\n" % decoded['url']) file.write('---\n') # we use the render_content() method here to ensure that we get # untruncated links in the output. file.write('%s\n' % microformats2.render_content(obj, False)) file.write("\n") file.close()