|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Compare generated markdown documentation with existing files. |
| 4 | + * |
| 5 | + * @package wordpress/secure-custom-fields |
| 6 | + */ |
| 7 | + |
| 8 | +namespace WordPress\SCF\Docs; |
| 9 | + |
| 10 | +// phpcs:disable WordPress.WP.AlternativeFunctions |
| 11 | + |
| 12 | +/** |
| 13 | + * Normalize markdown content for comparison by removing formatting. |
| 14 | + * |
| 15 | + * @param string $content The markdown content to normalize. |
| 16 | + * @return string Normalized content. |
| 17 | + */ |
| 18 | +function normalize_content( $content ) { |
| 19 | + // Remove URL formatting |
| 20 | + $content = preg_replace( '/<(https?:\/\/[^>]+)>/', '$1', $content ); |
| 21 | + // Normalize list markers |
| 22 | + $content = preg_replace( '/^[*-]\s+/m', '- ', $content ); |
| 23 | + // Normalize whitespace |
| 24 | + $content = preg_replace( '/\s+/', ' ', $content ); |
| 25 | + return trim( $content ); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Compare two directories of markdown files. |
| 30 | + * |
| 31 | + * @param string $dir1 First directory path. |
| 32 | + * @param string $dir2 Second directory path. |
| 33 | + * @return bool True if content matches, false otherwise. |
| 34 | + */ |
| 35 | +function compare_dirs( $dir1, $dir2 ) { |
| 36 | + $files1 = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $dir1 ) ); |
| 37 | + $files2 = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( $dir2 ) ); |
| 38 | + |
| 39 | + foreach ( $files1 as $file1 ) { |
| 40 | + if ( $file1->isFile() && 'md' === $file1->getExtension() ) { |
| 41 | + $relative_path = str_replace( $dir1 . '/', '', $file1->getPathname() ); |
| 42 | + $file2 = $dir2 . '/' . $relative_path; |
| 43 | + |
| 44 | + if ( ! file_exists( $file2 ) ) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + $content1 = normalize_content( file_get_contents( $file1 ) ); |
| 49 | + $content2 = normalize_content( file_get_contents( $file2 ) ); |
| 50 | + |
| 51 | + if ( $content1 !== $content2 ) { |
| 52 | + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 53 | + echo "Content mismatch in {$relative_path}\n"; |
| 54 | + return false; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +$result = compare_dirs( 'docs/code-reference', 'temp/code-reference' ); |
| 62 | +exit( $result ? 0 : 1 ); |
0 commit comments