Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tumblr and imgur #184

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/AMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class AMP
public $passes = [
'Lullabot\AMP\Pass\PreliminaryPass', // Removes user blacklisted tags
'Lullabot\AMP\Pass\ImgTagTransformPass',
'Lullabot\AMP\Pass\ImgurTransformPass',
'Lullabot\AMP\Pass\TumblrTransformPass',
'Lullabot\AMP\Pass\IframeSoundCloudTagTransformPass',
'Lullabot\AMP\Pass\IframeFacebookTagTransformPass',
'Lullabot\AMP\Pass\AudioTagTransformPass',
Expand Down
13 changes: 13 additions & 0 deletions src/Pass/ImgTagTransformPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ protected function getImageWidthHeight($src)

// Try obtaining image size without having to download the whole image
$size = $this->fastimage->getImageSize($img_url);

if (!$size) {
// Now try with downloading the whole image
list($width, $height) = @getimagesize($src);

if ($width && $height) {
$size = [
'width' => $width,
'height' => $height,
];
}
}

return $size;
}

Expand Down
59 changes: 59 additions & 0 deletions src/Pass/ImgurTransformPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Lullabot\AMP\Pass;

use QueryPath\DOMQuery;

use Lullabot\AMP\Utility\ActionTakenLine;
use Lullabot\AMP\Utility\ActionTakenType;

/**
* Class ImgurTransformPass
* @package Lullabot\AMP\Pass
*/
class ImgurTransformPass extends ImgTagTransformPass
{
function pass()
{
$all_imgur = $this->q->top()->find('blockquote.imgur-embed-pub');
/** @var DOMQuery $el */
foreach ($all_imgur as $el) {
/** @var \DOMElement $dom_el */
$dom_el = $el->get(0);
$lineno = $this->getLineNo($dom_el);
$context_string = $this->getContextString($dom_el);

/** @var \DOMElement $new_dom_el */
$imgur_id = $el->attr('data-id');
$img_src = 'https://i.imgur.com/' . $imgur_id . '.png';
$size = $this->getImageWidthHeight($img_src);

if (!$size) {
$size['height'] = 400;
$size['width'] = 400;
}

$amp_string =<<<"HTML"
<amp-iframe
height="{$size['height']}"
width="{$size['width']}"
layout="responsive"
frameborder="0"
sandbox="allow-scripts allow-same-origin"
src="https://imgur.com/$imgur_id/embed">
<amp-img layout="fill" src="$img_src" placeholder=''></amp-img>
</amp-iframe>
HTML;

$el->after($amp_string);
$new_dom_el = $el->get(0);

// Remove the blockquote, its children
$el->removeChildren()->remove();
$this->addActionTaken(new ActionTakenLine('blockquote.imgur', ActionTakenType::IMGUR_CONVERTED, $lineno, $context_string));
$this->context->addLineAssociation($new_dom_el, $lineno);
}

return $this->transformations;
}
}
68 changes: 68 additions & 0 deletions src/Pass/TumblrTransformPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
namespace Lullabot\AMP\Pass;

use QueryPath\DOMQuery;

use Lullabot\AMP\Utility\ActionTakenLine;
use Lullabot\AMP\Utility\ActionTakenType;

/**
* Class TumblrTransformPass
* @package Lullabot\AMP\Pass
*/
class TumblrTransformPass extends BasePass
{
function pass()
{
$all_tumblr = $this->q->top()->find('div.tumblr-post');
/** @var DOMQuery $el */
foreach ($all_tumblr as $el) {
/** @var \DOMElement $dom_el */
$dom_el = $el->get(0);
$lineno = $this->getLineNo($dom_el);
$context_string = $this->getContextString($dom_el);
$script_tag = $this->getScriptTag($el, '&(*UTF8)tumblr\.com/post\.js&i');

$height = isset($this->options['tumblr_height'])
? $this->options['tumblr_height'] : 360;

$width = isset($this->options['tumblr_width'])
? $this->options['tumblr_width'] : 414;

$src = $el->attr('data-href');

if ($src) {
$amp_string =<<<"HTML"
<amp-iframe
height="$height"
width="$width"
layout="responsive"
frameborder="0"
sandbox="allow-scripts allow-same-origin"
src="$src"></amp-iframe>
HTML;

$el->after($amp_string);
$new_dom_el = $el->get(0);

if (!empty($script_tag)) {
$script_tag->remove();
$this->addActionTaken(new ActionTakenLine('a (with associated script tag)', ActionTakenType::TUMBLR_CONVERTED, $lineno, $context_string));
}
else {
$this->addActionTaken(new ActionTakenLine('a', ActionTakenType::TUMBLR_CONVERTED, $lineno, $context_string));
}
$this->context->addLineAssociation($new_dom_el, $lineno);
}
else {
$this->addActionTaken(new ActionTakenLine('div.tumblr-post', ActionTakenType::TUMBLR_COULD_NOT_BE_CONVERTED, $lineno, $context_string));
}

// Remove the div, its children
$el->removeChildren()->remove();

}

return $this->transformations;
}
}
4 changes: 4 additions & 0 deletions src/Utility/ActionTakenType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class ActionTakenType
const IMG_PIXEL_CONVERTED = 'tag was converted to the amp-pixel tag.';
const IMG_ANIM_CONVERTED = 'tag was converted to the amp-anim tag.';
const IMG_COULD_NOT_BE_CONVERTED = 'tag could NOT be converted to the amp-img tag as the image is not accessible.';
const IMGUR_CONVERTED = 'imgur tag was converted to the amp-iframe tag';
const IMGUR_COULD_NOT_BE_CONVERTED = 'imgur tag could NOT be converted to the amp-iframe tag';
const TUMBLR_CONVERTED = 'tumblr tag was converted to the amp-iframe tag';
const TUMBLR_COULD_NOT_BE_CONVERTED = 'tumblr tag could NOT be converted to the amp-iframe tag';
const INSTAGRAM_CONVERTED = 'instagram embed code was converted to the amp-instagram tag.';
const PINTEREST_CONVERTED = 'pinterest embed code was converted to the amp-pinterest tag.';
const VINE_CONVERTED = 'vine embed code was converted to the amp-vine tag.';
Expand Down
3 changes: 3 additions & 0 deletions tests/test-data/fragment-html/img-test-fragment.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@

