Skip to content

Commit df11dbd

Browse files
committed
Inital Release
0 parents  commit df11dbd

29 files changed

+1821
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
.DS_Store
3+
composer.lock
4+
/vendor

README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Inky
2+
3+
A PHP Implementation of ZURB's Foundation for Email parser ([Inky](https://github.com/zurb/inky)).
4+
5+
## Installation
6+
7+
You can install this bundle using composer
8+
9+
composer require hampe/inky
10+
11+
or add the package to your `composer.json` file directly.
12+
13+
## Usage and Examples
14+
15+
### Basic Usage.
16+
17+
```php
18+
<?php
19+
use Hampe\Inky\Inky;
20+
21+
$gridColumns = 12; //optional, default is 12
22+
$additionalComponentFactories = []; //optional
23+
$inky = new Inky($gridColumns, $additionalComponentFactories);
24+
25+
$inky->releaseTheKraken('html...');
26+
```
27+
28+
### Add Tag-Alias
29+
30+
```php
31+
<?php
32+
use Hampe\Inky\Inky;
33+
34+
$inky = new Inky();
35+
$inky->addAlias('test', 'callout')
36+
37+
$inky->releaseTheKraken('<test>123</test>'); //equal to "<callout>123</callout>"
38+
```
39+
40+
### Add your own Custom Component Factory
41+
42+
Add your own Custom Component Factory, to convert HTML-Tags.
43+
44+
```
45+
<?php
46+
47+
use Hampe\Inky\Component\ComponentFactoryInterface;
48+
use Hampe\Inky\Inky;
49+
use PHPHtmlParser\Dom\HtmlNode;
50+
51+
class TestComponentFactory implements ComponentFactoryInterface
52+
{
53+
public function getName()
54+
{
55+
return 'test' // name of the html tag.
56+
}
57+
58+
public function parse(HtmlNode $element, Inky $inkyInstance)
59+
{
60+
// ...
61+
}
62+
}
63+
64+
$inky = new Inky();
65+
$inky->addComponentFactory(new TestComponentFactory());
66+
$inky->releaseTheKraken('<test></test>');
67+
```
68+
69+
## License
70+
See the [LICENSE](LICENSE) file for license info (it's the MIT license).
71+

composer.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "hampe/inky-parse",
3+
"description": "PHP Implementation of ZURB's Foundation for Email parser (Inky)",
4+
"version": "1.2.3.0",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Thomas Hampe",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require-dev": {
13+
"phpunit/phpunit": "4.8.*"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Hampe\\Inky\\": "src/"
18+
}
19+
},
20+
"require": {
21+
"paquettg/php-html-parser": "^1.6"
22+
}
23+
}

phpunit.xml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" ?>
2+
<phpunit bootstrap="vendor/autoload.php">
3+
<testsuites>
4+
<testsuite name="inky">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
</phpunit>
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* AbstractComponent.php
4+
*
5+
*
6+
* @license see LICENSE File
7+
* @filename AbstractComponent.php
8+
* @package inky-parse
9+
* @author Thomas Hampe <[email protected]>
10+
* @copyright 2013-2016 Thomas Hampe
11+
* @date 10.01.16
12+
*/
13+
14+
15+
namespace Hampe\Inky\Component;
16+
17+
18+
use PHPHtmlParser\Dom\HtmlNode;
19+
20+
abstract class AbstractComponentFactory implements ComponentFactoryInterface
21+
{
22+
23+
protected function copyChildren(HtmlNode $fromElement, HtmlNode $toElement)
24+
{
25+
$newNodeChildren = $fromElement->getChildren();
26+
foreach($newNodeChildren as $child) {
27+
$toElement->addChild($child);
28+
}
29+
return $newNodeChildren;
30+
}
31+
32+
protected function node($tag, $attributes = array())
33+
{
34+
$node = new HtmlNode($tag);
35+
foreach($attributes as $key => $attribute) {
36+
$node->setAttribute($key, $attribute);
37+
}
38+
return $node;
39+
}
40+
41+
protected function table($attributes = array())
42+
{
43+
return $this->node('table', $attributes);
44+
}
45+
46+
protected function tbody($attributes = array())
47+
{
48+
return $this->node('tbody', $attributes);
49+
}
50+
51+
protected function tr($attributes = array())
52+
{
53+
return $this->node('tr', $attributes);
54+
}
55+
56+
protected function td($attributes = array())
57+
{
58+
return $this->node('td', $attributes);
59+
}
60+
61+
protected function th($attributes = array())
62+
{
63+
return $this->node('th', $attributes);
64+
}
65+
66+
protected function img($attributes = array())
67+
{
68+
$node = $this->node('img', $attributes);
69+
$node->getTag()->selfClosing();
70+
return $node;
71+
}
72+
73+
protected function elementHasCssClass(HtmlNode $element, $cssClass)
74+
{
75+
$class = $element->getAttribute('class');
76+
return is_string($class) && strpos($class, $cssClass) !== false;
77+
}
78+
79+
protected function addCssClass($cssClass, HtmlNode $element)
80+
{
81+
$element->setAttribute('class', trim($cssClass . ' ' . $element->getAttribute('class')));
82+
}
83+
}

