Skip to content

Commit

Permalink
Inital Release
Browse files Browse the repository at this point in the history
  • Loading branch information
thampe committed Mar 13, 2016
0 parents commit df11dbd
Show file tree
Hide file tree
Showing 29 changed files with 1,821 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.DS_Store
composer.lock
/vendor
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Inky

A PHP Implementation of ZURB's Foundation for Email parser ([Inky](https://github.com/zurb/inky)).

## Installation

You can install this bundle using composer

composer require hampe/inky

or add the package to your `composer.json` file directly.

## Usage and Examples

### Basic Usage.

```php
<?php
use Hampe\Inky\Inky;

$gridColumns = 12; //optional, default is 12
$additionalComponentFactories = []; //optional
$inky = new Inky($gridColumns, $additionalComponentFactories);

$inky->releaseTheKraken('html...');
```

### Add Tag-Alias

```php
<?php
use Hampe\Inky\Inky;

$inky = new Inky();
$inky->addAlias('test', 'callout')

$inky->releaseTheKraken('<test>123</test>'); //equal to "<callout>123</callout>"
```

### Add your own Custom Component Factory

Add your own Custom Component Factory, to convert HTML-Tags.

```
<?php
use Hampe\Inky\Component\ComponentFactoryInterface;
use Hampe\Inky\Inky;
use PHPHtmlParser\Dom\HtmlNode;
class TestComponentFactory implements ComponentFactoryInterface
{
public function getName()
{
return 'test' // name of the html tag.
}
public function parse(HtmlNode $element, Inky $inkyInstance)
{
// ...
}
}
$inky = new Inky();
$inky->addComponentFactory(new TestComponentFactory());
$inky->releaseTheKraken('<test></test>');
```

## License
See the [LICENSE](LICENSE) file for license info (it's the MIT license).

23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "hampe/inky-parse",
"description": "PHP Implementation of ZURB's Foundation for Email parser (Inky)",
"version": "1.2.3.0",
"license": "MIT",
"authors": [
{
"name": "Thomas Hampe",
"email": "github@hampe.co"
}
],
"require-dev": {
"phpunit/phpunit": "4.8.*"
},
"autoload": {
"psr-4": {
"Hampe\\Inky\\": "src/"
}
},
"require": {
"paquettg/php-html-parser": "^1.6"
}
}
8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" ?>
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="inky">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
83 changes: 83 additions & 0 deletions src/Component/AbstractComponentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* AbstractComponent.php
*
*
* @license see LICENSE File
* @filename AbstractComponent.php
* @package inky-parse
* @author Thomas Hampe <github@hampe.co>
* @copyright 2013-2016 Thomas Hampe
* @date 10.01.16
*/


namespace Hampe\Inky\Component;


use PHPHtmlParser\Dom\HtmlNode;

abstract class AbstractComponentFactory implements ComponentFactoryInterface
{

protected function copyChildren(HtmlNode $fromElement, HtmlNode $toElement)
{
$newNodeChildren = $fromElement->getChildren();
foreach($newNodeChildren as $child) {
$toElement->addChild($child);
}
return $newNodeChildren;
}

protected function node($tag, $attributes = array())
{
$node = new HtmlNode($tag);
foreach($attributes as $key => $attribute) {
$node->setAttribute($key, $attribute);
}
return $node;
}

protected function table($attributes = array())
{
return $this->node('table', $attributes);
}

protected function tbody($attributes = array())
{
return $this->node('tbody', $attributes);
}

protected function tr($attributes = array())
{
return $this->node('tr', $attributes);
}

protected function td($attributes = array())
{
return $this->node('td', $attributes);
}

protected function th($attributes = array())
{
return $this->node('th', $attributes);
}

protected function img($attributes = array())
{
$node = $this->node('img', $attributes);
$node->getTag()->selfClosing();
return $node;
}

protected function elementHasCssClass(HtmlNode $element, $cssClass)
{
$class = $element->getAttribute('class');
return is_string($class) && strpos($class, $cssClass) !== false;
}

protected function addCssClass($cssClass, HtmlNode $element)
{
$element->setAttribute('class', trim($cssClass . ' ' . $element->getAttribute('class')));
}
}
61 changes: 61 additions & 0 deletions src/Component/BlockGridFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* BlockGridFactory.php
*
*
* @license see LICENSE File
* @filename BlockGridFactory.php
* @package inky-parse
* @author Thomas Hampe <github@hampe.co>
* @copyright 2013-2016 Thomas Hampe
* @date 27.02.16
*/


