Skip to content

Commit

Permalink
Convert leading double spaces to tabs... once more, with feeling
Browse files Browse the repository at this point in the history
  • Loading branch information
pglewis committed Dec 16, 2013
1 parent 413cd8c commit 10fbfa5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
3 changes: 1 addition & 2 deletions admin/class-pods-export-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ public function pods_export_code () {
echo "function {$function}() {\n\n";

foreach ( $pod_names as $this_pod ) {
$code_output = $export_to_code->export_pod( $this_pod );
echo preg_replace( "/ {2}/", "\t", $code_output );
echo $export_to_code->export_pod( $this_pod );
}

echo "}\n";
Expand Down
33 changes: 31 additions & 2 deletions admin/classes/pods-export-code-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,46 @@ public function export_pod( $pod_name ) {
}

// Output the pods_register_type() call
$output .= sprintf( "\t\$pod = %s;\n\n", preg_replace( '/\d+ => /', '', var_export( $pod, true ) ) );
$output .= sprintf( "\t\$pod = %s;\n\n", $this->var_export_format( $pod, 1 ) );
$output .= "\tpods_register_type( \$pod[ 'type' ], \$pod[ 'name' ], \$pod );\n\n";

// Output a pods_register_field() call for each field
foreach ( $fields as $this_field ) {
$output .= sprintf( "\t\$field = %s;\n\n", preg_replace( '/\d+ => /', '', var_export( $this_field, true ) ) );
$output .= sprintf( "\t\$field = %s;\n\n", preg_replace( '/\d+ => /', '', $this->var_export_format( $this_field, 1 ) ) );
$output .= "\tpods_register_field( \$pod[ 'name' ], \$field[ 'name' ], \$field );\n\n";
}

return $output;

}

/**
* @param mixed $var
* @param int $leading_tabs
*
* @return string
*/
private function var_export_format( $var, $leading_tabs = 0 ) {
$var = var_export( $var, true );
$leading_tabs = str_repeat( "\t", $leading_tabs );

// Convert params like 0 => 'Option 1' to just 'Option 1'
$var = preg_replace( '/\d+ => /', '', $var );

$output = '';
foreach( preg_split('~[\r\n]+~', $var ) as $line ){

// Skip blank lines
if( empty($line) || ctype_space( $line ) ) {
continue;
}

// Leading tabs plus replace double spaces with tabs
$output .= sprintf( "%s%s\n", $leading_tabs, preg_replace( "/ {2}/", "\t", $line ) );
}

// Trim the leading tab and the final newline
return ltrim( rtrim( $output, "\n"), "\t" );
}

}

0 comments on commit 10fbfa5

Please sign in to comment.