-
Notifications
You must be signed in to change notification settings - Fork 6
/
Premailer.class.php
155 lines (146 loc) · 4.4 KB
/
Premailer.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Premailer
*
* @author Oliver Nassar <[email protected]>
* @see https://github.com/alexdunae/premailer/
* @see http://premailer.dialect.ca/
*/
class Premailer
{
/**
* _markup
*
* @access protected
* @var null|string (default: null)
*/
protected $_markup = null;
/**
* _options
*
* @note Some servers bugged outwhen the following were turned on:
* - remove_classes
* - remove_ids
* @see https://github.com/premailer/premailer/blob/master/lib/premailer/premailer.rb
* @access protected
* @var array
*/
protected $_options = array(
'css_to_attributes' => true,
'include_link_tags' => true,
'include_style_tags' => true,
'input_encoding' => 'ASCII-8BIT',
'preserve_reset' => true,
'preserve_styles' => true,
'remove_classes' => false,
'remove_comments' => false,
'remove_ids' => false,
'remove_scripts' => true,
'replace_html_entities' => false
);
/**
* __construct
*
* @access public
* @return void
*/
public function __construct()
{
}
/**
* _getCLIOptions
*
* @access protected
* @return array
*/
protected function _getCLIOptions(): array
{
$cliOptions = array();
$escapedMarkup = $this->_getEscapedMarkup();
$this->_options['markup'] = '"' . ($escapedMarkup) . '"';
$this->_options['with_html_string'] = true;
$options = $this->_options;
foreach ($options as $key => $value) {
if ($value === false) {
continue;
}
if ($value === true) {
$cliOption = '--' . ($key);
array_push($cliOptions, $cliOption);
continue;
}
$cliOption = '--' . ($key) . ' ' . ($value);
array_push($cliOptions, $cliOption);
}
return $cliOptions;
}
/**
* _getCLIOptionsString
*
* @see http://rubyforge.org/docman/view.php/735/281/README.html
* @access protected
* @return string
*/
protected function _getCLIOptionsString()
{
$options = $this->_getCLIOptions();
$options = implode(' ', $options);
return $options;
}
/**
* _getEscapedMarkup
*
* @access protected
* @return string
*/
protected function _getEscapedMarkup(): string
{
$markup = $this->_markup;
$escapedMarkup = str_replace('"', '\"', $markup);
return $escapedMarkup;
}
/**
* getInlinedMarkup
*
* @access public
* @return string
*/
public function getInlinedMarkup()
{
$scriptPath = dirname(__FILE__) . '/converter.rb';
$output = array();
$returnVar = 0;
$command = ($scriptPath) . ' ' . $this->_getCLIOptionsString();
$response = exec($command, $output, $returnVar);
if ($returnVar === 1) {
$msg = 'Error';
throw new Exception($msg);
}
$response = implode("\n", $output);
return $response;
}
/**
* setMarkup
*
* @access public
* @param string $markup
* @return void
*/
public function setMarkup(string $markup): void
{
$this->_markup = $markup;
}
/**
* setOption
*
* @see https://github.com/alexdunae/premailer/blob/master/lib/premailer/premailer.rb#L164
* @access public
* @param string $key
* @param mixed $value
* @return void
*/
public function setOption(string $key, $value): void
{
$this->_options[$key] = $value;
}
}