namespace Hampe\Inky\Component;


use Hampe\Inky\Inky;
use PHPHtmlParser\Dom\HtmlNode;

class BlockGridFactory extends AbstractComponentFactory
{
const NAME = 'block-grid';

/**
* @return string
*/
public function getName()
{
return self::NAME;
}

/**
* <block-grid up="{up}">{inner}</block-grid>
* ------------------------------------------
* <table class="block-grid up-{up}">
* <tr>{inner}</tr>
* </table>
*
* @param HtmlNode $element
* @param Inky $inkyInstance
*
* @return HtmlNode
*/
public function parse(HtmlNode $element, Inky $inkyInstance)
{
$upAttribute = (string) $element->getAttribute('up');
$table = $this->table(array('class' => trim(sprintf(
'block-grid up-%s %s',
$upAttribute,
(string) $element->getAttribute('class')
))));
$tr = $this->tr();
$this->copyChildren($element, $tr);
$table->addChild($tr);

return $table;
}


}
113 changes: 113 additions & 0 deletions src/Component/ButtonFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Button.php
*
*
* @license see LICENSE File
* @filename Button.php
* @package inky-parse
* @author Thomas Hampe <github@hampe.co>
* @copyright 2013-2016 Thomas Hampe
* @date 10.01.16
*/


namespace Hampe\Inky\Component;


use Hampe\Inky\Inky;
use PHPHtmlParser\Dom\HtmlNode;

class ButtonFactory extends AbstractComponentFactory
{
const NAME = 'button';

public function getName()
{
return self::NAME;
}


/**
* <button href="" class="{class}">{inner}</button>
* -----------------------------------------------
* <table class="button {class}">
* <tr>
* <td>
* <table>
* <tr>
* <td>{inner}</td>
* </tr>
* </table>
* </td>
* </tr>
* </table>
*
* - OR -
*
* <button href="" class="expand {class}">{inner}</inner>
* -----------------------------------------------
* <table class="button {class}">
* <tr>
* <td>
* <table>
* <tr>
* <center>
* <td>{inner}</td>
* </center>
* </tr>
* </table>
* </td>
* </tr>
* </table>
*
* @param HtmlNode $element
* @param Inky $inkyInstance
*
* @return HtmlNode
*/
public function parse(HtmlNode $element, Inky $inkyInstance)
{
$attributes = $element->getAttributes();
if(isset($attributes['href'])) {
$href= $attributes['href'];
unset($attributes['href']);
} else {
$href = null;
}

$table = $this->table($attributes);
$this->addCssClass('button', $table);
$tr = $this->tr();
$td = $this->td();
$childTable = $this->table();
$childTr = $this->tr();
$childTd = $this->td();

$lastChild = $childTd;
//wrap in center if element has class expand
if($this->elementHasCssClass($element, 'expand')) {
$center = $this->node('center');
$lastChild->addChild($center);
$lastChild = $center;
}
//wrap in <a /> if element has href
if($href !== null) {
$a = $this->node('a', array('href' => (string) $href));
$lastChild->addChild($a);
$lastChild = $a;
}

$this->copyChildren($element, $lastChild);

$childTr->addChild($childTd);
$childTable->addChild($childTr);
$td->addChild($childTable);
$tr->addChild($td);
$table->addChild($tr);

return $table;
}


}
Loading

0 comments on commit df11dbd

Please sign in to comment.