Skip to content

Commit

Permalink
Add table of contents generation function to ReadmeTest
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
SmetDenis committed Apr 3, 2024
1 parent 03152da commit 98a8d4e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.


<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

<!-- auto-update:toc -->
- [Introduction](#introduction)
- [Why?](#why)
- [Features](#features)
Expand All @@ -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)
Expand All @@ -52,8 +48,8 @@ specifications, making it invaluable in scenarios where data quality and consist
- [Contributing](#contributing)
- [License](#license)
- [See Also](#see-also)
<!-- auto-update:/toc -->

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Introduction

Expand Down
34 changes: 34 additions & 0 deletions tests/ReadmeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 98a8d4e

Please sign in to comment.