Skip to content

Commit

Permalink
Put the plugins.json in the root directory
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Nov 11, 2021
1 parent 5685036 commit bff1e6d
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 106 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ Your own WordPress at the center of your online activity. Follow friends and oth
**Requires at least:** 5.0
**Tested up to:** 5.8
**Requires PHP:** 5.2.4
**License:** GPLv2 or later
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html
**License:** [GPLv2 or later](http://www.gnu.org/licenses/gpl-2.0.html)
**Stable tag:** trunk

## Description
Expand Down Expand Up @@ -38,6 +37,10 @@ In future, I could see mobile apps instead of talking to a third party, to talk

The logo was created by Ramon Dodd, @ramonopoly. Thank you!

Documentation for the plugin can be found on the [GitHub project Wiki](https://github.com/akirk/friends/wiki).

**Development of this plugin is done [on GitHub](https://github.com/akirk/friends). Pull requests welcome. Please see [issues](https://github.com/akirk/friends/issues) reported there before going to the [plugin forum](https://wordpress.org/support/plugin/friends).**

## Installation

1. Upload the `friends` directory to the `/wp-content/plugins/` directory
Expand Down
243 changes: 139 additions & 104 deletions bin/convert-readme-md.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env php
<?php
/**
* Rewrite README.md into WordPress's readme.txt
Expand All @@ -17,127 +16,163 @@
exit( 1 );
}

$readme_md = file_get_contents( __DIR__ . '/../README.md' );
// Run the conversion only if the file wasn't included.
$included_files = get_included_files();
if ( __FILE__ === $included_files[0] ) {
$readme_txt = convert_readme_md( file_get_contents( __DIR__ . '/../README.md' ), true );

$readme_txt = $readme_md;
if ( false === $readme_txt ) {
// error was already printed.
exit( 1 );
}

// Transform the sections above the description.
$readme_txt = preg_replace_callback(
'/^.+?(?=## Description)/s',
static function ( $matches ) {
// Delete lines with images.
$input = trim( preg_replace( '/\[?!\[.+/', '', $matches[0] ) );
if ( ! file_put_contents( __DIR__ . '/../readme.txt', $readme_txt ) ) {
fwrite( STDERR, "Failed to write readme.txt.\n" );
exit( 1 );
}

$parts = preg_split( '/\n\n+/', $input );

if ( 3 !== count( $parts ) ) {
fwrite( STDERR, "Too many sections in header found.\n" );
exit( __LINE__ );
}
fwrite( STDOUT, "Validated README.md and generated readme.txt\n" );
exit( 0 );
}

$header = $parts[0];
/**
* Convert a (Github) README.md to a (WordPress.org) readme.txt
*
* @param string $readme_md The readme md file contents.
* @param bool $print_errors Whether to print the errors.
*
* @return bool|string False if an error, the readme.txt otherwise.
*/
function convert_readme_md( $readme_md, $print_errors = false ) {
$readme_txt = $readme_md;
$metadata = array();

// Transform the sections above the description.
$readme_txt = preg_replace_callback(
'/^.+?(?=## Description)/s',
static function ( $matches ) use ( $print_errors, &$metadata ) {
// Delete lines with images.
$input = trim( preg_replace( '/\[?!\[.+/', '', $matches[0] ) );

$parts = preg_split( '/\n\n+/', $input );

if ( 3 !== count( $parts ) ) {
if ( $print_errors ) {
fwrite( STDERR, "Too many sections in header found.\n" );
}
return false;
}

$description = $parts[1];
if ( strlen( $description ) > 150 ) {
fwrite( STDERR, 'The short description is too long (' . strlen( $description ) . " chars): $description\n" );
exit( __LINE__ );
}
$header = $parts[0];

$metadata = array();
foreach ( explode( "\n", $parts[2] ) as $meta ) {
$meta = trim( $meta );
if ( ! preg_match( '/^\*\*(?P<key>.+?):\*\* (?P<value>.+)/', $meta, $matches ) ) {
fwrite( STDERR, "Parse error for meta line: $meta.\n" );
exit( __LINE__ );
$description = $parts[1];
if ( strlen( $description ) > 150 ) {
if ( $print_errors ) {
fwrite( STDERR, 'The short description is too long (' . strlen( $description ) . " chars): $description\n" );
}
return false;
}

$unlinked_value = preg_replace( '/\[(.+?)]\(.+?\)/', '$1', $matches['value'] );
foreach ( explode( "\n", $parts[2] ) as $meta ) {
$meta = trim( $meta );
if ( ! preg_match( '/^\*\*(?P<key>.+?):\*\* (?P<value>.+)/', $meta, $matches ) ) {
if ( $print_errors ) {
fwrite( STDERR, "Parse error for meta line: $meta.\n" );
}
return false;
}

$unlinked_value = preg_replace( '/\[(.+?)]\(.+?\)/', '$1', $matches['value'] );

$metadata[ $matches['key'] ] = $unlinked_value;

$metadata[ $matches['key'] ] = $unlinked_value;
// Extract License URI from link.
if ( 'License' === $matches['key'] ) {
$license_uri = preg_replace( '/\[.+?]\((.+?)\)/', '$1', $matches['value'] );

// Extract License URI from link.
if ( 'License' === $matches['key'] ) {
$license_uri = preg_replace( '/\[.+?]\((.+?)\)/', '$1', $matches['value'] );
if ( 0 !== strpos( $license_uri, 'http' ) ) {
if ( $print_errors ) {
fwrite( STDERR, "Unable to extract License URI from: $meta.\n" );
}
return false;
}

if ( 0 !== strpos( $license_uri, 'http' ) ) {
fwrite( STDERR, "Unable to extract License URI from: $meta.\n" );
exit( __LINE__ );
$metadata['License URI'] = $license_uri;
}
}

$metadata['License URI'] = $license_uri;
$expected_metadata = array(
'Contributors',
'Tags',
'Requires at least',
'Tested up to',
'Stable tag',
'License',
'License URI',
'Requires PHP',
);
foreach ( $expected_metadata as $key ) {
if ( empty( $metadata[ $key ] ) ) {
if ( $print_errors ) {
fwrite( STDERR, "Failed to parse metadata. Missing: $key\n" );
}
return false;
}
}
}

$expected_metadata = array(
'Contributors',
'Tags',
'Requires at least',
'Tested up to',
'Stable tag',
'License',
'License URI',
'Requires PHP',
);
foreach ( $expected_metadata as $key ) {
if ( empty( $metadata[ $key ] ) ) {
fwrite( STDERR, "Failed to parse metadata. Missing: $key\n" );
exit( __LINE__ );
$replaced = "$header\n";
foreach ( $metadata as $key => $value ) {
$replaced .= "$key: $value\n";
}
$replaced .= "\n$description\n\n";

return $replaced;
},
$readme_txt
);

// Convert markdown headings into WP readme headings for good measure.
$readme_txt = preg_replace_callback(
'/^(#+)\s(.+)/m',
static function ( $matches ) use ( $print_errors ) {
$md_heading_level = strlen( $matches[1] );
$heading_text = $matches[2];

// #: ===
// ##: ==
// ###: =
$txt_heading_level = 4 - $md_heading_level;
if ( $txt_heading_level <= 0 ) {
if ( $print_errors ) {
fwrite( STDERR, "Heading too small to transform: {$matches[0]}.\n" );
}
return false;
}
}

$replaced = "$header\n";
foreach ( $metadata as $key => $value ) {
$replaced .= "$key: $value\n";
}
$replaced .= "\n$description\n\n";

return $replaced;
},
$readme_txt
);

// Convert markdown headings into WP readme headings for good measure.
$readme_txt = preg_replace_callback(
'/^(#+)\s(.+)/m',
static function ( $matches ) {
$md_heading_level = strlen( $matches[1] );
$heading_text = $matches[2];

// #: ===
// ##: ==
// ###: =
$txt_heading_level = 4 - $md_heading_level;
if ( $txt_heading_level <= 0 ) {
fwrite( STDERR, "Heading too small to transform: {$matches[0]}.\n" );
exit( __LINE__ );
return sprintf(
'%1$s %2$s %1$s',
str_repeat( '=', $txt_heading_level ),
$heading_text
);
},
$readme_txt,
-1,
$replace_count
);

if ( 0 === $replace_count ) {
if ( $print_errors ) {
fwrite( STDERR, "Unable to transform headings.\n" );
}
return false;
}

return sprintf(
'%1$s %2$s %1$s',
str_repeat( '=', $txt_heading_level ),
$heading_text
);
},
$readme_txt,
-1,
$replace_count
);

if ( 0 === $replace_count ) {
fwrite( STDERR, "Unable to transform headings.\n" );
exit( __LINE__ );
}

// Convert Youtube video links to just a URL since the WordPress plugin directory will display them.
$readme_txt = preg_replace(
'#^\[!\[[^\]]+\]\([^\)]+\)\]\((https://www.youtube.com/[^\)]+|https://youtu.be/[^\)]+)\)#m',
'$1',
$readme_txt
);
// Convert Youtube video links to just a URL since the WordPress plugin directory will display them.
$readme_txt = preg_replace(
'#^\[!\[[^\]]+\]\([^\)]+\)\]\((https://www.youtube.com/[^\)]+|https://youtu.be/[^\)]+)\)#m',
'$1',
$readme_txt
);

if ( ! file_put_contents( __DIR__ . '/../readme.txt', $readme_txt ) ) {
fwrite( STDERR, "Failed to write readme.txt.\n" );
exit( __LINE__ );
return $readme_txt;
}

fwrite( STDOUT, "Validated README.md and generated readme.txt\n" );
67 changes: 67 additions & 0 deletions bin/generate-plugins-json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* A script to generate a plugins.json to be used for the update functionality of the friends plugin.
*
* @package Friends
*/

$json = array();
foreach ( glob( __DIR__ . '/../../friends-*', GLOB_ONLYDIR ) as $dir ) {
$slug = basename( $dir );
if ( ! file_exists( "$dir/$slug.php" ) ) {
continue;
}

preg_match( '/^\s*\*\s+Version:\s*(.*)$/mi', file_get_contents( "$dir/$slug.php" ), $version );
if ( ! $version ) {
continue;
}
$version = $version[1];

$readme_md = file_get_contents( "$dir/README.md" );

$headline = strtok( $readme_md, PHP_EOL );
$short_description_start = 2 + strlen( $headline );

$name = trim( $headline, ' #' );

$data = array(
'name' => $name,
'short_description' => substr( $readme_md, $short_description_start, strpos( $readme_md, PHP_EOL, $short_description_start ) - $short_description_start ),
'more_info' => 'https://github.com/akirk/' . $slug,
'slug' => $slug,
'version' => $version,
);

$data['name'] = trim( strtok( $readme_md, PHP_EOL ), ' #' );

$data['trunk'] = "https://github.com/akirk/$slug/$slug.$version.zip";
$data['download_link'] = $data['trunk'];
$data['last_updated'] = gmdate( 'Y-m-d', exec( 'git --git-dir=' . $dir . '/.git/ log -1 --format=%ct ' ) );
$data['sections'] = array();

foreach ( explode( PHP_EOL . '## ', $readme_md ) as $i => $section ) {
if ( 0 === $i ) {
continue;
}
$title = strtok( $section, PHP_EOL );
$data['sections'][ $title ] = trim( substr( $section, strlen( $title ) ) );
}
$json[ $slug ] = $data;
}
file_put_contents( __DIR__ . '/../plugins.json', json_encode( $json, JSON_PRETTY_PRINT ) );
echo 'plugins.json was created.', PHP_EOL;

/**
* A simple function to convert Markdown to HTML.
*
* @param string $md The Markdown content.
*
* @return string The HTML.
*/
function simple_convert_markdown( $md ) {
$html = $md;
$html = preg_replace( '/^# (.*)$/m', '<h2>$1</h2>', $html );
$html = preg_replace( '/\[([^\]]*)\]\(([^)]*)\)/', '<a href="$2">$1</a>', $html );
return preg_replace( '/\n+/', '<br/>\n', $html );
}
Loading

0 comments on commit bff1e6d

Please sign in to comment.