Completed
Push — master ( cfb08a...968302 )
by Stéphane
02:10
created

readme-builder.php ➔ parse_markdown()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 22
nc 6
nop 1
dl 0
loc 28
ccs 0
cts 20
cp 0
crap 56
rs 6.7272
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 47.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * This file is a readme.txt builder used to publish updates on WordPress repository.
4
 * It avoid duplicating README.md files and ensure simplicity on Github.
5
 *
6
 * @package wp-mautic
7
 */
8
9
// -----------------------------------------------------------------------------
10
/**
11
 * Markdown parser helper
12
 *
13
 * @param  string $file
14
 * @return array
15
 */
16
function parse_markdown( string $file ) :array {
17
	$source = fopen( $file, 'r' );
18
	$content = [];
19
	$current = 'default';
20
	$complement = '';
21
	$level = 0;
22
	while ( ! feof( $source ) ) {
23
		$line = trim( fgets( $source ) );
24
		if ( '' === $line ) {
25
			continue;
26
		}
27
		if ( strpos( $line, '#' ) === 0 ) {
28
			$tmp = strpos( $line, ' ' );
29
			if ( $level > 1 && $tmp > $level ) {
30
				$complement = $current . '{}';
31
			} elseif ( $tmp < $level ) {
32
				$complement = '';
33
			}
34
			$current = $complement . trim( substr( $line, $tmp ) );
35
			$content[ $current ] = '';
36
			$level = $tmp;
37
		} else {
38
			$content[ $current ] .= $line . PHP_EOL;
39
		}
40
	}
41
42
	return $content;
43
}
44
45
// -----------------------------------------------------------------------------
46
// readme.txt template
47
$template = <<<TXT
48
=== WP Mautic ===
49
Author: mautic
50
Tags: marketing, automation
51
%tags%
52
Donate link: http://mautic.org/
53
54
== Description ==
55
56
%heading%
57
58
## Key features
59
60
%key-features%
61
62
## Configuration
63
64
%configuration%
65
66
## Usage
67
68
%documentation%
69
70
== Installation ==
71
72
%installation%
73
74
== Upgrade Notice ==
75
76
%upgrade%
77
== Changelog ==
78
79
%changelog%
80
TXT;
81
82
// -----------------------------------------------------------------------------
83
// Extract package tag
84
$source = fopen( __DIR__ . '/../wpmautic.php', 'r' );
85
$tags = [];
86
while ( ! feof( $source ) ) {
87
	$line = fgets( $source );
88
	if ( " * @package wp-mautic\n" === $line ) {
89
		break;
90
	}
91
	if (
92
		0 === strpos( $line, ' * ' ) &&
93
		false === strpos( $line, 'Author:' ) &&
94
		false === strpos( $line, 'Plugin ' )
95
	) {
96
		$tags[] = str_replace( 'Version:', 'Stable tag:', trim( substr( $line, 3 ) ) );
97
	}
98
}
99
$tags = implode( PHP_EOL, $tags );
100
101
// -----------------------------------------------------------------------------
102
// Extract upgrade notices
103
$notices = [];
104
foreach ( parse_markdown( __DIR__ . '/../UPGRADE.md' ) as $key => $notice ) {
105
	if ( ! preg_match( '/^v.*/', $key ) ) {
106
		continue;
107
	}
108
109
	$notices[] = "= $key =\n$notice";
110
}
111
$notices = implode( PHP_EOL, $notices );
112
113
// -----------------------------------------------------------------------------
114
// Extract changelog
115
$changelog = '';
116
$title = '';
117
foreach ( parse_markdown( __DIR__ . '/../CHANGELOG.md' ) as $key => $notice ) {
118
	if ( ! preg_match( '/^\[v.*/', $key ) ) {
119
		continue;
120
	}
121
	if ( false === strpos( $key, '{}' ) ) {
122
		preg_match( '/\[(.*)\] - (.*)/', $key, $matches );
123
		$changelog .= "= {$matches[1]} =\n\nRelease date: {$matches[2]}\n\n";
124
	} else {
125
		$tmp = explode( '{}', $key );
126
		$changelog .= "* {$tmp[1]}\n" . str_replace( '- ', '  * ', $notice ) . PHP_EOL;
127
	}
128
}
129
130
// -----------------------------------------------------------------------------
131
// Export template
132
echo str_replace([
133
    '%tags%',
134
    '%changelog%',
135
    '%upgrade%',
136
], [
137
    $tags,
138
    $changelog,
139
    $notices
140
], $template);
141