All Projects → magyarandras → amp-converter

magyarandras / amp-converter

Licence: MIT license
A PHP library to convert HTML articles, blog posts or similar content to AMP (Accelerated Mobile Pages).

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to amp-converter

amp-react-renderer-plugin
⚡Plugin makes it painless to create React component based AMP page⚡
Stars: ✭ 29 (-50.85%)
Mutual labels:  amp, amp-html
ampjucks
Boilerplate and base project to create static websites with AMP, Nunjucks and Gulp
Stars: ✭ 18 (-69.49%)
Mutual labels:  amp, amp-html
amp-surface
⚡ AMP Surface CSS Framework
Stars: ✭ 26 (-55.93%)
Mutual labels:  amp, amp-html
amp-browser
AMP Browser
Stars: ✭ 39 (-33.9%)
Mutual labels:  amp, amp-html
amp-mui
⚡ AMP MUI CSS Framework
Stars: ✭ 18 (-69.49%)
Mutual labels:  amp, amp-html
wordpress-amp-theme
A free WordPress theme for blogging built entirely on Google AMP
Stars: ✭ 22 (-62.71%)
Mutual labels:  amp, amp-html
amp-renderer
Server-side rendering for AMP in Python
Stars: ✭ 14 (-76.27%)
Mutual labels:  amp, amp-html
react-amp-components
A (hopefully) simple way to render AMP components via React on the server.
Stars: ✭ 30 (-49.15%)
Mutual labels:  amp, amp-html
cdn
🚀 ✈️ 🚄 free CDN for everyone who wants to speed his website freely!😄
Stars: ✭ 16 (-72.88%)
Mutual labels:  amp
amp-bootstrap-example
Example workflow for using Bootstrap with AMP
Stars: ✭ 75 (+27.12%)
Mutual labels:  amp-html
youcount.github.io
YouCount allows you to see the subscriber count of any YouTube user in realtime. It has been designed using the Material Design language and is optimized for mobile devices and older browsers.
Stars: ✭ 27 (-54.24%)
Mutual labels:  amp
resiliency
A modern PHP library that allows you to make resilient calls to external services 🔁
Stars: ✭ 79 (+33.9%)
Mutual labels:  php8
deepvac
PyTorch Project Specification.
Stars: ✭ 507 (+759.32%)
Mutual labels:  amp
dnmp
docker-compose部署LNMP环境 Nginx/Openresty、MySQL(5.7、8.0、8.1)、PHP7.4(8.0、5.6)、Redis5.0、PHPMyAdmin、Xdebug、RabbitMQ、Nacos
Stars: ✭ 138 (+133.9%)
Mutual labels:  php8
AMP-dockerized
CubeCoders AMP in a Docker Image. Easily create game servers for games like Minecraft, GMod, TF2, Factorio, and StarBound!
Stars: ✭ 54 (-8.47%)
Mutual labels:  amp
gdgpisa.github.io
Public website on github-pages for GDG Pisa 🌎🇮🇹💻
Stars: ✭ 14 (-76.27%)
Mutual labels:  amp
schema-and-structured-data-for-wp
Creating the best Structured Data and Schema plugin for WordPress
Stars: ✭ 66 (+11.86%)
Mutual labels:  amp
Stratum-Panel
Stratum aims to be a scalable, reliable panel better than most solutions on the market made for selling virtual machines. It works by utilizing Proxmox's API to automatically manage/deploy virtual machines. Stratum is made in Laravel.
Stars: ✭ 26 (-55.93%)
Mutual labels:  php8
docker-lemp
A single container LEMP complete fullstack with latest release of PHP7.4.33, 8.0.26 & 8.1.13/8.2RC and MySQL, nginx, PostgreSQL, phalcon, swoole, mailcatcher, beanstalkd, elasticsearch, memcached, redis, adminer and all you ever need; on top alpine3.15
Stars: ✭ 106 (+79.66%)
Mutual labels:  php8
darknet
php ffi darknet
Stars: ✭ 21 (-64.41%)
Mutual labels:  php8

AMP converter library

Build Status

A library to convert HTML articles, blog posts or similar content to AMP (Accelerated Mobile Pages).

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

Installation:

composer require magyarandras/amp-converter

Currently supported elments:

  • amp-img
  • amp-img in amp-pan-zoom
  • amp-img with lightbox
  • amp-video
  • amp-audio
  • amp-iframe(A placeholder is automatically added to the iframes, so you can embed iframes even above the fold.)
  • amp-youtube
  • amp-facebook
  • amp-instagram
  • amp-twitter
  • amp-pinterest
  • amp-playbuzz
  • amp-gist(Github gist embed)
  • amp-vimeo
  • amp-soundcloud(You can use amp-iframe instead)
  • amp-vk
  • amp-imgur
  • amp-dailymotion
  • amp-gfycat

Usage:

Simple example:

Make sure your HTML code doesn't contain tags or attributes invalid in HTML5 otherwise, the generated AMP will be invalid too.

<?php

use magyarandras\AMPConverter\Converter;

/*
If you have images with unknown dimension in your HTML code and you use relative URLs, you have to pass the base URL of the images to the constructor.

Examples:
$converter = new Converter('https://example.com');
$converter = new Converter('https://cdn.example.com');

*/
$converter = new Converter();

//Load built-in converters
$converter->loadDefaultConverters();