src/Component/BlockGridFactory.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* BlockGridFactory.php
4+
*
5+
*
6+
* @license see LICENSE File
7+
* @filename BlockGridFactory.php
8+
* @package inky-parse
9+
* @author Thomas Hampe <[email protected]>
10+
* @copyright 2013-2016 Thomas Hampe
11+
* @date 27.02.16
12+
*/
13+
14+
15+
namespace Hampe\Inky\Component;
16+
17+
18+
use Hampe\Inky\Inky;
19+
use PHPHtmlParser\Dom\HtmlNode;
20+
21+
class BlockGridFactory extends AbstractComponentFactory
22+
{
23+
const NAME = 'block-grid';
24+
25+
/**
26+
* @return string
27+
*/
28+
public function getName()
29+
{
30+
return self::NAME;
31+
}
32+
33+
/**
34+
* <block-grid up="{up}">{inner}</block-grid>
35+
* ------------------------------------------
36+
* <table class="block-grid up-{up}">
37+
* <tr>{inner}</tr>
38+
* </table>
39+
*
40+
* @param HtmlNode $element
41+
* @param Inky $inkyInstance
42+
*
43+
* @return HtmlNode
44+
*/
45+
public function parse(HtmlNode $element, Inky $inkyInstance)
46+
{
47+
$upAttribute = (string) $element->getAttribute('up');
48+
$table = $this->table(array('class' => trim(sprintf(
49+
'block-grid up-%s %s',
50+
$upAttribute,
51+
(string) $element->getAttribute('class')
52+
))));
53+
$tr = $this->tr();
54+
$this->copyChildren($element, $tr);
55+
$table->addChild($tr);
56+
57+
return $table;
58+
}
59+
60+
61+
}

src/Component/ButtonFactory.php

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Button.php
4+
*
5+
*
6+
* @license see LICENSE File
7+
* @filename Button.php
8+
* @package inky-parse
9+
* @author Thomas Hampe <[email protected]>
10+
* @copyright 2013-2016 Thomas Hampe
11+
* @date 10.01.16
12+
*/
13+
14+
15+
namespace Hampe\Inky\Component;
16+
17+
18+
use Hampe\Inky\Inky;
19+
use PHPHtmlParser\Dom\HtmlNode;
20+
21+
class ButtonFactory extends AbstractComponentFactory
22+
{
23+
const NAME = 'button';
24+
25+
public function getName()
26+
{
27+
return self::NAME;
28+
}
29+
30+
31+
/**
32+
* <button href="" class="{class}">{inner}</button>
33+
* -----------------------------------------------
34+
* <table class="button {class}">
35+
* <tr>
36+
* <td>
37+
* <table>
38+
* <tr>
39+
* <td>{inner}</td>
40+
* </tr>
41+
* </table>
42+
* </td>
43+
* </tr>
44+
* </table>
45+
*
46+
* - OR -
47+
*
48+
* <button href="" class="expand {class}">{inner}</inner>
49+
* -----------------------------------------------
50+
* <table class="button {class}">
51+
* <tr>
52+
* <td>
53+
* <table>
54+
* <tr>
55+
* <center>
56+
* <td>{inner}</td>
57+
* </center>
58+
* </tr>
59+
* </table>
60+
* </td>
61+
* </tr>
62+
* </table>
63+
*
64+
* @param HtmlNode $element
65+
* @param Inky $inkyInstance
66+
*
67+
* @return HtmlNode
68+
*/
69+
public function parse(HtmlNode $element, Inky $inkyInstance)
70+
{
71+
$attributes = $element->getAttributes();
72+
if(isset($attributes['href'])) {
73+
$href= $attributes['href'];
74+
unset($attributes['href']);
75+
} else {
76+
$href = null;
77+
}
78+
79+
$table = $this->table($attributes);
80+
$this->addCssClass('button', $table);
81+
$tr = $this->tr();
82+
$td = $this->td();
83+
$childTable = $this->table();
84+
$childTr = $this->tr();
85+
$childTd = $this->td();
86+
87+
$lastChild = $childTd;
88+
//wrap in center if element has class expand
89+
if($this->elementHasCssClass($element, 'expand')) {
90+
$center = $this->node('center');
91+
$lastChild->addChild($center);
92+
$lastChild = $center;
93+
}
94+
//wrap in <a /> if element has href
95+
if($href !== null) {
96+
$a = $this->node('a', array('href' => (string) $href));
97+
$lastChild->addChild($a);
98+
$lastChild = $a;
99+
}
100+
101+
$this->copyChildren($element, $lastChild);
102+
103+
$childTr->addChild($childTd);
104+
$childTable->addChild($childTr);
105+
$td->addChild($childTable);
106+
$tr->addChild($td);
107+
$table->addChild($tr);
108+
109+
return $table;
110+
}
111+
112+
113+
}

0 commit comments

Comments
 (0)