mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTML API: Return elements pushed and popped rather than tags read.
- Loading branch information
Showing
4 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
src/wp-includes/html-api/class-wp-html-to-markdown-converter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
|
||
class WP_HTML_To_Markdown_Converter { | ||
public static function convert( $html ) { | ||
$processor = WP_HTML_Processor::create_fragment( $html ); | ||
$md = ''; | ||
$list_items = array(); | ||
$depth = 0; | ||
echo "\n"; | ||
|
||
echo "\e[90mFound these nodes…\e[m\n"; | ||
$node_count = 8; | ||
while ( $processor->next_token() ) { | ||
$indent = str_pad( '', $depth * 2, ' ' ); | ||
$token_name = $processor->get_token_name(); | ||
$breadcrumbs = $processor->get_breadcrumbs(); | ||
|
||
$closer = $processor->is_tag_closer() ? '/' : ''; | ||
if ( 0 === --$node_count ) { | ||
$node_count = 8; | ||
echo "\n"; | ||
} | ||
echo "\e[36m{$closer}\e[32m{$token_name}\e[m "; | ||
|
||
if ( $processor->is_tag_closer() ) { | ||
switch ( $token_name ) { | ||
case 'H1': | ||
case 'H2': | ||
case 'H3': | ||
case 'H4': | ||
case 'H5': | ||
case 'H6': | ||
$md .= "\n"; | ||
break; | ||
|
||
case 'B': | ||
case 'STRONG': | ||
$md .= '*'; | ||
break; | ||
|
||
case 'I': | ||
case 'EM': | ||
$md .= '_'; | ||
break; | ||
|
||
case 'OL': | ||
case 'UL': | ||
--$depth; | ||
array_pop( $list_items ); | ||
break; | ||
} | ||
|
||
// Proceed to the next token. | ||
continue; | ||
} | ||
|
||
switch ( $token_name ) { | ||
case '#text': | ||
$md .= $processor->get_modifiable_text(); | ||
break; | ||
|
||
case 'P': | ||
$md .= "\n"; | ||
break; | ||
|
||
case 'H1': | ||
case 'H2': | ||
case 'H3': | ||
case 'H4': | ||
case 'H5': | ||
case 'H6': | ||
$hash_count = intval( $token_name[1] ); | ||
$hashes = str_pad( '', $hash_count, '#' ); | ||
$md .= "\n\n{$hashes} "; | ||
break; | ||
|
||
case 'B': | ||
case 'STRONG': | ||
$md .= '*'; | ||
break; | ||
|
||
case 'I': | ||
case 'EM': | ||
$md .= '_'; | ||
break; | ||
|
||
case 'LI': | ||
$list_item = end( $list_items ); | ||
$md .= "\n{$indent}{$list_item} "; | ||
break; | ||
|
||
case 'OL': | ||
++$depth; | ||
$list_items[] = '*'; | ||
break; | ||
|
||
case 'UL': | ||
++$depth; | ||
$list_items[] = '-'; | ||
break; | ||
} | ||
|
||
$last_breadcrumbs = $breadcrumbs; | ||
} | ||
|
||
if ( null !== $processor->get_last_error() ) { | ||
die( "Encountered unsupported HTML: failed to convert.\n" ); | ||
} | ||
|
||
$closed_elements = array(); | ||
for ( $i = 0; $i < count( $last_breadcrumbs ); $i++ ) { | ||
if ( | ||
isset( $last_breadcrumbs[ $i ], $breadcrumbs[ $i ] ) && | ||
$last_breadcrumbs[ $i ] === $breadcrumbs[ $i ] | ||
) { | ||
continue; | ||
} | ||
|
||
$closed_elements = array_slice( $last_breadcrumbs, $i ); | ||
break; | ||
} | ||
|
||
$closed_elements = array_reverse( $closed_elements ); | ||
foreach ( $closed_elements as $element ) { | ||
switch ( $element ) { | ||
case 'H1': | ||
case 'H2': | ||
case 'H3': | ||
case 'H4': | ||
case 'H5': | ||
case 'H6': | ||
$md .= "\n"; | ||
break; | ||
|
||
case 'B': | ||
case 'STRONG': | ||
$md .= '*'; | ||
break; | ||
|
||
case 'I': | ||
case 'EM': | ||
$md .= '_'; | ||
break; | ||
|
||
case 'OL': | ||
case 'UL': | ||
--$depth; | ||
array_pop( $list_items ); | ||
break; | ||
} | ||
} | ||
|
||
return $md; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters