Skip to content

Commit

Permalink
update to latest class-parser.php
Browse files Browse the repository at this point in the history
which fixes some parsing issues.
  • Loading branch information
afragen committed Aug 29, 2016
1 parent 2886b5f commit a917f0d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 66 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
@@ -1,7 +1,7 @@
#### [unreleased]
* added `Refresh Transients` button to Settings page because the `Check Again` button is going away
* added `redirect_on_save()` for Settings page
* switched to modified version of [wp.org plugin readme parser](https://meta.trac.wordpress.org/browser/sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/readme/class-parser.php), now accepts _Markdownified_ readme.txt files
* switched to slightly modified version of [wp.org plugin readme parser](https://meta.trac.wordpress.org/browser/sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/readme/class-parser.php), now accepts _Markdownified_ readme.txt files
* fixed re-activation of RESTful plugin update, multisite vs single site
* when creating Settings page, check current Plugin/Theme class instance, not transient. Fixes issue where remote install of private repo not having private settings saved.
* fixed PHP errors in Settings page
Expand Down
2 changes: 1 addition & 1 deletion github-updater.php
Expand Up @@ -12,7 +12,7 @@
* Plugin Name: GitHub Updater
* Plugin URI: https://github.com/afragen/github-updater
* Description: A plugin to automatically update GitHub, Bitbucket, or GitLab hosted plugins and themes. It also allows for remote installation of plugins or themes into WordPress.
* Version: 5.5.0.21
* Version: 5.5.0.22
* Author: Andy Fragen
* License: GNU General Public License v2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down
18 changes: 0 additions & 18 deletions src/GitHub_Updater/Readme_Parser.php
Expand Up @@ -71,22 +71,4 @@ protected function sanitize_contributors( $users ) {
return $users;
}

/**
* @access protected
*
* @param string|array $text
*
* @return string
*/
protected function sanitize_text( $text ) { // not fancy
if ( is_array( $text ) ) {
$text = implode( "\n", $text );
}
$text = strip_tags( $text );
$text = esc_html( $text );
$text = trim( $text );

return $text;
}

}
134 changes: 88 additions & 46 deletions vendor/class-parser.php
@@ -1,10 +1,5 @@
<?php

/**
* Minor modifications by Andy Fragen.
* Needed to account for changelog elements.
*/

/**
* WordPress.org Plugin Readme Parser.
*
Expand Down Expand Up @@ -69,6 +64,11 @@ class Parser {
*/
public $screenshots = array();

/**
* @var array
*/
public $faq = array();

/**
* These are the readme sections that we expect.
*
Expand Down Expand Up @@ -125,11 +125,16 @@ public function __construct( $file ) {
/**
* @param string $file
*
* @return bool|object
* @return bool
*/
protected function parse_readme( $file ) {

/**
* Mod for GitHub Updater.
*/
//$contents = file_get_contents( $file );
$contents = $file;

$contents = preg_split( '!\R!', $contents );
$contents = array_map( array( $this, 'strip_newlines' ), $contents );

Expand Down Expand Up @@ -268,19 +273,6 @@ protected function parse_readme( $file ) {
}
continue;
}
/**
* Add changelog elements account for older style `= 1.0 =`
*
* @author Andy Fragen
*/
if ( 'changelog' === $section_name ) {
if ( '=' === $line[0] ) {
$current .= '<h4>' . trim( $line, "= \t" ) . '</h4>';
continue;
}
$current .= "\n" . $line . "\n";
continue;
}

$current .= $line . "\n";
}
Expand All @@ -294,7 +286,7 @@ protected function parse_readme( $file ) {

// Use the first line of the description for the short description if not provided.
if ( empty( $this->short_description ) && ! empty( $this->sections['description'] ) ) {
$this->short_description = array_filter( explode( "\n", $this->sections['description'] ) );
$this->short_description = array_filter( explode( "\n", $this->sections['description'] ) )[0];
}

// Use the short description for the description section if not provided.
Expand All @@ -304,36 +296,21 @@ protected function parse_readme( $file ) {

// Parse out the Upgrade Notice section into it's own data.
if ( isset( $this->sections['upgrade_notice'] ) ) {
$lines = explode( "\n", $this->sections['upgrade_notice'] );
$version = null;
$current = '';
while ( ( $line = array_shift( $lines ) ) !== null ) {
$trimmed = trim( $line );
if ( empty( $trimmed ) ) {
continue;
}

if ( '=' === $trimmed[0] || '#' === $trimmed[0] ) {
if ( ! empty( $current ) ) {
$this->upgrade_notice[ $version ] = $this->sanitize_text( trim( $current ) );
}

$current = '';
$version = trim( $line, "#= \t" );
continue;
}

$current .= $line . "\n";
}
if ( ! empty( $version ) && ! empty( $current ) ) {
$this->upgrade_notice[ $version ] = $this->sanitize_text( trim( $current ) );
}
$this->upgrade_notice = $this->parse_section( $this->sections['upgrade_notice'] );
$this->upgrade_notice = array_map( array( $this, 'sanitize_text' ), $this->upgrade_notice );
unset( $this->sections['upgrade_notice'] );
}

// Display FAQs as a definition list.
if ( isset( $this->sections['faq'] ) ) {
$this->faq = $this->parse_section( $this->sections['faq'] );
$this->sections['faq'] = '';
}

// Markdownify!
$this->sections = array_map( array( $this, 'parse_markdown' ), $this->sections );
$this->upgrade_notice = array_map( array( $this, 'parse_markdown' ), $this->upgrade_notice );
$this->faq = array_map( array( $this, 'parse_markdown' ), $this->faq );

// Sanitize and trim the short_description to match requirements.
$this->short_description = $this->sanitize_text( $this->short_description );
Expand All @@ -352,11 +329,26 @@ protected function parse_readme( $file ) {
unset( $this->sections['screenshots'] );
}

if ( ! empty( $this->faq ) ) {
// If the FAQ contained data we couldn't parse, we'll treat it as freeform and display it before any questions which are found.
if ( isset( $this->faq[''] ) ) {
$this->sections['faq'] .= $this->faq[''];
unset( $this->faq[''] );
}

if ( $this->faq ) {
$this->sections['faq'] .= "\n<dl>\n";
foreach ( $this->faq as $question => $answer ) {
$this->sections['faq'] .= "<dt>{$question}</dt>\n<dd>{$answer}</dd>\n";
}
$this->sections['faq'] .= "\n</dl>\n";
}
}

// Filter the HTML.
$this->sections = array_map( array( $this, 'filter_text' ), $this->sections );

//return true;
return $this;
return true;
}

/**
Expand Down Expand Up @@ -436,6 +428,9 @@ protected function filter_text( $text ) {
'strong' => true,
'ul' => true,
'ol' => true,
'dl' => true,
'dt' => true,
'dd' => true,
'li' => true,
'h3' => true,
'h4' => true,
Expand Down Expand Up @@ -520,6 +515,53 @@ protected function sanitize_stable_tag( $stable_tag ) {
return $stable_tag;
}

/**
* Parses a slice of lines from the file into an array of Heading => Content.
*
* We assume that every heading encountered is a new item, and not a sub heading.
* We support headings which are either `= Heading`, `# Heading` or `** Heading`.
*
* @param string|array $lines The lines of the section to parse.
*
* @return array
*/
protected function parse_section( $lines ) {
$key = $value = '';
$return = array();

if ( ! is_array( $lines ) ) {
$lines = explode( "\n", $lines );
}

while ( ( $line = array_shift( $lines ) ) !== null ) {
$trimmed = trim( $line );
if ( ! $trimmed ) {
$value .= "\n";
continue;
}

// Normal headings (##.. == ... ==) are matched if they exist, Bold is only used if it starts and ends the line.
if ( $trimmed[0] == '#' || $trimmed[0] == '=' || ( substr( $trimmed, 0, 2 ) == '**' && substr( $trimmed, - 2 ) == '**' ) ) {
if ( $value ) {
$return[ $key ] = trim( $value );
}

$value = '';
// Trim off the first character of the line, as we know that's the heading style we're expecting to remove.
$key = trim( $line, $trimmed[0] . " \t" );
continue;
}

$value .= $line . "\n";
}

if ( $key || $value ) {
$return[ $key ] = trim( $value );
}

return $return;
}

/**
* @param string $text
*
Expand Down

0 comments on commit a917f0d

Please sign in to comment.