Giter Club home page Giter Club logo

camp's Introduction

Camp

Convert HTML main article to AMPHTML. Please note that this library is not meant to convert entirely HTML document to AMP. Use the library to convert main article of the page only, as in most case main article is the part where we generated it by WYSIWYG ( ex: tinymce ). Another HTML part should rewrite to fullfill AMP specifications.

Due to complicated processing in background, I suggest to not to use the library for every user request. Cache the result or save it to DB instead of convert it everytime user ask for it.

Usage

<?php

require_once '/path/to/Camp.php';

$html = '<div id="main-content">...</div>';
$options = array(...);

$camp = new Camp($html, $options);

$amp   = $camp->amp;
$comps = $camp->components;

foreach($comps as $comp)
    echo '<script async custom-element="' . $comp . '" src="https://cdn.ampproject.org/v0/' . $comp . '-0.1.js"></script>';

Methods

setOptions ( string|array $key, mixed $value )

Set the options value

<?php

// #1
$options = array(
    'defaultWidth' => 600,
    'defaultHeight'=> 450
);

$camp->setOptions($options);

// #2
$camp->setOptions('localHost', 'http://localhost');

convert ( string $html, array $options )

Start convert the content.

<?php

// #1

$html = '<div id="main-content">...</div>';
$opts = array(...);

$camp = new Camp($html, $opts);

$amp  = $camp->amp;
$comps= $camp->components;

// #2

$camp = new Camp;

$camp->html = '<div id="main-content">...</div>';
$camp->setOptions(...);

$camp->convert();

$amp   = $camp->amp;
$comps = $camp->comps;

Properties

List of Camp properties.

amp string

AMP version of the HTML content.

html string

Original HTML of the content.

Options

List of options that Camp understand and use during conversion.

downloadImage boolean

Download the image if attribute width or height not found on the img tag. Default false.

localImagePath string

Find the image on local system in case the src attribute of the image is relative url. This option value will be prefixed to the image src attribute as absolute location of the image. Default ''.

$camp = new Camp;
$camp->html = '<img src="/relative/path/to/image.jpg">';
$camp->setOptions('localImagePath', '/var/www/html');

// the image size taken from file "/var/www/html/relative/path/to/image.jpg"

localHost string

Download the image from this host in case the image not found on local system by option localImagePath. Used only if the attribute src of the image is relative url. Default ''.

$camp = new Camp;
$camp->html = '<img src="/relative/path/to/image.jpg">';
$camp->setOptions('localImagePath', '/var/www/html');
$camp->setOptions('localHost', 'http://example.com/');

// the image downloaded from  "http://example.com/relative/path/to/image.jpg"
// if the image not found on "/var/www/html/relative/path/to/image.jpg"

defaultWidth integer

If no way to find image size, use this size instead. Default 300.

defaultHeight integer

If no way to find image size, use this size instead. Default 200.

iframePlaceholder string

Default image to use as amp-iframe image placeholder. The image is an amp-img with attribute layout=fill.

videoPoster string

Default image to use as default video poster if no poster found on the video tag.

Dependencies

  1. html5lib-php
    Not to mention PHP 7.0.3, they still have no idea how to read HTML5 syntax.

Contribute

This project is under MIT license, and hosted on github. There's always new tag that may visible on article content, I'll need help to add more parser/converter for each element. Please see below list of element that need to convert.

TODO

  1. Remove unusable SVG syntax.
  2. amp-ad. Support another ad provider, currently only support AdSense.
  3. Support another components
    1. amp-3q-player
    2. amp-apester-media
    3. amp-audio
    4. amp-brid-player
    5. amp-brightcove
    6. amp-carousel
    7. amp-dailymotion
    8. amp-gist
    9. amp-google-vrview-image
    10. amp-gfycat
    11. amp-hulu
    12. amp-ima-video
    13. amp-image-lightbox
    14. amp-imgur
    15. amp-izlesene
    16. amp-jwplayer
    17. amp-kaltura-player
    18. amp-lightbox
    19. amp-list
    20. amp-mustache
    21. amp-nexxtv-player
    22. amp-o2-player
    23. amp-ooyala-player
    24. amp-pinterest
    25. amp-playbuzz
    26. amp-reach-player
    27. amp-reddit
    28. amp-slides
    29. amp-soundcloud
    30. amp-springboard-player
    31. amp-vimeo
    32. amp-viz-vega
    33. amp-vk

Bug?

It's oke to create a ticket.

camp's People

Contributors

iqbalfn avatar

Stargazers

Yvan avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.