Skip to content

Commit

Permalink
more test and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Kadis committed Dec 22, 2016
1 parent 279f312 commit 060b416
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
38 changes: 37 additions & 1 deletion inc/migrate-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,32 @@ public function __construct( $post ) {

$this->front_matter_src = $this->extract_front_matter( $this->post );
$this->front_matter = $this->transform_front_matter( $this->front_matter_src );
$this->content = $this->transform_post_content( $this->post->post_content );
$this->markdown = $this->transform_post_content( $this->post->post_content );

// Check content for excessive HTML tags, flag for manual inspection
$this->num_tags = $this->count_tags( $this->markdown );
if ( 10 < $this->num_tags ) {
return array( 'error' => sprintf( 'Markdown for post %s contains %s HTML tags; manual inspection required.', $this->post->ID, $this->num_tags ) );
}

// Write output to file

return array( 'success' => 'Migrated post ' . $this->post->ID );
}

/**
* Count the number of HTML tags in the string. Used to flag Markdown for manual review
*
* @param string $input
* @param int
*/
public function count_tags( $input ) {
// split string by html opening tags, doesn't need to be exact
$count = count( preg_split( '/<([A-Z][A-Z0-9]*)\b[^>]*>/i', $input ) );

return $count - 1;
}

/**
* Extract data that will be converted to YAML front matter
*
Expand Down Expand Up @@ -122,6 +143,7 @@ public function transform_post_content( $content ) {

/**
* Handle some known gotchas in WP -> Markdown
* @todo unit testing for this method
*/
public function filter_markdown( $markdown ) {
// Update links
Expand Down Expand Up @@ -209,6 +231,9 @@ public function convert_link_embeds( $content ) {
return $content;
}

/**
* Convert Jetpack media embeds and other shortcodes
*/
public function convert_shortcodes( $content ) {
// Override Jetpack shortcodes
add_shortcode( 'youtube', function( $atts ) {
Expand All @@ -220,10 +245,21 @@ public function convert_shortcodes( $content ) {
return $this->hugo_shortcode( 'vimeo', $id );
} );

/**
* @todo handle quote, caption
*/

$content = do_shortcode( $content );
return $content;
}

/**
* Generate Hugo shortcode, e.g. `{{< tweet 12341234 >}}`
*
* @param string $provider
* @param string|int $id Can be id or link
* @param bool $from_link If true, parse id from URL depending on provider
*/
public function hugo_shortcode( $provider, $id, $from_link = false ) {
// Convert URL to ID if needed
if ( $from_link) {
Expand Down
7 changes: 7 additions & 0 deletions tests/data/test/count_tags.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<ul>
<li>Dublin is having a merry <a href="https://www.meetup.com/hacks-hackers-dublin/events/236094987/">Christmas gathering</a></li>
<li>San Francisco is <a href="https://www.eventbrite.com/e/holiday-party-for-bay-area-journalists-tickets-27677632544">meeting</a> Solutions Journalism Network's Bay Area chapter for a drink</li>
<li><a href="http://www.meetup.com/Hacks-Hackers-Miami/">Miami</a> is holding its regular OpenHack Miami</li>
<li><a href="http://www.meetup.com/hackshackersIRE/">IRE</a> in Missouri is holding its weekly open lab</li>
<li>Nairobi is <a href="https://docs.google.com/forms/d/13iJf6EHjGSSPJWCCgv5SSrSRn43yoMP-uuITZ4sgwvI/viewform?edit_requested=true">discussing how</a> governments can be held more accountable</li>
</ul>
26 changes: 23 additions & 3 deletions tests/test-migrate-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ public function setUp() {
$this->migrator = new HH_Hugo\Migrate_Post('test');
}

/**
* Get contents of test data file
*
* @param string $type 'test' or 'expect'
* @param string $filename
* @return string Contents of file
*/
private function _get_test_data( $type, $filename ) {
return file_get_contents( HH_HUGO_COMMAND_DIR . '/tests/data/' . $type . '/' . $filename );
}

public function test_class_exists() {
$this->assertTrue( class_exists( '\\HH_Hugo\\Migrate_Post' ) );
}
Expand All @@ -59,7 +70,7 @@ public function test_extract_front_matter() {
// Delete migration data since we can't test the ID and timestamp
unset( $front_matter['_migration'] );
$this->assertEquals(
file_get_contents( HH_HUGO_COMMAND_DIR . '/tests/data/expect/front-matter.yml' ),
$this->_get_test_data( 'expect', 'front-matter.yml' ),
$this->migrator->transform_front_matter( $front_matter )
);

Expand Down Expand Up @@ -121,13 +132,22 @@ public function test_transform_post_content() {
}

private function _test_transform_post_content( $filename, $dump = false ) {
$input = file_get_contents( HH_HUGO_COMMAND_DIR . '/tests/data/test/' . $filename . '.html' );
$output = file_get_contents( HH_HUGO_COMMAND_DIR . '/tests/data/expect/' . $filename . '.md' );
$input = $this->_get_test_data( 'test', $filename . '.html' );
$output = $this->_get_test_data( 'expect', $filename . '.md' );

if ( $dump ) {
die( var_dump( $this->migrator->transform_post_content( $input ) ) );
}

$this->assertEquals( $output, $this->migrator->transform_post_content( $input ) );
}

public function test_count_tags() {
$input = $this->_get_test_data( 'test', 'count_tags.html' );
$this->assertEquals( 11, $this->migrator->count_tags( $input ) );
$this->assertEquals( 1, $this->migrator->count_tags( '<h1>title</h1>' ) );
$this->assertEquals( 1, $this->migrator->count_tags( '<img src="http://foo.bar/img.png" />' ) );
$this->assertEquals( 2, $this->migrator->count_tags( '<p><span>hi</span></p>' ) );
$this->assertEquals( 0, $this->migrator->count_tags( 'hello world' ) );
}
}

0 comments on commit 060b416

Please sign in to comment.