Browse Source

convert newlines to <br> for html in tweets

pull/64/head v1.4.16
Aaron Parecki 6 years ago
parent
commit
cb1e32278d
No known key found for this signature in database GPG Key ID: 276C2817346D6056
3 changed files with 333 additions and 2 deletions
  1. +1
    -1
      lib/XRay/Formats/Twitter.php
  2. +14
    -1
      tests/TwitterTest.php
  3. +318
    -0
      tests/data/api.twitter.com/tweet-with-newlines.json

+ 1
- 1
lib/XRay/Formats/Twitter.php View File

@ -271,7 +271,7 @@ class Twitter extends Format {
// Twitter escapes & as &amp; in the text // Twitter escapes & as &amp; in the text
$text = html_entity_decode($text); $text = html_entity_decode($text);
$html = $text;
$html = str_replace("\n", "<br>\n", $text);
if(property_exists($entities, 'user_mentions')) { if(property_exists($entities, 'user_mentions')) {
foreach($entities->user_mentions as $user) { foreach($entities->user_mentions as $user) {

+ 14
- 1
tests/TwitterTest.php View File

@ -222,7 +222,7 @@ Feel like I could (maybe) rewrite previous open code to do some of this :)', $da
$this->assertEquals('https://pbs.twimg.com/media/DWaAhZ2UQAAIEoS.jpg', $data['data']['photo'][3]); $this->assertEquals('https://pbs.twimg.com/media/DWaAhZ2UQAAIEoS.jpg', $data['data']['photo'][3]);
} }
public function testStreamingTweetTruncatedWithVidoe() {
public function testStreamingTweetTruncatedWithVideo() {
list($url, $json) = $this->loadTweet('streaming-tweet-truncated-with-video'); list($url, $json) = $this->loadTweet('streaming-tweet-truncated-with-video');
$data = $this->parse(['url' => $url, 'body' => $json]); $data = $this->parse(['url' => $url, 'body' => $json]);
@ -231,4 +231,17 @@ Feel like I could (maybe) rewrite previous open code to do some of this :)', $da
$this->assertEquals('https://video.twimg.com/ext_tw_video/965608338917548032/pu/vid/720x720/kreAfCMf-B1dLqBH.mp4', $data['data']['video'][0]); $this->assertEquals('https://video.twimg.com/ext_tw_video/965608338917548032/pu/vid/720x720/kreAfCMf-B1dLqBH.mp4', $data['data']['video'][0]);
} }
public function testTweetWithNewlines() {
list($url, $json) = $this->loadTweet('tweet-with-newlines');
$data = $this->parse(['url' => $url, 'body' => $json]);
$this->assertEquals(4, substr_count($data['data']['content']['text'], "\n"));
$this->assertEquals(4, substr_count($data['data']['content']['html'], "<br>\n"));
$this->assertEquals("🌈🌈 I’ve watched the sun rise at Corona Heights countless times, but never before have I seen a #rainbow at #sunrise.
#CoronaHeights #SanFrancisco #SF #wakeupthesun #fromwhereirun #nofilter
Woke up this morning feeling compelled to run to Corona… http://tantek.com/2018/049/t3/rainbow-at-sunrise", $data['data']['content']['text']);
}
} }

+ 318
- 0
tests/data/api.twitter.com/tweet-with-newlines.json View File