<!-- should transform to amp-img instead of amp-anim because of default option['use_amp_anim_tag'] = false -->
<img src="https://upload.wikimedia.org/wikipedia/commons/b/bb/Quilt_design_as_46x46_uncompressed_GIF.gif">

<!-- should transform to amp-img -->
<blockquote class="imgur-embed-pub" lang="en" data-id="UY7mHHT"><a href="//imgur.com/UY7mHHT">Another victory - it fits!</a></blockquote>
14 changes: 13 additions & 1 deletion tests/test-data/fragment-html/img-test-fragment.html.out
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<!-- should transform to amp-img instead of amp-anim because of default option['use_amp_anim_tag'] = false -->
<amp-img src="https://upload.wikimedia.org/wikipedia/commons/b/bb/Quilt_design_as_46x46_uncompressed_GIF.gif" width="46" height="46" layout="fixed"></amp-img>

<!-- should transform to amp-img -->
<amp-iframe height="720" width="720" layout="responsive" frameborder="0" sandbox="allow-scripts allow-same-origin" src="https://imgur.com/UY7mHHT/embed">
<amp-img layout="fill" src="https://i.imgur.com/UY7mHHT.png" placeholder></amp-img>
</amp-iframe>


ORIGINAL HTML
---------------
Expand Down Expand Up @@ -59,6 +64,9 @@ Line 26:
Line 27: <!-- should transform to amp-img instead of amp-anim because of default option['use_amp_anim_tag'] = false -->
Line 28: <img src="https://upload.wikimedia.org/wikipedia/commons/b/bb/Quilt_design_as_46x46_uncompressed_GIF.gif">
Line 29:
Line 30: <!-- should transform to amp-img -->
Line 31: <blockquote class="imgur-embed-pub" lang="en" data-id="UY7mHHT"><a href="//imgur.com/UY7mHHT">Another victory - it fits!</a></blockquote>
Line 32:


Transformations made from HTML tags to AMP custom tags
Expand All @@ -79,6 +87,9 @@ Transformations made from HTML tags to AMP custom tags
<img src="https://upload.wikimedia.org/wikipedia/commons/b/bb/Quilt_design_as_46x46_uncompressed_GIF.gif"> at line 28
ACTION TAKEN: img tag was converted to the amp-img tag.

<blockquote class="imgur-embed-pub" lang="en" data-id="UY7mHHT"> at line 31
ACTION TAKEN: blockquote.imgur imgur tag was converted to the amp-iframe tag


AMP-HTML Validation Issues and Fixes
-------------------------------------
Expand Down Expand Up @@ -110,4 +121,5 @@ FAIL

COMPONENT NAMES WITH JS PATH
------------------------------
No custom amp script includes required
'amp-iframe', include path 'https://cdn.ampproject.org/v0/amp-iframe-0.1.js'

2 changes: 2 additions & 0 deletions tests/test-data/fragment-html/tumblr-fragment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div class="tumblr-post" data-href="https://embed.tumblr.com/embed/post/k33lX6gULMz1j_FK_w_a1g/145429050241" data-did="da39a3ee5e6b4b0d3255bfef95601890afd80709"><a href="http://potterlove975.tumblr.com/post/145429050241">http://potterlove975.tumblr.com/post/145429050241</a></div>
<script async="" src="https://assets.tumblr.com/post.js"></script>
24 changes: 24 additions & 0 deletions tests/test-data/fragment-html/tumblr-fragment.html.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<amp-iframe height="360" width="414" layout="responsive" frameborder="0" sandbox="allow-scripts allow-same-origin" src="https://embed.tumblr.com/embed/post/k33lX6gULMz1j_FK_w_a1g/145429050241"></amp-iframe>


ORIGINAL HTML
---------------
Line 1: <div class="tumblr-post" data-href="https://embed.tumblr.com/embed/post/k33lX6gULMz1j_FK_w_a1g/145429050241" data-did="da39a3ee5e6b4b0d3255bfef95601890afd80709"><a href="http://potterlove975.tumblr.com/post/145429050241">http://potterlove975.tumblr.com/post/145429050241</a></div>
Line 2: <script async="" src="https://assets.tumblr.com/post.js"></script>


Transformations made from HTML tags to AMP custom tags
-------------------------------------------------------

<div class="tumblr-post" data-href="https://embed.tumblr.com/embed/post/k33lX6gULMz1j_FK_w_a1g/145429050241" data-did="da39a3ee5e6b4b0d3255bfef95601890afd80709"> at line 1
ACTION TAKEN: a (with associated script tag) tumblr tag was converted to the amp-iframe tag


AMP-HTML Validation Issues and Fixes
-------------------------------------
PASS

COMPONENT NAMES WITH JS PATH
------------------------------
'amp-iframe', include path 'https://cdn.ampproject.org/v0/amp-iframe-0.1.js'