diff --git a/src/AMP.php b/src/AMP.php index e852ade7..7d3d94f9 100644 --- a/src/AMP.php +++ b/src/AMP.php @@ -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', diff --git a/src/Pass/ImgTagTransformPass.php b/src/Pass/ImgTagTransformPass.php index 8e28efeb..74a00b97 100644 --- a/src/Pass/ImgTagTransformPass.php +++ b/src/Pass/ImgTagTransformPass.php @@ -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; } diff --git a/src/Pass/ImgurTransformPass.php b/src/Pass/ImgurTransformPass.php new file mode 100644 index 00000000..58a3bffa --- /dev/null +++ b/src/Pass/ImgurTransformPass.php @@ -0,0 +1,59 @@ +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" + + + +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; + } +} diff --git a/src/Pass/TumblrTransformPass.php b/src/Pass/TumblrTransformPass.php new file mode 100644 index 00000000..7419b88f --- /dev/null +++ b/src/Pass/TumblrTransformPass.php @@ -0,0 +1,68 @@ +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" + +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; + } +} diff --git a/src/Utility/ActionTakenType.php b/src/Utility/ActionTakenType.php index 60b88025..2a244c46 100644 --- a/src/Utility/ActionTakenType.php +++ b/src/Utility/ActionTakenType.php @@ -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.'; diff --git a/tests/test-data/fragment-html/img-test-fragment.html b/tests/test-data/fragment-html/img-test-fragment.html index 69554435..1cfb1262 100644 --- a/tests/test-data/fragment-html/img-test-fragment.html +++ b/tests/test-data/fragment-html/img-test-fragment.html @@ -26,3 +26,6 @@ + + +
Another victory - it fits!
diff --git a/tests/test-data/fragment-html/img-test-fragment.html.out b/tests/test-data/fragment-html/img-test-fragment.html.out index 20a30bb1..204ba458 100644 --- a/tests/test-data/fragment-html/img-test-fragment.html.out +++ b/tests/test-data/fragment-html/img-test-fragment.html.out @@ -27,6 +27,11 @@ + + + + + ORIGINAL HTML --------------- @@ -59,6 +64,9 @@ Line 26: Line 27: Line 28: Line 29: +Line 30: +Line 31:
Another victory - it fits!
+Line 32: Transformations made from HTML tags to AMP custom tags @@ -79,6 +87,9 @@ Transformations made from HTML tags to AMP custom tags at line 28 ACTION TAKEN: img tag was converted to the amp-img tag. +
at line 31 + ACTION TAKEN: blockquote.imgur imgur tag was converted to the amp-iframe tag + AMP-HTML Validation Issues and Fixes ------------------------------------- @@ -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' + diff --git a/tests/test-data/fragment-html/tumblr-fragment.html b/tests/test-data/fragment-html/tumblr-fragment.html new file mode 100644 index 00000000..c88611f4 --- /dev/null +++ b/tests/test-data/fragment-html/tumblr-fragment.html @@ -0,0 +1,2 @@ +
http://potterlove975.tumblr.com/post/145429050241
+ \ No newline at end of file diff --git a/tests/test-data/fragment-html/tumblr-fragment.html.out b/tests/test-data/fragment-html/tumblr-fragment.html.out new file mode 100644 index 00000000..002e693e --- /dev/null +++ b/tests/test-data/fragment-html/tumblr-fragment.html.out @@ -0,0 +1,24 @@ + + + +ORIGINAL HTML +--------------- +Line 1:
http://potterlove975.tumblr.com/post/145429050241
+Line 2: + + +Transformations made from HTML tags to AMP custom tags +------------------------------------------------------- + +
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' +