//HTML to convert
$html = '
<p><strong>This is a sample HTML code generated by TinyMCE.</strong></p>
<p><strong><img src="https://cdn.example.com/photo/example_960_720.jpg" alt="" width="960" height="640" /></strong></p>
<p><span style="font-family: \'times new roman\', times;">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.</span></p>
<p style="text-align: center;"><iframe src="https://www.youtube.com/embed/gYJ03GyrSrM" width="560" height="314" allowfullscreen="allowfullscreen"></iframe></p>
<p style="text-align: center;">&nbsp;</p>
<blockquote class="twitter-tweet">
<p dir="ltr" lang="en">Check out the latest post on the AMP Blog to learn how <a href="https://twitter.com/AdobeExpCloud?ref_src=twsrc%5Etfw">@AdobeExpCloud</a> has been working to seamless integrate AMP support into its applications ⚡<br /><br />Learn more here 👉 <a href="https://t.co/hX3QmJ707x">https://t.co/hX3QmJ707x</a></p>
&mdash; AMP Project (@AMPhtml) <a href="https://twitter.com/AMPhtml/status/1248666798901194753?ref_src=twsrc%5Etfw">April 10, 2020</a></blockquote>
';

//Convert html to amp html
$amphtml = $converter->convert($html);

//Get the necessary amp components
$amp_scripts = $converter->getScripts();

print_r($amphtml);
echo PHP_EOL;
print_r($amp_scripts);

The output:

<p><strong>This is a sample HTML code generated by TinyMCE.</strong></p>
<p><strong><amp-img src="https://cdn.example.com/photo/example_960_720.jpg" width="960" height="640" alt layout="responsive"></amp-img></strong></p>
<p><span>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.</span></p>
<p><amp-youtube data-videoid="gYJ03GyrSrM" width="480" height="270" layout="responsive"></amp-youtube></p>
<p>&nbsp;</p>
<amp-twitter width="375" height="472" layout="responsive" data-tweetid="1248666798901194753"></amp-twitter>
Array
(
    [0] => <script async custom-element="amp-youtube" src="https://cdn.ampproject.org/v0/amp-youtube-0.1.js"></script>
    [1] => <script async custom-element="amp-twitter" src="https://cdn.ampproject.org/v0/amp-twitter-0.1.js"></script>
)

You can specify which converters to use by loading them manually:

<?php

use magyarandras\AMPConverter\Converter;

use magyarandras\AMPConverter\TagConverter\AMPImgZoom;
use magyarandras\AMPConverter\TagConverter\AMPYoutube;

//PHP 7+ syntax:
//use magyarandras\AMPConverter\TagConverter\{AMPImgZoom,AMPYoutube}

$converter = new Converter();

$converter->addConverter(new AMPImgZoom());
$converter->addConverter(new AMPYoutube());

$amphtml = $converter->convert($html);

$amp_scripts = $converter->getScripts();

print_r($amphtml);
print_r($amp_scripts);

Writing your own converters:

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).

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.

<!-- The following line should be replaced with amp-date-countdown component -->
<div data-countdown="2038/01/19"></div>

<!-- Countdown library: http://hilios.github.io/jQuery.countdown/examples/multiple-instances.html -->
<script>
$('[data-countdown]').each(function() {
  var $this = $(this), finalDate = $(this).data('countdown');
  $this.countdown(finalDate, function(event) {
    $this.html(event.strftime('%D days %H:%M:%S'));
  });
});
</script>

You can create a custom converter class that implements the TagConverterInterface.

<?php

class CountdownConverter implements \magyarandras\AMPConverter\TagConverterInterface
{

    private $necessary_scripts = [];

    private $extension_scripts = [
    '<script async custom-element="amp-date-countdown" src="https://cdn.ampproject.org/v0/amp-date-countdown-0.1.js"></script>',
    '<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>'
    ];

    public function convert(\DOMDocument $doc)
    {

        $query = '//div[@data-countdown]';

        $xpath = new \DOMXPath($doc);

        $entries = $xpath->query($query);

        if ($entries->length > 0) {
            $this->necessary_scripts = $this->extension_scripts;
        }

        foreach ($entries as $tag) {

            //Although in this example there isn't any validation, you definitely should check if the date is valid.
            $timestamp = strtotime($tag->getAttribute('data-countdown'));

            $countdown = $doc->createElement('amp-date-countdown');

            $countdown->setAttribute('timestamp-seconds', $timestamp);
            $countdown->setAttribute('layout', 'fixed-height');
            $countdown->setAttribute('height', '50');

            $template = $doc->createElement('template');
            $template->setAttribute('type', 'amp-mustache');

            $paragraph = $doc->createElement('p');
            $paragraph->setAttribute('class', 'p1');

            $text = $doc->createTextNode('{{d}} days, {{h}} hours, {{m}} minutes and {{s}} seconds');

            $paragraph->appendChild($text);
            
            $template->appendChild($paragraph);
            $countdown->appendChild($template);

            $tag->parentNode->replaceChild($countdown, $tag);
        }
                  


        return $doc;
    }

    public function getNecessaryScripts() 
    {
        return $this->necessary_scripts;
    }

}

Using the custom converter:

<?php

require_once 'vendor/autoload.php';
require_once 'CountdownConverter.php';

use magyarandras\AMPConverter\Converter;

$converter = new Converter();

//Load built-in converters
$converter->loadDefaultConverters();

$converter->addConverter(new CountdownConverter());

$html = '<div><h1>Hello!</h1><div data-countdown="2038/01/19"></div></div>';

//Convert html to amp html
$amphtml = $converter->convert($html);

//Get the necessary amp components
$amp_scripts = $converter->getScripts();

print_r($amphtml);
print_r($amp_scripts);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].