@ -0,0 +1,318 @@
{
"created_at": "Mon Feb 19 07:53:50 +0000 2018",
"id": 965494622209363968,
"id_str": "965494622209363968",
"full_text": "🌈🌈 I’ve watched the sun rise at Corona Heights countless times, but never before have I seen a #rainbow at #sunrise.\n\n#CoronaHeights #SanFrancisco #SF #wakeupthesun #fromwhereirun #nofilter\n\nWoke up this morning feeling compelled to run to Corona… https://t.co/0otP3aLZo2 https://t.co/0Q5kNFZO9D",
"truncated": false,
"display_text_range": [
0,
271
],
"entities": {
"hashtags": [
{
"text": "rainbow",
"indices": [
95,
103
]
},
{
"text": "sunrise",
"indices": [
107,
115
]
},
{
"text": "CoronaHeights",
"indices": [
118,
132
]
},
{
"text": "SanFrancisco",
"indices": [
133,
146
]
},
{
"text": "SF",
"indices": [
147,
150
]
},
{
"text": "wakeupthesun",
"indices": [
151,
164
]
},
{
"text": "fromwhereirun",
"indices": [
165,
179
]
},
{
"text": "nofilter",
"indices": [
180,
189
]
}
],
"symbols": [
],
"user_mentions": [
],
"urls": [
{
"url": "https://t.co/0otP3aLZo2",
"expanded_url": "http://tantek.com/2018/049/t3/rainbow-at-sunrise",
"display_url": "tantek.com/2018/049/t3/ra…",
"indices": [
248,
271
]
}
],
"media": [
{
"id": 965494609689288705,
"id_str": "965494609689288705",
"indices": [
272,
295
],
"media_url": "http://pbs.twimg.com/media/DWYgO2sVoAEeRK0.jpg",
"media_url_https": "https://pbs.twimg.com/media/DWYgO2sVoAEeRK0.jpg",
"url": "https://t.co/0Q5kNFZO9D",
"display_url": "pic.twitter.com/0Q5kNFZO9D",
"expanded_url": "https://twitter.com/t/status/965494622209363968/photo/1",
"type": "photo",
"sizes": {
"medium": {
"w": 1200,
"h": 667,
"resize": "fit"
},
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"small": {
"w": 680,
"h": 378,
"resize": "fit"
},
"large": {
"w": 1920,
"h": 1067,
"resize": "fit"
}
}
}
]
},
"extended_entities": {
"media": [
{
"id": 965494609689288705,
"id_str": "965494609689288705",
"indices": [
272,
295
],
"media_url": "http://pbs.twimg.com/media/DWYgO2sVoAEeRK0.jpg",
"media_url_https": "https://pbs.twimg.com/media/DWYgO2sVoAEeRK0.jpg",
"url": "https://t.co/0Q5kNFZO9D",
"display_url": "pic.twitter.com/0Q5kNFZO9D",
"expanded_url": "https://twitter.com/t/status/965494622209363968/photo/1",
"type": "photo",
"sizes": {
"medium": {
"w": 1200,
"h": 667,
"resize": "fit"
},
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"small": {
"w": 680,
"h": 378,
"resize": "fit"
},
"large": {
"w": 1920,
"h": 1067,
"resize": "fit"
}
}
},
{
"id": 965494615313858560,
"id_str": "965494615313858560",
"indices": [
272,
295
],
"media_url": "http://pbs.twimg.com/media/DWYgPLpVwAAjpFL.jpg",
"media_url_https": "https://pbs.twimg.com/media/DWYgPLpVwAAjpFL.jpg",
"url": "https://t.co/0Q5kNFZO9D",
"display_url": "pic.twitter.com/0Q5kNFZO9D",
"expanded_url": "https://twitter.com/t/status/965494622209363968/photo/1",
"type": "photo",
"sizes": {
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"medium": {
"w": 900,
"h": 1200,
"resize": "fit"
},
"large": {
"w": 1440,
"h": 1920,
"resize": "fit"
},
"small": {
"w": 510,
"h": 680,
"resize": "fit"
}
}
},
{
"id": 965494620024053760,
"id_str": "965494620024053760",
"indices": [
272,
295
],
"media_url": "http://pbs.twimg.com/media/DWYgPdMVoAAMUbb.jpg",
"media_url_https": "https://pbs.twimg.com/media/DWYgPdMVoAAMUbb.jpg",
"url": "https://t.co/0Q5kNFZO9D",
"display_url": "pic.twitter.com/0Q5kNFZO9D",
"expanded_url": "https://twitter.com/t/status/965494622209363968/photo/1",
"type": "photo",
"sizes": {
"small": {
"w": 680,
"h": 510,
"resize": "fit"
},
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"medium": {
"w": 1200,
"h": 900,
"resize": "fit"
},
"large": {
"w": 1920,
"h": 1440,
"resize": "fit"
}
}
}
]
},
"source": "<a href=\"https://brid.gy/\" rel=\"nofollow\">Bridgy</a>",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 11628,
"id_str": "11628",
"name": "🌈",
"screen_name": "t",
"location": "Pacific Time Zone",
"description": "Cofounder #indieweb #barcamp @IndieWebCamp @microformats. Working @Mozilla @w3cab @csswg @socialwebwg. Code @Falcon @cassisjs. #write #climb #run #yoga #RESIST",
"url": "https://t.co/imeZHaJsth",
"entities": {
"url": {
"urls": [
{
"url": "https://t.co/imeZHaJsth",
"expanded_url": "http://tantek.com/",
"display_url": "tantek.com",
"indices": [
0,
23
]
}
]
},
"description": {
"urls": [
]
}
},
"protected": false,
"followers_count": 73397,
"friends_count": 1614,
"listed_count": 2020,
"created_at": "Tue Nov 07 02:26:19 +0000 2006",
"favourites_count": 0,
"utc_offset": -28800,
"time_zone": "Pacific Time (US & Canada)",
"geo_enabled": true,
"verified": false,
"statuses_count": 9252,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "C0DEED",
"profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
"profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
"profile_background_tile": false,
"profile_image_url": "http://pbs.twimg.com/profile_images/423350922408767488/nlA_m2WH_normal.jpeg",
"profile_image_url_https": "https://pbs.twimg.com/profile_images/423350922408767488/nlA_m2WH_normal.jpeg",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": false,
"following": true,
"follow_request_sent": false,
"notifications": false,
"translator_type": "regular"
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"retweet_count": 2,
"favorite_count": 8,
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"possibly_sensitive_appealable": false,
"lang": "en"
}

Loading…
Cancel
Save