forked from beezwax/WP-Publish-to-Apple-News
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathclass-markdown.php
311 lines (272 loc) · 6.93 KB
/
class-markdown.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* Publish to Apple News Includes: Apple_Exporter\Markdown class
*
* Contains a class which is used to parse raw HTML into Apple News Markdown format.
*
* @package Apple_News
* @subpackage Apple_Exporter
* @since 0.2.0
*/
/* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */
namespace Apple_Exporter;
use DOMElement;
use DOMNodeList;
/**
* This class transforms HTML into Article Format Markdown, which is a subset
* of Markdown.
*
* For elements that are not supported, just skip them and add the contents of
* the tag.
*
* @since 0.2.0
*/
class Markdown {
/**
* List index for OL and UL components being transformed to Markdown.
*
* @access private
* @var int
*/
private $list_index = 1;
/**
* List mode for lists being transformed to Markdown.
*
* @access private
* @var string
*/
private $list_mode = 'ul';
/**
* Parse an array of nodes into the specified format.
*
* @param DOMNodeList $nodes A list of DOMElement nodes to be processed.
*
* @access public
* @return string
*/
public function parse_nodes( $nodes ) {
// Loop over each DOMElement and pass off for parsing.
$result = '';
foreach ( $nodes as $node ) {
$result .= $this->parse_node( $node );
}
return $result;
}
/**
* Parse an individual node for markdown.
*
* @param DOMElement $node The node to process.
*
* @access private
* @return string The processed content, converted to a Markdown string.
*/
private function parse_node( $node ) {
switch ( $node->nodeName ) {
case 'strong':
return $this->parse_node_strong( $node );
case 'i':
case 'em':
return $this->parse_node_emphasis( $node );
case 'br':
return "\n";
case 'p':
return $this->parse_node_paragraph( $node );
case 'a':
return $this->parse_node_hyperlink( $node );
case 'ul':
return $this->parse_node_unordered_list( $node );
case 'ol':
return $this->parse_node_ordered_list( $node );
case 'li':
return $this->parse_node_list_item( $node );
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
return $this->parse_node_heading( $node );
case '#comment':
return '';
case '#text':
default:
return $this->parse_node_text( $node );
}
}
/**
* Convert an <em> node to markdown.
*
* @param DOMElement $node The node to process.
*
* @access private
* @return string The processed node, converted to a string.
*/
private function parse_node_emphasis( $node ) {
// If there is no text for this node, bail.
$text = $this->parse_nodes( $node->childNodes );
$check = trim( $text );
if ( empty( $check ) ) {
return '';
}
return '_' . $text . '_';
}
/**
* Convert a heading node to markdown.
*
* @param DOMElement $node The node to process.
*
* @access private
* @return string The processed node, converted to a string.
*/
private function parse_node_heading( $node ) {
// If there is no text for this node, bail.
$text = $this->parse_nodes( $node->childNodes );
$check = trim( $text );
if ( empty( $check ) ) {
return '';
}
return sprintf(
'%s %s' . "\n",
str_repeat( '#', intval( substr( $node->nodeName, 1, 1 ) ) ),
$text
);
}
/**
* Convert an <a> node to markdown.
*
* @param DOMElement $node The node to process.
*
* @access private
* @return string The processed node, converted to a string.
*/
private function parse_node_hyperlink( $node ) {
// Set the URL from the HREF parameter on the tag.
$url = $node->getAttribute( 'href' );
// Set the text from the content of the child nodes.
$text = $this->parse_nodes( $node->childNodes );
/**
* Modifies the URL for a specific hyperlink if for some reason it needed
* to be reformatted for Apple News, such as to add tracking parameters to
* track traffic originating from Apple News.
*
* @since 0.2.0
*
* @param string $url The URL to be filtered.
*/
$url = apply_filters( 'apple_news_markdown_hyperlink', $url );
// If there is no URL or no text, do not add the link.
$check = trim( $text );
if ( empty( $url ) || empty( $check ) ) {
return '';
}
return sprintf( '[%s](%s)', $text, esc_url_raw( $url ) );
}
/**
* Convert an <li> node to markdown.
*
* @param DOMElement $node The node to process.
*
* @access private
* @return string The processed node, converted to a string.
*/
private function parse_node_list_item( $node ) {
// If there is no text for this node, bail.
$text = $this->parse_nodes( $node->childNodes );
$check = trim( $text );
if ( empty( $check ) ) {
return '';
}
// Fork for ordered list items.
if ( 'ol' === $this->list_mode ) {
return sprintf(
'%d. %s',
$this->list_index++,
$text
);
}
return '- ' . $text;
}
/**
* Convert an <ol> node to markdown.
*
* @param DOMElement $node The node to process.
*
* @access private
* @return string The processed node, converted to a string.
*/
private function parse_node_ordered_list( $node ) {
$this->list_mode = 'ol';
$this->list_index = 1;
// If there is no text for this node, bail.
$text = $this->parse_nodes( $node->childNodes );
$check = trim( $text );
if ( empty( $check ) ) {
return '';
}
return $text . "\n\n";
}
/**
* Convert a <p> node to markdown.
*
* @param DOMElement $node The node to process.
*
* @access private
* @return string The processed node, converted to a string.
*/
private function parse_node_paragraph( $node ) {
// If there is no text for this node, bail.
$text = $this->parse_nodes( $node->childNodes );
$check = trim( $text );
if ( empty( $check ) ) {
return '';
}
return $text . "\n\n";
}
/**
* Convert a <strong> node to markdown.
*
* @param DOMElement $node The node to process.
*
* @access private
* @return string The processed node, converted to a string.
*/
private function parse_node_strong( $node ) {
// If there is no text for this node, bail.
$text = $this->parse_nodes( $node->childNodes );
$check = trim( $text );
if ( empty( $check ) ) {
return '';
}
return '**' . $text . '**';
}
/**
* Convert a text node to markdown.
*
* @param DOMElement $node The node to process.
*
* @access private
* @return string The processed node, converted to a string.
*/
private function parse_node_text( $node ) {
return str_replace( '!', '\\!', $node->nodeValue );
}
/**
* Convert a <ul> node to markdown.
*
* @param DOMElement $node The node to process.
*
* @access private
* @return string The processed node, converted to a string.
*/
private function parse_node_unordered_list( $node ) {
$this->list_mode = 'ul';
// If there is no text for this node, bail.
$text = $this->parse_nodes( $node->childNodes );
$check = trim( $text );
if ( empty( $check ) ) {
return '';
}
return $text . "\n\n";
}
}
/* phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */