├── .github └── workflows │ └── test.yml ├── .gitignore ├── License.md ├── README.md ├── composer.json ├── phpspec.yml ├── spec └── ConverterSpec.php └── src ├── Converter.php ├── TagConverter ├── AMPAudio.php ├── AMPDailymotion.php ├── AMPFacebook.php ├── AMPGfycat.php ├── AMPGist.php ├── AMPIframe.php ├── AMPImg.php ├── AMPImgLightbox.php ├── AMPImgZoom.php ├── AMPImgur.php ├── AMPInstagram.php ├── AMPPinterest.php ├── AMPPlaybuzz.php ├── AMPTwitter.php ├── AMPVideo.php ├── AMPVimeo.php ├── AMPVk.php └── AMPYoutube.php ├── TagConverterInterface.php └── Util └── ImageSize.php /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | tests: 7 | runs-on: ubuntu-20.04 8 | 9 | strategy: 10 | matrix: 11 | php: ['7.3', '7.4', '8.0', '8.1', '8.2'] 12 | 13 | name: PHP ${{ matrix.php }} 14 | 15 | steps: 16 | - uses: actions/checkout@v2 17 | - uses: shivammathur/setup-php@v2 18 | with: 19 | php-version: ${{ matrix.php }} 20 | extensions: ctype, dom, libxml, mbstring 21 | tools: composer:v2 22 | coverage: none 23 | - run: composer install --prefer-source --no-interaction 24 | - run: vendor/bin/phpspec run -v --format=progress -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.lock -------------------------------------------------------------------------------- /License.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 András Magyar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AMP ⚡ converter library 2 | 3 | [![Build Status](https://github.com/magyarandras/amp-converter/workflows/test/badge.svg)](https://github.com/magyarandras/amp-converter/actions) 4 | 5 | A library to convert HTML articles, blog posts or similar content to [AMP (Accelerated Mobile Pages)](https://amp.dev). 6 | 7 | **Note**: This library is not intended to convert entire HTML documents if you want to convert an entire page you should use a more advanced library, for example: [Lullabot/amp-library](https://github.com/Lullabot/amp-library/) 8 | 9 | ## Installation: 10 | 11 | ``` 12 | composer require magyarandras/amp-converter 13 | ``` 14 | 15 | ## Currently supported elments: 16 | * [x] amp-img 17 | * [x] amp-img in amp-pan-zoom 18 | * [x] amp-img with lightbox 19 | * [x] amp-video 20 | * [x] amp-audio 21 | * [x] amp-iframe(A placeholder is automatically added to the iframes, so you can [embed iframes even above the fold](https://www.youtube.com/watch?v=TqfYmlkHVCs).) 22 | * [x] amp-youtube 23 | * [x] amp-facebook 24 | * [x] amp-instagram 25 | * [x] amp-twitter 26 | * [x] amp-pinterest 27 | * [x] amp-playbuzz 28 | * [x] amp-gist(Github gist embed) 29 | * [x] amp-vimeo 30 | * [ ] amp-soundcloud(You can use amp-iframe instead) 31 | * [x] amp-vk 32 | * [x] amp-imgur 33 | * [x] amp-dailymotion 34 | * [x] amp-gfycat 35 | 36 | ## Usage: 37 | 38 | Simple example: 39 | 40 | **Make sure your HTML code doesn't contain tags or attributes invalid in HTML5 otherwise, the generated AMP will be invalid too.** 41 | 42 | ```php 43 | loadDefaultConverters(); 59 | 60 | //HTML to convert 61 | $html = ' 62 |

This is a sample HTML code generated by TinyMCE.

63 |

64 |

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

65 |

66 |

 

67 |
68 |

Check out the latest post on the AMP Blog to learn how @AdobeExpCloud has been working to seamless integrate AMP support into its applications ⚡

Learn more here 👉 https://t.co/hX3QmJ707x

69 | — AMP Project (@AMPhtml) April 10, 2020
70 | '; 71 | 72 | //Convert html to amp html 73 | $amphtml = $converter->convert($html); 74 | 75 | //Get the necessary amp components 76 | $amp_scripts = $converter->getScripts(); 77 | 78 | print_r($amphtml); 79 | echo PHP_EOL; 80 | print_r($amp_scripts); 81 | 82 | ``` 83 | The output: 84 | ```html 85 |

This is a sample HTML code generated by TinyMCE.

86 |

87 |

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

88 |

89 |

 

90 | 91 | ``` 92 | 93 | ``` 94 | Array 95 | ( 96 | [0] => 97 | [1] => 98 | ) 99 | ``` 100 | 101 | You can specify which converters to use by loading them manually: 102 | 103 | ```php 104 | addConverter(new AMPImgZoom()); 117 | $converter->addConverter(new AMPYoutube()); 118 | 119 | $amphtml = $converter->convert($html); 120 | 121 | $amp_scripts = $converter->getScripts(); 122 | 123 | print_r($amphtml); 124 | print_r($amp_scripts); 125 | 126 | ``` 127 | 128 | ## Images with unknown sizes 129 | 130 | If you have images with unknown dimensions in your HTML code (you should avoid this situation if possible) and use relative URLs, you must pass the images' base URL to the constructor. You can also specify the time limit for obtaining the size of a single image (the default is 10 seconds). (Note: the time limit applies for every single image, not for the entire process) 131 | 132 | ```php 133 | 'https://example.com', //This base URL will be used if the src of an image is a relative path. 139 | 'image_timeout' => 5 //Obtaining the size of a single image (downloading the required data) can't take longer than 5 seconds. 140 | ]); 141 | 142 | //Load built-in converters 143 | $converter->loadDefaultConverters(); 144 | 145 | //HTML to convert 146 | $html = ' 147 |

This is a sample HTML code generated by TinyMCE.

148 |

Sample image

149 | '; 150 | 151 | //Convert html to amp html 152 | $amphtml = $converter->convert($html); 153 | 154 | //Get the necessary amp components 155 | $amp_scripts = $converter->getScripts(); 156 | 157 | print_r($amphtml); 158 | echo PHP_EOL; 159 | print_r($amp_scripts); 160 | ``` 161 | 162 | ## Writing your own converters: 163 | 164 | The library can't support everything out of the box, but you can extend it with your own converters(or you can replace existing ones if you need). 165 | 166 | For example, consider the following: You use a jQuery countdown library in some of your articles/blog posts and you want to convert the following code to AMP. 167 | 168 | ```html 169 | 170 |
171 | 172 | 173 | 181 | ``` 182 | 183 | You can create a custom converter class that implements the TagConverterInterface. 184 | 185 | ```php 186 | ', 195 | '' 196 | ]; 197 | 198 | public function convert(\DOMDocument $doc) 199 | { 200 | 201 | $query = '//div[@data-countdown]'; 202 | 203 | $xpath = new \DOMXPath($doc); 204 | 205 | $entries = $xpath->query($query); 206 | 207 | if ($entries->length > 0) { 208 | $this->necessary_scripts = $this->extension_scripts; 209 | } 210 | 211 | foreach ($entries as $tag) { 212 | 213 | //Although in this example there isn't any validation, you definitely should check if the date is valid. 214 | $timestamp = strtotime($tag->getAttribute('data-countdown')); 215 | 216 | $countdown = $doc->createElement('amp-date-countdown'); 217 | 218 | $countdown->setAttribute('timestamp-seconds', $timestamp); 219 | $countdown->setAttribute('layout', 'fixed-height'); 220 | $countdown->setAttribute('height', '50'); 221 | 222 | $template = $doc->createElement('template'); 223 | $template->setAttribute('type', 'amp-mustache'); 224 | 225 | $paragraph = $doc->createElement('p'); 226 | $paragraph->setAttribute('class', 'p1'); 227 | 228 | $text = $doc->createTextNode('{{d}} days, {{h}} hours, {{m}} minutes and {{s}} seconds'); 229 | 230 | $paragraph->appendChild($text); 231 | 232 | $template->appendChild($paragraph); 233 | $countdown->appendChild($template); 234 | 235 | $tag->parentNode->replaceChild($countdown, $tag); 236 | } 237 | 238 | 239 | 240 | return $doc; 241 | } 242 | 243 | public function getNecessaryScripts() 244 | { 245 | return $this->necessary_scripts; 246 | } 247 | 248 | } 249 | 250 | ``` 251 | 252 | Using the custom converter: 253 | 254 | ```php 255 | loadDefaultConverters(); 266 | 267 | $converter->addConverter(new CountdownConverter()); 268 | 269 | $html = '

Hello!

'; 270 | 271 | //Convert html to amp html 272 | $amphtml = $converter->convert($html); 273 | 274 | //Get the necessary amp components 275 | $amp_scripts = $converter->getScripts(); 276 | 277 | print_r($amphtml); 278 | print_r($amp_scripts); 279 | 280 | ``` -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "magyarandras/amp-converter", 3 | "description": "A library to convert HTML articles, blog posts or similar content to AMP (Accelerated Mobile Pages).", 4 | "keywords": ["amp", "html", "amphtml", "amp-html", "converter"], 5 | "type": "library", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "András Magyar", 10 | "email": "magyarandrasistvan@gmail.com" 11 | } 12 | ], 13 | "autoload": { 14 | "psr-4": { 15 | "magyarandras\\AMPConverter\\":"src/" 16 | } 17 | }, 18 | "require": { 19 | "ext-dom": "*", 20 | "masterminds/html5": "^2.6", 21 | "fasterimage/fasterimage": "^1.5" 22 | }, 23 | "require-dev": { 24 | "phpspec/phpspec": "^7.3" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /phpspec.yml: -------------------------------------------------------------------------------- 1 | suites: 2 | default: 3 | namespace: magyarandras\AMPConverter 4 | psr4_prefix: magyarandras\AMPConverter -------------------------------------------------------------------------------- /spec/ConverterSpec.php: -------------------------------------------------------------------------------- 1 | shouldHaveType(Converter::class); 35 | } 36 | 37 | public function it_converts_html_to_amp() 38 | { 39 | $this->addConverter(new AMPImg()); 40 | $this->convert('

text

') 41 | ->shouldReturn('

text

'); 42 | } 43 | 44 | public function it_removes_prohibited_attributes() 45 | { 46 | $this->convert('

hh

Hello!
') 47 | ->shouldReturn('

hh

'); 48 | } 49 | 50 | public function it_converts_img_to_ampimg() 51 | { 52 | $this->addConverter(new AMPImg()); 53 | 54 | $this->convert('Sample image') 55 | ->shouldReturn(''); 56 | 57 | $this->getScripts()->shouldReturn([]); 58 | } 59 | 60 | public function it_converts_img_to_ampimg_with_zoom() 61 | { 62 | $this->addConverter(new AMPImgZoom()); 63 | 64 | $this->convert('Sample image') 65 | ->shouldReturn(''); 66 | 67 | $this->getScripts()->shouldReturn([ 68 | '' 69 | ]); 70 | } 71 | 72 | public function it_converts_img_to_ampimg_with_lightbox() 73 | { 74 | $this->addConverter(new AMPImgLightbox()); 75 | 76 | $this->convert('Sample image') 77 | ->shouldReturn(''); 78 | 79 | $this->getScripts()->shouldReturn([ 80 | '' 81 | ]); 82 | } 83 | 84 | public function it_converts_youtube_videos() 85 | { 86 | $this->addConverter(new AMPYoutube()); 87 | 88 | $this->convert('') 89 | ->shouldReturn(''); 90 | 91 | $this->getScripts()->shouldReturn([ 92 | '' 93 | ]); 94 | } 95 | 96 | public function it_converts_iframe_to_ampiframe() 97 | { 98 | $this->addConverter(new AMPIframe()); 99 | 100 | $this->convert('') 101 | ->shouldReturn('

Loading...

'); 102 | 103 | $this->getScripts()->shouldReturn([ 104 | '' 105 | ]); 106 | } 107 | 108 | public function it_converts_video_to_ampvideo() 109 | { 110 | $this->addConverter(new AMPVideo()); 111 | 112 | $this->convert('') 116 | ->shouldReturn(' 117 | 118 | 119 | '); 120 | 121 | $this->getScripts()->shouldReturn([ 122 | '' 123 | ]); 124 | } 125 | 126 | public function it_converts_audio_to_ampaudio() 127 | { 128 | $this->addConverter(new AMPAudio()); 129 | 130 | $this->convert('') 134 | ->shouldReturn(' 135 | 136 | 137 | '); 138 | 139 | $this->getScripts()->shouldReturn([ 140 | '' 141 | ]); 142 | } 143 | 144 | public function it_converts_fb_posts_to_ampfacebook() 145 | { 146 | $this->addConverter(new AMPFacebook()); 147 | 148 | $this->convert('
') 149 | ->shouldReturn(''); 150 | 151 | $this->getScripts()->shouldReturn([ 152 | '' 153 | ]); 154 | } 155 | 156 | public function it_converts_facebook_iframe_post_embed_to_ampfacebook() 157 | { 158 | $this->addConverter(new AMPFacebook()); 159 | 160 | $this->convert('') 161 | ->shouldReturn(''); 162 | 163 | $this->getScripts()->shouldReturn([ 164 | '' 165 | ]); 166 | } 167 | 168 | public function it_converts_facebook_iframe_video_embed_to_ampfacebook() 169 | { 170 | $this->addConverter(new AMPFacebook()); 171 | 172 | $this->convert('') 173 | ->shouldReturn(''); 174 | 175 | $this->getScripts()->shouldReturn([ 176 | '' 177 | ]); 178 | } 179 | 180 | public function it_converts_fb_video_to_ampfacebook() 181 | { 182 | $this->addConverter(new AMPFacebook()); 183 | 184 | $this->convert('
185 |
186 |
187 | How to Share With Just Friends 188 |

How to share with just friends.

189 | Posted by Facebook on Friday, December 5, 2014 190 |
191 |
192 |
') 193 | ->shouldReturn(''); 194 | 195 | $this->getScripts()->shouldReturn([ 196 | '' 197 | ]); 198 | } 199 | 200 | public function it_converts_fb_comment_to_ampfacebook() 201 | { 202 | $this->addConverter(new AMPFacebook()); 203 | 204 | $this->convert('
') 207 | ->shouldReturn(''); 208 | 209 | $this->getScripts()->shouldReturn([ 210 | '' 211 | ]); 212 | } 213 | 214 | public function it_converts_pinterest_embed_to_amppinterest() 215 | { 216 | $this->addConverter(new AMPPinterest()); 217 | 218 | $this->convert('') 219 | ->shouldReturn(''); 220 | 221 | $this->getScripts()->shouldReturn([ 222 | '' 223 | ]); 224 | } 225 | 226 | public function it_converts_playbuzz_embed_to_ampplaybuzz() 227 | { 228 | $this->addConverter(new AMPPlaybuzz()); 229 | 230 | $this->convert(' 231 |
') 232 | ->shouldReturn(''); 233 | 234 | $this->getScripts()->shouldReturn([ 235 | '' 236 | ]); 237 | } 238 | 239 | public function it_converts_imgur_embeds_to_ampimgur() 240 | { 241 | $this->addConverter(new AMPImgur()); 242 | 243 | $this->convert('
can't see? no problem
') 244 | ->shouldReturn(''); 245 | 246 | $this->getScripts()->shouldReturn([ 247 | '' 248 | ]); 249 | } 250 | 251 | public function it_converts_gist_embed_to_ampgist() 252 | { 253 | $this->addConverter(new AMPGist()); 254 | 255 | $this->convert('') 256 | ->shouldReturn(''); 257 | 258 | $this->getScripts()->shouldReturn([ 259 | '' 260 | ]); 261 | } 262 | 263 | public function it_converts_twitter_post_embed_to_amptwitter() 264 | { 265 | $this->addConverter(new AMPTwitter()); 266 | 267 | $this->convert('

Not just web pages need better UX. Here's how we bring @AMPhtml to other parts of the internet: https://t.co/hSqH3nKsr7

— Paul Bakaus (@pbakaus) May 15, 2018
') 268 | ->shouldReturn(''); 269 | 270 | $this->getScripts()->shouldReturn([ 271 | '' 272 | ]); 273 | } 274 | 275 | public function it_converts_instagram_post_embed_to_ampinstagram() 276 | { 277 | $this->addConverter(new AMPInstagram()); 278 | 279 | $this->convert('
A bejegyzés megtekintése az Instagramon

We’re putting the Weekend Hashtag Project on hold this weekend. Instead, we’re challenging people around the world to participate in the 10th Worldwide InstaMeet! Grab a few good friends or meet up with a larger group in your area and share your best photos and videos from the InstaMeet with the #WWIM10 hashtag for a chance to be featured on our blog Monday morning. Be sure to include the name of the location where your event took place along with the unique hashtag you've chosen for your InstaMeet in your caption. Photo by @sun_shinealight

Instagram (@instagram) által megosztott bejegyzés,

') 280 | ->shouldReturn(''); 281 | 282 | $this->getScripts()->shouldReturn([ 283 | '' 284 | ]); 285 | } 286 | 287 | public function it_converts_instagram_igtv_embed_to_ampinstagram() 288 | { 289 | $this->addConverter(new AMPInstagram()); 290 | 291 | $this->convert('
View this post on Instagram

A post shared by 9GAG: Go Fun The World (@9gag)

') 292 | ->shouldReturn(''); 293 | 294 | $this->getScripts()->shouldReturn([ 295 | '' 296 | ]); 297 | } 298 | 299 | public function it_converts_instagram_reel_embed_to_ampinstagram() 300 | { 301 | $this->addConverter(new AMPInstagram()); 302 | 303 | $this->convert('
View this post on Instagram

A post shared by 9GAG: Go Fun The World (@9gag)

') 304 | ->shouldReturn(''); 305 | 306 | $this->getScripts()->shouldReturn([ 307 | '' 308 | ]); 309 | } 310 | 311 | public function it_converts_vk_embed_to_ampvk() 312 | { 313 | $this->addConverter(new AMPVk()); 314 | 315 | $this->convert('
') 320 | ->shouldReturn(''); 321 | 322 | $this->getScripts()->shouldReturn([ 323 | '' 324 | ]); 325 | } 326 | 327 | public function it_converts_vimeo_embed_to_ampvimeo() 328 | { 329 | $this->addConverter(new AMPVimeo()); 330 | 331 | $this->convert('') 332 | ->shouldReturn(''); 333 | 334 | $this->getScripts()->shouldReturn([ 335 | '' 336 | ]); 337 | } 338 | 339 | public function it_converts_dailymotion_embed_to_ampdailymotion() 340 | { 341 | $this->addConverter(new AMPDailymotion()); 342 | 343 | $this->convert('') 344 | ->shouldReturn(''); 345 | 346 | $this->getScripts()->shouldReturn([ 347 | '' 348 | ]); 349 | } 350 | 351 | public function it_converts_gfycat_embed_to_ampgfycat() 352 | { 353 | $this->addConverter(new AMPGfycat()); 354 | 355 | $this->convert('

from Silicon Valley GIFs via Gfycat

') 356 | ->shouldReturn('

from Silicon Valley GIFs via Gfycat

'); 357 | 358 | $this->getScripts()->shouldReturn([ 359 | '' 360 | ]); 361 | } 362 | } 363 | -------------------------------------------------------------------------------- /src/Converter.php: -------------------------------------------------------------------------------- 1 | '*', 35 | 'width' => 'hr|table|td|th|col|colgroup|pre', 36 | 'height' => 'hr|table|td|th|col|colgroup|pre', 37 | 'valign' => 'col|colgroup|tbody|td|tfoot|th|thead|tr', 38 | 'type' => 'li|ol|ul', 39 | 'size' => 'hr', 40 | 'rules' => 'table', 41 | 'nowrap' => 'td|th', 42 | 'noshade' => 'hr', 43 | 'frame' =>'table', 44 | 'compact' => 'dl|ol|ul', 45 | 'clear' => 'br', 46 | 'charoff' => 'col|colgroup|tbody|td|tfoot|th|thead|tr', 47 | 'char' => 'col|colgroup|tbody|td|tfoot|th|thead|tr', 48 | 'cellspacing' => 'table', 49 | 'cellpadding' => 'table', 50 | 'border' => '*', 51 | 'bgcolor' => 'table|tr|td|th', 52 | 'align' => '*', 53 | 'scope' => 'td', 54 | 'axis' => 'td', 55 | 'abbr' => 'td', 56 | 'charset' => 'a', 57 | 'coords' => 'a', 58 | 'shape' => 'a', 59 | 'onkeydown' => '*', 60 | 'onkeyup' => '*', 61 | 'onkeypress' => '*', 62 | 'onclick' => '*', 63 | 'ondblclick' => '*', 64 | 'onmousedown' => '*', 65 | 'onmousemove' => '*', 66 | 'onmouseout' => '*', 67 | 'onmouseover' => '*', 68 | 'onmouseup' => '*', 69 | 'onmousewheel' => '*', 70 | 'onwheel' => '*', 71 | 'ondrag' => '*', 72 | 'ondragend' => '*', 73 | 'ondragenter' => '*', 74 | 'ondragleave' => '*', 75 | 'ondragover' => '*', 76 | 'ondragstart' => '*', 77 | 'ondrop' => '*', 78 | 'onscroll' => '*', 79 | 'ontoggle' => '*', 80 | 'oncopy' => '*', 81 | 'onpaste' => '*', 82 | 'oncut' => '*' 83 | ]; 84 | 85 | private $converters = [ 86 | 87 | ]; 88 | 89 | private $scripts = [ 90 | 91 | ]; 92 | 93 | private $base_url = ''; 94 | 95 | private $image_timeout = 10; 96 | 97 | public function __construct($arg = '') 98 | { 99 | if (is_array($arg)) { 100 | 101 | if (array_key_exists('image_base_url', $arg)) { 102 | $this->base_url = $arg['image_base_url']; 103 | } 104 | 105 | if (array_key_exists('image_timeout', $arg)) { 106 | $this->image_timeout = $arg['image_timeout']; 107 | } 108 | 109 | } else { 110 | $this->base_url = $arg; 111 | } 112 | } 113 | 114 | public function loadDefaultConverters() 115 | { 116 | $built_in_converters = [ 117 | new \magyarandras\AMPConverter\TagConverter\AMPImg($this->base_url, $this->image_timeout), 118 | new \magyarandras\AMPConverter\TagConverter\AMPYoutube, 119 | new \magyarandras\AMPConverter\TagConverter\AMPVideo, 120 | new \magyarandras\AMPConverter\TagConverter\AMPAudio, 121 | new \magyarandras\AMPConverter\TagConverter\AMPVimeo, 122 | new \magyarandras\AMPConverter\TagConverter\AMPFacebook, 123 | new \magyarandras\AMPConverter\TagConverter\AMPPinterest, 124 | new \magyarandras\AMPConverter\TagConverter\AMPPlaybuzz, 125 | new \magyarandras\AMPConverter\TagConverter\AMPImgur, 126 | new \magyarandras\AMPConverter\TagConverter\AMPDailymotion, 127 | new \magyarandras\AMPConverter\TagConverter\AMPGfycat, 128 | new \magyarandras\AMPConverter\TagConverter\AMPGist, 129 | new \magyarandras\AMPConverter\TagConverter\AMPTwitter, 130 | new \magyarandras\AMPConverter\TagConverter\AMPInstagram, 131 | new \magyarandras\AMPConverter\TagConverter\AMPVk, 132 | //AMPIframe must be the last 133 | new \magyarandras\AMPConverter\TagConverter\AMPIframe 134 | ]; 135 | 136 | 137 | foreach ($built_in_converters as $converter) { 138 | $this->converters[] = $converter; 139 | } 140 | } 141 | 142 | public function convert($html) 143 | { 144 | $html5 = new HTML5([ 145 | 'disable_html_ns' => true 146 | ]); 147 | $this->doc = $html5->loadHTML($html); 148 | 149 | $this->removeIncorrectDimensionAttributes(); 150 | 151 | foreach ($this->converters as $converter) { 152 | $result = $converter->convert($this->doc); 153 | $this->doc = $result; 154 | 155 | $this->scripts = array_unique(array_merge($this->scripts, $converter->getNecessaryScripts())); 156 | } 157 | 158 | $this->removeProhibitedTags(); 159 | $this->removeProhibitedAttributes(); 160 | 161 | $amphtml = $html5->saveHTML($this->doc); 162 | 163 | $to_replace = [ 164 | '', '', '', '', '', '', '' 165 | ]; 166 | 167 | $amphtml = str_replace($to_replace, '', $amphtml); 168 | 169 | $amphtml = trim($amphtml, "\n\r\0\x0B"); 170 | 171 | 172 | return $amphtml; 173 | } 174 | 175 | public function addConverter(TagConverterInterface $converter) 176 | { 177 | //array_unshift($this->converters, $converter); 178 | $this->converters[] = $converter; 179 | } 180 | 181 | 182 | //Remove prohibited tags 183 | private function removeProhibitedTags() 184 | { 185 | $query = '//' . implode('|//', $this->prohibited_tags); 186 | 187 | $xpath = new \DOMXPath($this->doc); 188 | 189 | $entries = $xpath->query($query); 190 | 191 | foreach ($entries as $entry) { 192 | if ($entry->parentNode !== null) { 193 | $entry->parentNode->removeChild($entry); 194 | } 195 | } 196 | } 197 | 198 | //Remove width and height attributes if they provided in percent 199 | private function removeIncorrectDimensionAttributes() 200 | { 201 | $xpath = new \DOMXPath($this->doc); 202 | 203 | $entries = $xpath->query('//*[contains(@width, "%") or contains(@height, "%") or @width="auto" or @height="auto"]'); 204 | 205 | foreach ($entries as $entry) { 206 | if ($entry->hasAttribute('width')) { 207 | $entry->removeAttribute('width'); 208 | } 209 | 210 | if ($entry->hasAttribute('height')) { 211 | $entry->removeAttribute('height'); 212 | } 213 | } 214 | } 215 | 216 | //Remove prohibited attributes 217 | private function removeProhibitedAttributes() 218 | { 219 | $xpath = new \DOMXPath($this->doc); 220 | 221 | foreach ($this->prohibited_attributes as $attribute=>$tags) { 222 | $entries = $xpath->query('//' . $tags . '[@'.$attribute.']'); 223 | 224 | foreach ($entries as $entry) { 225 | $entry->removeAttribute($attribute); 226 | } 227 | } 228 | 229 | //Remove anchors with href="javascript:*" 230 | $invalid_a_tags = $xpath->query('//a[starts-with(@href, "javascript:")]'); 231 | 232 | foreach ($invalid_a_tags as $tag) { 233 | $tag->parentNode->removeChild($tag); 234 | } 235 | } 236 | 237 | 238 | public function getScripts() 239 | { 240 | return $this->scripts; 241 | } 242 | } 243 | -------------------------------------------------------------------------------- /src/TagConverter/AMPAudio.php: -------------------------------------------------------------------------------- 1 | '; 11 | 12 | public function convert(\DOMDocument $doc) 13 | { 14 | $query = '//audio'; 15 | 16 | $xpath = new \DOMXPath($doc); 17 | 18 | $entries = $xpath->query($query); 19 | 20 | if ($entries->length > 0) { 21 | $this->necessary_scripts[] = $this->extension_script; 22 | } 23 | 24 | $allowed_attributes = ['src', 'autoplay', 'controls', 'loop', 'crossorigin']; 25 | 26 | foreach ($entries as $tag) { 27 | $audio = $doc->createElement('amp-audio'); 28 | 29 | 30 | foreach ($allowed_attributes as $attribute) { 31 | if ($tag->hasAttribute($attribute)) { 32 | $audio->setAttribute($attribute, $tag->getAttribute($attribute)); 33 | } 34 | } 35 | 36 | if ($tag->hasChildNodes()) { 37 | foreach ($tag->childNodes as $node) { 38 | $audio->appendChild($node->cloneNode(true)); 39 | } 40 | } 41 | 42 | 43 | $tag->parentNode->replaceChild($audio, $tag); 44 | } 45 | 46 | return $doc; 47 | } 48 | 49 | public function getNecessaryScripts() 50 | { 51 | return $this->necessary_scripts; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/TagConverter/AMPDailymotion.php: -------------------------------------------------------------------------------- 1 | '; 11 | 12 | public function convert(\DOMDocument $doc) 13 | { 14 | $query = '//iframe[php:functionString(\'preg_match\', \'/dailymotion.com\/embed\/video\/([0-9a-zA-Z]+)/i\', @src) > 0]'; 15 | 16 | $xpath = new \DOMXPath($doc); 17 | 18 | $xpath->registerNamespace("php", "http://php.net/xpath"); 19 | $xpath->registerPhpFunctions('preg_match'); 20 | 21 | $entries = $xpath->query($query); 22 | 23 | if ($entries->length > 0) { 24 | $this->necessary_scripts[] = $this->extension_script; 25 | } 26 | 27 | foreach ($entries as $tag) { 28 | preg_match('/dailymotion.com\/embed\/video\/([0-9a-zA-Z]+)/i', $tag->getAttribute('src'), $id_match); 29 | $video_id = $id_match[1]; 30 | 31 | $dailymotion = $doc->createElement('amp-dailymotion'); 32 | 33 | $dailymotion->setAttribute('data-videoid', $video_id); 34 | $dailymotion->setAttribute('width', '480'); 35 | $dailymotion->setAttribute('height', '270'); 36 | $dailymotion->setAttribute('layout', 'responsive'); 37 | 38 | $tag->parentNode->replaceChild($dailymotion, $tag); 39 | } 40 | 41 | return $doc; 42 | } 43 | 44 | public function getNecessaryScripts() 45 | { 46 | return $this->necessary_scripts; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/TagConverter/AMPFacebook.php: -------------------------------------------------------------------------------- 1 | '; 11 | 12 | public function convert(\DOMDocument $doc) 13 | { 14 | $converted_doc = $this->convertScriptEmbeds($doc); 15 | $converted_doc = $this->convertIframeEmbeds($doc); 16 | 17 | return $converted_doc; 18 | } 19 | 20 | private function convertIframeEmbeds(\DOMDocument $doc) 21 | { 22 | $query = '//iframe[php:functionString(\'preg_match\', \'/\/plugins\/(video|post).php\?href=([^&]*)/i\', @src) > 0]'; 23 | 24 | $xpath = new \DOMXPath($doc); 25 | 26 | $xpath->registerNamespace("php", "http://php.net/xpath"); 27 | $xpath->registerPhpFunctions('preg_match'); 28 | 29 | $entries = $xpath->query($query); 30 | 31 | if ($entries->length > 0 && !$this->necessary_scripts) { 32 | $this->necessary_scripts[] = $this->extension_script; 33 | } 34 | 35 | foreach ($entries as $tag) { 36 | preg_match('/\/plugins\/(video|post).php\?href=([^&]*)/i', $tag->getAttribute('src'), $id_match); 37 | 38 | $type = $id_match[1]; 39 | $post_url = urldecode($id_match[2]); 40 | 41 | $fb_embed = $doc->createElement('amp-facebook'); 42 | 43 | if ($type == 'video') { 44 | $embed_type = 'video'; 45 | } else { 46 | $embed_type = 'post'; 47 | } 48 | 49 | $fb_embed->setAttribute('width', '552'); 50 | $fb_embed->setAttribute('height', '310'); 51 | $fb_embed->setAttribute('layout', 'responsive'); 52 | 53 | $fb_embed->setAttribute('data-href', $post_url); 54 | $fb_embed->setAttribute('data-embed-as', $embed_type); 55 | 56 | $tag->parentNode->replaceChild($fb_embed, $tag); 57 | } 58 | 59 | return $doc; 60 | } 61 | 62 | private function convertScriptEmbeds(\DOMDocument $doc) 63 | { 64 | $query = '//div[(@class="fb-post" or @class="fb-video" or @class="fb-comment-embed") and @data-href]'; 65 | 66 | $xpath = new \DOMXPath($doc); 67 | 68 | $entries = $xpath->query($query); 69 | 70 | if ($entries->length > 0 && !$this->necessary_scripts) { 71 | $this->necessary_scripts[] = $this->extension_script; 72 | } 73 | 74 | foreach ($entries as $tag) { 75 | if ($tag->hasAttribute('data-href')) { 76 | $fb_embed = $doc->createElement('amp-facebook'); 77 | 78 | if ($tag->getAttribute('class') == 'fb-video') { 79 | $embed_type = 'video'; 80 | } elseif ($tag->getAttribute('class') == 'fb-comment-embed') { 81 | $embed_type = 'comment'; 82 | } else { 83 | $embed_type = 'post'; 84 | } 85 | 86 | $fb_embed->setAttribute('width', '552'); 87 | $fb_embed->setAttribute('height', '310'); 88 | $fb_embed->setAttribute('layout', 'responsive'); 89 | 90 | $fb_embed->setAttribute('data-href', $tag->getAttribute('data-href')); 91 | $fb_embed->setAttribute('data-embed-as', $embed_type); 92 | 93 | $tag->parentNode->replaceChild($fb_embed, $tag); 94 | } else { 95 | $tag->parentNode->removeChild($tag); 96 | } 97 | } 98 | 99 | return $doc; 100 | } 101 | 102 | public function getNecessaryScripts() 103 | { 104 | return $this->necessary_scripts; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/TagConverter/AMPGfycat.php: -------------------------------------------------------------------------------- 1 | '; 11 | 12 | public function convert(\DOMDocument $doc) 13 | { 14 | $query = '//iframe[php:functionString(\'preg_match\', \'/gfycat.com\/ifr\/([0-9a-zA-Z]+)/i\', @src) > 0]'; 15 | 16 | $xpath = new \DOMXPath($doc); 17 | 18 | $xpath->registerNamespace("php", "http://php.net/xpath"); 19 | $xpath->registerPhpFunctions('preg_match'); 20 | 21 | $entries = $xpath->query($query); 22 | 23 | if ($entries->length > 0) { 24 | $this->necessary_scripts[] = $this->extension_script; 25 | } 26 | 27 | foreach ($entries as $tag) { 28 | preg_match('/gfycat.com\/ifr\/([0-9a-zA-Z]+)/i', $tag->getAttribute('src'), $id_match); 29 | $gfy_id = $id_match[1]; 30 | 31 | $gfycat = $doc->createElement('amp-gfycat'); 32 | 33 | $gfycat->setAttribute('data-gfyid', $gfy_id); 34 | $gfycat->setAttribute('width', '640'); 35 | $gfycat->setAttribute('height', '360'); 36 | $gfycat->setAttribute('layout', 'responsive'); 37 | 38 | $tag->parentNode->replaceChild($gfycat, $tag); 39 | } 40 | 41 | return $doc; 42 | } 43 | 44 | public function getNecessaryScripts() 45 | { 46 | return $this->necessary_scripts; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/TagConverter/AMPGist.php: -------------------------------------------------------------------------------- 1 | '; 12 | 13 | public function convert(\DOMDocument $doc) 14 | { 15 | 16 | // /https?\:\/\/gist.github.com\/.*\/([0-9a-z]+)\.js/i 17 | $query = '//script[php:functionString(\'preg_match\', \'/https?\:\/\/gist.github.com\/.*\/([0-9a-z]+)\.js/i\', @src) > 0]'; 18 | 19 | $xpath = new \DOMXPath($doc); 20 | 21 | $xpath->registerNamespace("php", "http://php.net/xpath"); 22 | $xpath->registerPhpFunctions('preg_match'); 23 | 24 | $entries = $xpath->query($query); 25 | 26 | if ($entries->length > 0) { 27 | $this->necessary_scripts[] = $this->extension_script; 28 | } 29 | 30 | foreach ($entries as $tag) { 31 | preg_match('/https?\:\/\/gist.github.com\/.*\/([0-9a-z]+)\.js/i', $tag->getAttribute('src'), $id_match); 32 | $gist_id = $id_match[1]; 33 | 34 | $gist = $doc->createElement('amp-gist'); 35 | 36 | $gist->setAttribute('data-gistid', $gist_id); 37 | $gist->setAttribute('layout', 'fixed-height'); 38 | $gist->setAttribute('height', '250'); 39 | 40 | $tag->parentNode->replaceChild($gist, $tag); 41 | } 42 | 43 | return $doc; 44 | } 45 | 46 | public function getNecessaryScripts() 47 | { 48 | return $this->necessary_scripts; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/TagConverter/AMPIframe.php: -------------------------------------------------------------------------------- 1 | '; 11 | 12 | public function convert(\DOMDocument $doc) 13 | { 14 | $query = '//iframe'; 15 | 16 | $xpath = new \DOMXPath($doc); 17 | 18 | $entries = $xpath->query($query); 19 | 20 | if ($entries->length > 0) { 21 | $this->necessary_scripts[] = $this->extension_script; 22 | } 23 | 24 | foreach ($entries as $tag) { 25 | if ($tag->hasAttribute('width') && $tag->hasAttribute('height')) { 26 | $width = $tag->getAttribute('width'); 27 | $height = $tag->getAttribute('height'); 28 | } else { 29 | $width = '600'; 30 | $height = '450'; 31 | } 32 | 33 | if ($tag->getAttribute('sandbox')) { 34 | $sandbox = $tag->getAttribute('sandbox'); 35 | } else { 36 | $sandbox = 'allow-scripts allow-same-origin allow-popups'; 37 | } 38 | 39 | $src = $tag->getAttribute('src'); 40 | 41 | $ampiframe = $doc->createElement('amp-iframe'); 42 | 43 | $ampiframe->setAttribute('src', $src); 44 | $ampiframe->setAttribute('sandbox', $sandbox); 45 | $ampiframe->setAttribute('width', $width); 46 | $ampiframe->setAttribute('height', $height); 47 | 48 | if ($tag->hasAttribute('allowfullscreen')) { 49 | $ampiframe->setAttribute('allowfullscreen', ''); 50 | } 51 | 52 | $ampiframe->setAttribute('layout', 'responsive'); 53 | 54 | //Add a placeholder to the amp-iframe element so it can be embedded even above the fold. 55 | $placeholder = $doc->createElement('p'); 56 | $placeholder->setAttribute('placeholder', ''); 57 | $placeholder_text = $doc->createTextNode('Loading...'); 58 | $placeholder->appendChild($placeholder_text); 59 | $ampiframe->appendChild($placeholder); 60 | 61 | $tag->parentNode->replaceChild($ampiframe, $tag); 62 | } 63 | 64 | return $doc; 65 | } 66 | 67 | public function getNecessaryScripts() 68 | { 69 | return $this->necessary_scripts; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/TagConverter/AMPImg.php: -------------------------------------------------------------------------------- 1 | base_url = $base_url; 22 | $this->timeout = $timeout; 23 | } 24 | 25 | public function convert(\DOMDocument $doc) 26 | { 27 | 28 | $query = '//img'; 29 | 30 | $xpath = new \DOMXPath($doc); 31 | 32 | $entries = $xpath->query($query); 33 | 34 | $allowed_attributes = ['src', 'width', 'height', 'alt', 'srcset', 'sizes']; 35 | 36 | foreach ($entries as $tag) { 37 | if ($tag->hasAttribute('src')) { 38 | $img = $doc->createElement('amp-img'); 39 | 40 | if (!$tag->hasAttribute('width') || !$tag->hasAttribute('height')) { 41 | $imageSize = ImageSize::getImageSize( 42 | $this->base_url, 43 | $tag->getAttribute('src'), 44 | $this->timeout 45 | ); 46 | 47 | if ($imageSize) { 48 | $img->setAttribute('width', $imageSize['width']); 49 | $img->setAttribute('height', $imageSize['height']); 50 | } else { 51 | $tag->parentNode->removeChild($tag); 52 | continue; 53 | } 54 | } 55 | 56 | foreach ($allowed_attributes as $attribute) { 57 | if ($tag->hasAttribute($attribute)) { 58 | $img->setAttribute($attribute, $tag->getAttribute($attribute)); 59 | } 60 | } 61 | 62 | $img->setAttribute('layout', 'responsive'); 63 | 64 | $tag->parentNode->replaceChild($img, $tag); 65 | } else { 66 | $tag->parentNode->removeChild($tag); 67 | } 68 | } 69 | 70 | return $doc; 71 | } 72 | 73 | public function getNecessaryScripts() 74 | { 75 | return $this->necessary_scripts; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/TagConverter/AMPImgLightbox.php: -------------------------------------------------------------------------------- 1 | '; 14 | 15 | 16 | //Base URL of the images 17 | private $base_url; 18 | 19 | //Time limit for acquiring image dimensions in seconds 20 | private $timeout; 21 | 22 | public function __construct($base_url = '', $timeout = 10) 23 | { 24 | $this->base_url = $base_url; 25 | $this->timeout = $timeout; 26 | } 27 | 28 | public function convert(\DOMDocument $doc) 29 | { 30 | $query = '//img'; 31 | 32 | $xpath = new \DOMXPath($doc); 33 | 34 | $entries = $xpath->query($query); 35 | 36 | if ($entries->length > 0) { 37 | $this->necessary_scripts[] = $this->extension_script; 38 | } 39 | 40 | $allowed_attributes = ['src', 'width', 'height', 'alt', 'srcset', 'sizes']; 41 | 42 | foreach ($entries as $tag) { 43 | if ($tag->hasAttribute('src')) { 44 | $img = $doc->createElement('amp-img'); 45 | 46 | if (!$tag->hasAttribute('width') || !$tag->hasAttribute('height')) { 47 | $imageSize = ImageSize::getImageSize( 48 | $this->base_url, 49 | $tag->getAttribute('src'), 50 | $this->timeout 51 | ); 52 | 53 | if ($imageSize) { 54 | $img->setAttribute('width', $imageSize['width']); 55 | $img->setAttribute('height', $imageSize['height']); 56 | } else { 57 | $tag->parentNode->removeChild($tag); 58 | continue; 59 | } 60 | } 61 | 62 | foreach ($allowed_attributes as $attribute) { 63 | if ($tag->hasAttribute($attribute)) { 64 | $img->setAttribute($attribute, $tag->getAttribute($attribute)); 65 | } 66 | } 67 | 68 | $img->setAttribute('layout', 'responsive'); 69 | $img->setAttribute('lightbox', ''); 70 | 71 | $tag->parentNode->replaceChild($img, $tag); 72 | } else { 73 | $tag->parentNode->removeChild($tag); 74 | } 75 | } 76 | 77 | return $doc; 78 | } 79 | 80 | public function getNecessaryScripts() 81 | { 82 | return $this->necessary_scripts; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/TagConverter/AMPImgZoom.php: -------------------------------------------------------------------------------- 1 | '; 12 | 13 | //Base URL of the images 14 | private $base_url; 15 | 16 | //Time limit for acquiring image dimensions in seconds 17 | private $timeout; 18 | 19 | public function __construct($base_url = '', $timeout = 10) 20 | { 21 | $this->base_url = $base_url; 22 | $this->timeout = $timeout; 23 | } 24 | 25 | public function convert(\DOMDocument $doc) 26 | { 27 | $query = '//img'; 28 | 29 | $xpath = new \DOMXPath($doc); 30 | 31 | $entries = $xpath->query($query); 32 | 33 | $allowed_attributes = ['src', 'alt', 'srcset', 'sizes']; 34 | 35 | foreach ($entries as $tag) { 36 | if ($tag->hasAttribute('src')) { 37 | if (!$tag->hasAttribute('width') || !$tag->hasAttribute('height')) { 38 | 39 | $imageSize = ImageSize::getImageSize( 40 | $this->base_url, 41 | $tag->getAttribute('src'), 42 | $this->timeout 43 | ); 44 | 45 | if ($imageSize) { 46 | $width = $imageSize['width']; 47 | $height = $imageSize['height']; 48 | } else { 49 | $tag->parentNode->removeChild($tag); 50 | continue; 51 | } 52 | } else { 53 | $width = $tag->getAttribute('width'); 54 | $height = $tag->getAttribute('height'); 55 | } 56 | 57 | if (!$this->necessary_scripts) { 58 | $this->necessary_scripts[] = $this->extension_script; 59 | } 60 | 61 | $img = $doc->createElement('amp-img'); 62 | 63 | $panzoom = $doc->createElement('amp-pan-zoom'); 64 | 65 | foreach ($allowed_attributes as $attribute) { 66 | if ($tag->hasAttribute($attribute)) { 67 | $img->setAttribute($attribute, $tag->getAttribute($attribute)); 68 | } 69 | } 70 | 71 | $img->setAttribute('layout', 'fill'); 72 | 73 | $panzoom->setAttribute('width', $width); 74 | $panzoom->setAttribute('height', $height); 75 | $panzoom->setAttribute('layout', 'responsive'); 76 | 77 | $panzoom->appendChild($img); 78 | 79 | $tag->parentNode->replaceChild($panzoom, $tag); 80 | } else { 81 | $tag->parentNode->removeChild($tag); 82 | } 83 | } 84 | 85 | return $doc; 86 | } 87 | 88 | public function getNecessaryScripts() 89 | { 90 | return $this->necessary_scripts; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/TagConverter/AMPImgur.php: -------------------------------------------------------------------------------- 1 | '; 11 | 12 | public function convert(\DOMDocument $doc) 13 | { 14 | //
can't see? no problem
15 | $query = '//blockquote[@class="imgur-embed-pub" and @data-id]'; 16 | 17 | $xpath = new \DOMXPath($doc); 18 | 19 | $entries = $xpath->query($query); 20 | 21 | if ($entries->length > 0) { 22 | $this->necessary_scripts[] = $this->extension_script; 23 | } 24 | 25 | foreach ($entries as $tag) { 26 | if ($tag->hasAttribute('data-id')) { 27 | $imgur = $doc->createElement('amp-imgur'); 28 | 29 | $imgur->setAttribute('data-imgur-id', $tag->getAttribute('data-id')); 30 | 31 | $imgur->setAttribute('width', '540'); 32 | $imgur->setAttribute('height', '670'); 33 | $imgur->setAttribute('layout', 'responsive'); 34 | 35 | 36 | $tag->parentNode->replaceChild($imgur, $tag); 37 | } else { 38 | $tag->parentNode->removeChild($tag); 39 | } 40 | } 41 | 42 | return $doc; 43 | } 44 | 45 | public function getNecessaryScripts() 46 | { 47 | return $this->necessary_scripts; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/TagConverter/AMPInstagram.php: -------------------------------------------------------------------------------- 1 | '; 12 | 13 | public function convert(\DOMDocument $doc) 14 | { 15 | 16 | // /instagram.com\/(p|tv|reel)\/([0-9a-zA-Z_\-]+)/i 17 | $query = '//blockquote[@class="instagram-media" and php:functionString(\'preg_match\', \'/instagram.com\/(p|tv|reel)\/([0-9a-zA-Z_\-]+)/i\', @data-instgrm-permalink) > 0]'; 18 | 19 | $xpath = new \DOMXPath($doc); 20 | 21 | $xpath->registerNamespace("php", "http://php.net/xpath"); 22 | $xpath->registerPhpFunctions('preg_match'); 23 | 24 | $entries = $xpath->query($query); 25 | 26 | if ($entries->length > 0) { 27 | $this->necessary_scripts[] = $this->extension_script; 28 | } 29 | 30 | foreach ($entries as $tag) { 31 | preg_match('/instagram.com\/(p|tv|reel)\/([0-9a-zA-Z_\-]+)/i', $tag->getAttribute('data-instgrm-permalink'), $id_match); 32 | $post_id = $id_match[2]; 33 | 34 | $ampinstagram = $doc->createElement('amp-instagram'); 35 | 36 | $ampinstagram->setAttribute('data-shortcode', $post_id); 37 | $ampinstagram->setAttribute('data-captioned', ''); 38 | $ampinstagram->setAttribute('width', '400'); 39 | $ampinstagram->setAttribute('height', '400'); 40 | $ampinstagram->setAttribute('layout', 'responsive'); 41 | 42 | $tag->parentNode->replaceChild($ampinstagram, $tag); 43 | } 44 | 45 | return $doc; 46 | } 47 | 48 | public function getNecessaryScripts() 49 | { 50 | return $this->necessary_scripts; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/TagConverter/AMPPinterest.php: -------------------------------------------------------------------------------- 1 | '; 11 | 12 | public function convert(\DOMDocument $doc) 13 | { 14 | $query = '//a[@data-pin-do="embedPin" and @href]'; 15 | 16 | $xpath = new \DOMXPath($doc); 17 | 18 | $entries = $xpath->query($query); 19 | 20 | if ($entries->length > 0) { 21 | $this->necessary_scripts[] = $this->extension_script; 22 | } 23 | 24 | foreach ($entries as $tag) { 25 | if ($tag->hasAttribute('href')) { 26 | $pin_embed = $doc->createElement('amp-pinterest'); 27 | 28 | $pin_embed->setAttribute('width', '552'); 29 | $pin_embed->setAttribute('height', '310'); 30 | $pin_embed->setAttribute('layout', 'responsive'); 31 | 32 | $pin_embed->setAttribute('data-url', $tag->getAttribute('href')); 33 | $pin_embed->setAttribute('data-do', 'embedPin'); 34 | 35 | $tag->parentNode->replaceChild($pin_embed, $tag); 36 | } else { 37 | $tag->parentNode->removeChild($tag); 38 | } 39 | } 40 | 41 | return $doc; 42 | } 43 | 44 | public function getNecessaryScripts() 45 | { 46 | return $this->necessary_scripts; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/TagConverter/AMPPlaybuzz.php: -------------------------------------------------------------------------------- 1 | '; 11 | 12 | public function convert(\DOMDocument $doc) 13 | { 14 | $query = '//div[@class="playbuzz" and @data-id]'; 15 | 16 | $xpath = new \DOMXPath($doc); 17 | 18 | $entries = $xpath->query($query); 19 | 20 | if ($entries->length > 0) { 21 | $this->necessary_scripts[] = $this->extension_script; 22 | } 23 | 24 | foreach ($entries as $tag) { 25 | if ($tag->hasAttribute('data-id')) { 26 | $playbuzz = $doc->createElement('amp-playbuzz'); 27 | 28 | $playbuzz->setAttribute('height', '700'); 29 | 30 | $playbuzz->setAttribute('data-item', $tag->getAttribute('data-id')); 31 | $playbuzz->setAttribute('data-share-buttons', 'true'); 32 | 33 | $tag->parentNode->replaceChild($playbuzz, $tag); 34 | } else { 35 | $tag->parentNode->removeChild($tag); 36 | } 37 | } 38 | 39 | return $doc; 40 | } 41 | 42 | public function getNecessaryScripts() 43 | { 44 | return $this->necessary_scripts; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/TagConverter/AMPTwitter.php: -------------------------------------------------------------------------------- 1 | '; 12 | 13 | public function convert(\DOMDocument $doc) 14 | { 15 | 16 | // /https?\:\/\/twitter.com\/.*\/status\/([0-9a-z]+)/i 17 | $query = '//blockquote[@class="twitter-tweet"]/a[php:functionString(\'preg_match\', \'/https?\:\/\/twitter.com\/.*\/status\/([0-9a-z]+)/i\', @href) > 0]'; 18 | 19 | $xpath = new \DOMXPath($doc); 20 | 21 | $xpath->registerNamespace("php", "http://php.net/xpath"); 22 | $xpath->registerPhpFunctions('preg_match'); 23 | 24 | $entries = $xpath->query($query); 25 | 26 | if ($entries->length > 0) { 27 | $this->necessary_scripts[] = $this->extension_script; 28 | } 29 | 30 | foreach ($entries as $tag) { 31 | preg_match('/https?\:\/\/twitter.com\/.*\/status\/([0-9a-z]+)/i', $tag->getAttribute('href'), $id_match); 32 | $post_id = $id_match[1]; 33 | 34 | $amptwitter = $doc->createElement('amp-twitter'); 35 | 36 | 37 | $amptwitter->setAttribute('width', '375'); 38 | $amptwitter->setAttribute('height', '472'); 39 | $amptwitter->setAttribute('layout', 'responsive'); 40 | $amptwitter->setAttribute('data-tweetid', $post_id); 41 | 42 | $tag->parentNode->parentNode->replaceChild($amptwitter, $tag->parentNode); 43 | } 44 | 45 | return $doc; 46 | } 47 | 48 | public function getNecessaryScripts() 49 | { 50 | return $this->necessary_scripts; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/TagConverter/AMPVideo.php: -------------------------------------------------------------------------------- 1 | '; 11 | 12 | public function convert(\DOMDocument $doc) 13 | { 14 | $query = '//video'; 15 | 16 | $xpath = new \DOMXPath($doc); 17 | 18 | $entries = $xpath->query($query); 19 | 20 | if ($entries->length > 0) { 21 | $this->necessary_scripts[] = $this->extension_script; 22 | } 23 | 24 | $allowed_attributes = ['src', 'poster', 'autoplay', 'controls', 'loop', 'crossorigin']; 25 | 26 | foreach ($entries as $tag) { 27 | if ($tag->hasAttribute('width') && $tag->hasAttribute('height')) { 28 | $width = $tag->getAttribute('width'); 29 | $height = $tag->getAttribute('height'); 30 | 31 | 32 | $video = $doc->createElement('amp-video'); 33 | 34 | $video->setAttribute('width', $width); 35 | $video->setAttribute('height', $height); 36 | $video->setAttribute('layout', 'responsive'); 37 | 38 | 39 | foreach ($allowed_attributes as $attribute) { 40 | if ($tag->hasAttribute($attribute)) { 41 | $video->setAttribute($attribute, $tag->getAttribute($attribute)); 42 | } 43 | } 44 | 45 | if ($tag->hasChildNodes()) { 46 | foreach ($tag->childNodes as $node) { 47 | $video->appendChild($node->cloneNode(true)); 48 | } 49 | } 50 | 51 | 52 | $tag->parentNode->replaceChild($video, $tag); 53 | } else { 54 | $tag->parentNode->removeChild($tag); 55 | } 56 | } 57 | 58 | return $doc; 59 | } 60 | 61 | public function getNecessaryScripts() 62 | { 63 | return $this->necessary_scripts; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/TagConverter/AMPVimeo.php: -------------------------------------------------------------------------------- 1 | '; 11 | 12 | public function convert(\DOMDocument $doc) 13 | { 14 | $query = '//iframe[php:functionString(\'preg_match\', \'/https?:\/\/player.vimeo.com\/video\/([0-9]+)/i\', @src) > 0]'; 15 | 16 | $xpath = new \DOMXPath($doc); 17 | 18 | $xpath->registerNamespace("php", "http://php.net/xpath"); 19 | $xpath->registerPhpFunctions('preg_match'); 20 | 21 | $entries = $xpath->query($query); 22 | 23 | if ($entries->length > 0) { 24 | $this->necessary_scripts[] = $this->extension_script; 25 | } 26 | 27 | foreach ($entries as $tag) { 28 | preg_match('/https?:\/\/player.vimeo.com\/video\/([0-9]+)/i', $tag->getAttribute('src'), $id_match); 29 | $video_id = $id_match[1]; 30 | 31 | $vimeo = $doc->createElement('amp-vimeo'); 32 | 33 | $vimeo->setAttribute('data-videoid', $video_id); 34 | $vimeo->setAttribute('width', '500'); 35 | $vimeo->setAttribute('height', '281'); 36 | $vimeo->setAttribute('layout', 'responsive'); 37 | 38 | $tag->parentNode->replaceChild($vimeo, $tag); 39 | } 40 | 41 | return $doc; 42 | } 43 | 44 | public function getNecessaryScripts() 45 | { 46 | return $this->necessary_scripts; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/TagConverter/AMPVk.php: -------------------------------------------------------------------------------- 1 | '; 16 | 17 | public function convert(\DOMDocument $doc) 18 | { 19 | $query = '//script[contains(., "VK.Widgets.Post")]'; 20 | $expression = '/VK.Widgets.Post[\n\r\s]*\([\n\r\s]*["|\'](.*)["|\'][\n\r\s]*,[\n\r\s]*([0-9]+)[\n\r\s]*,[\n\r\s]*([0-9]+)[\n\r\s]*,[\n\r\s]*["|\']([0-9a-zA-Z]+)["|\']/'; 21 | 22 | 23 | $xpath = new \DOMXPath($doc); 24 | 25 | $xpath->registerNamespace("php", "http://php.net/xpath"); 26 | $xpath->registerPhpFunctions('preg_match'); 27 | 28 | $entries = $xpath->query($query); 29 | 30 | foreach ($entries as $tag) { 31 | if (preg_match($expression, $tag->nodeValue, $data)) { 32 | $tag_id = $data[1]; 33 | $owner_id = $data[2]; 34 | $post_id = $data[3]; 35 | $hash = $data[4]; 36 | 37 | $embed_container = $xpath->query('div[@id="'.$tag_id.'"]'); 38 | 39 | if ($embed_container->length > 0) { 40 | $vk_container = $embed_container[0]; 41 | 42 | $ampvk = $doc->createElement('amp-vk'); 43 | $ampvk->setAttribute('data-embedtype', 'post'); 44 | $ampvk->setAttribute('data-owner-id', $owner_id); 45 | $ampvk->setAttribute('data-post-id', $post_id); 46 | $ampvk->setAttribute('data-hash', $hash); 47 | $ampvk->setAttribute('width', '500'); 48 | $ampvk->setAttribute('height', '300'); 49 | $ampvk->setAttribute('layout', 'responsive'); 50 | 51 | $vk_container->parentNode->replaceChild($ampvk, $vk_container); 52 | 53 | if (!$this->necessary_scripts) { 54 | $this->necessary_scripts[] = $this->extension_script; 55 | } 56 | } 57 | } 58 | } 59 | 60 | return $doc; 61 | } 62 | 63 | public function getNecessaryScripts() 64 | { 65 | return $this->necessary_scripts; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/TagConverter/AMPYoutube.php: -------------------------------------------------------------------------------- 1 | '; 11 | 12 | public function convert(\DOMDocument $doc) 13 | { 14 | $query = '//iframe[php:functionString(\'preg_match\', \'/youtube\.com\/(?:v|embed)\/([a-zA-z0-9_-]+)/i\', @src) > 0]'; 15 | 16 | $xpath = new \DOMXPath($doc); 17 | 18 | $xpath->registerNamespace("php", "http://php.net/xpath"); 19 | $xpath->registerPhpFunctions('preg_match'); 20 | 21 | $entries = $xpath->query($query); 22 | 23 | if ($entries->length > 0) { 24 | $this->necessary_scripts[] = $this->extension_script; 25 | } 26 | 27 | foreach ($entries as $tag) { 28 | preg_match('/youtube\.com\/(?:v|embed)\/([a-zA-z0-9_-]+)/i', $tag->getAttribute('src'), $id_match); 29 | $video_id = $id_match[1]; 30 | 31 | $youtube_video = $doc->createElement('amp-youtube'); 32 | 33 | $youtube_video->setAttribute('data-videoid', $video_id); 34 | $youtube_video->setAttribute('width', '480'); 35 | $youtube_video->setAttribute('height', '270'); 36 | $youtube_video->setAttribute('layout', 'responsive'); 37 | 38 | $tag->parentNode->replaceChild($youtube_video, $tag); 39 | } 40 | 41 | return $doc; 42 | } 43 | 44 | public function getNecessaryScripts() 45 | { 46 | return $this->necessary_scripts; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/TagConverterInterface.php: -------------------------------------------------------------------------------- 1 | setTimeout($timeout); 10 | 11 | if (preg_match('/https?:\/\//i', $url)) { 12 | $img_url = $url; 13 | } else { 14 | if (substr($base_url, -1) == '/') { 15 | $base_url = substr($base_url, 0, -1); 16 | } 17 | 18 | if (substr($url, 0, 1) == '/') { 19 | $url = substr($url, 1); 20 | } 21 | 22 | $img_url = $base_url . '/' . $url; 23 | } 24 | 25 | $imageSize = $fasterImageSize->batch([ 26 | $img_url 27 | ]); 28 | 29 | if ($imageSize[$img_url]['size'] == 'failed') { 30 | return null; 31 | } 32 | 33 | list($width, $height) = $imageSize[$img_url]['size']; 34 | return [ 35 | 'width' => $width, 36 | 'height' => $height, 37 | 'type' => $imageSize[$img_url]['type'] 38 | ]; 39 | 40 | } 41 | } 42 | --------------------------------------------------------------------------------