From e73d754bf107a3af38ba9e0a2cd7c912943b61ca Mon Sep 17 00:00:00 2001 From: Denis Smetannikov Date: Wed, 3 Apr 2024 19:28:30 +0400 Subject: [PATCH] Add table of contents generation for READNE.md (#135) A new method named `testGenerateTOC()` has been added to the ReadmeTest.php file. This function generates a Table of Contents for the README.md file by parsing section headers. The purpose is to improve usability and navigation within the README file. --- README.md | 8 ++------ tests/ReadmeTest.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9b942696..84a7cc1e 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,7 @@ This utility facilitates automated checks to verify that the structure and conte specifications, making it invaluable in scenarios where data quality and consistency are critical. - - - + - [Introduction](#introduction) - [Why?](#why) - [Features](#features) @@ -42,8 +40,6 @@ specifications, making it invaluable in scenarios where data quality and consist - [Complete CLI Help Message](#complete-cli-help-message) - [Report Examples](#report-examples) - [Benchmarks](#benchmarks) - - [Profiles](#profiles) - - [Divisions](#divisions) - [Brief Conclusions](#brief-conclusions) - [Examples of CSV Files](#examples-of-csv-files) - [Run benchmark locally](#run-benchmark-locally) @@ -52,8 +48,8 @@ specifications, making it invaluable in scenarios where data quality and consist - [Contributing](#contributing) - [License](#license) - [See Also](#see-also) + - ## Introduction diff --git a/tests/ReadmeTest.php b/tests/ReadmeTest.php index d3c467e3..189bc1ef 100644 --- a/tests/ReadmeTest.php +++ b/tests/ReadmeTest.php @@ -17,6 +17,7 @@ namespace JBZoo\PHPUnit; use JBZoo\Utils\Cli; +use JBZoo\Utils\Str; use Symfony\Component\Console\Input\StringInput; use function JBZoo\Data\yml; @@ -260,4 +261,37 @@ public function testBenchmarkTable(): void Tools::insertInReadme('benchmark-table', \implode("\n", $output)); } + + public function testGenerateTOC(): void + { + $markdown = \file_get_contents(PROJECT_ROOT . '/README.md'); + + // Split the content by code block delimiters + $splitContent = \preg_split('/(```.*?```)/s', $markdown, -1, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE); + + $toc = ''; + $currentContent = ''; + + foreach ($splitContent as $section) { + // If the section is a code block, skip it + if (\preg_match('/^```/', $section)) { + continue; + } + + $currentContent .= $section; + // Match headers outside of code blocks + \preg_match_all('/^(#{2,6})\s*(.*)$/m', $currentContent, $matches, \PREG_SET_ORDER); + + foreach ($matches as $match) { + $level = \strlen($match[1]) - 2; // Subtract 1 to start level from 0 + $title = \trim($match[2]); + $slug = Str::slug($title); + $toc .= \str_repeat(' ', $level) . "- [{$title}](#{$slug})\n"; + } + // Reset the current content to prevent duplicate entries + $currentContent = ''; + } + + Tools::insertInReadme('toc', $toc); + } }