https://github.com/colinodell' introduced a PHP library called Indentation, that can be utilized to detect and manipulate the indentation of files and strings. Detecting the indentation of a string or file use ColinODell\Indentation\Indentation; $indentation = Indentation::detect(file_get_contents('composer.json')); assert($indentation->getAmount() === 4); assert($indentation->getType() === Indentation::TYPE_SPACE); assert((string)$indentation === ' '); Changing the indentation of a string or file use ColinODell\Indentation\Indentation; $composerJson = file_get_contents('composer.json'); $composerJson = Indentation::change($composerJson, new Indentation(1, Indentation::TYPE_TAB)); file_put_contents('composer.json', $composerJson); Adding leading indentation to all lines
use ColinODell\Indentation\Indentation; $codeExample = file_get_contents('file.php'); $indented = Indentation::indent($codeExample, new Indentation(4, Indentation::TYPE_SPACE)); Now you can embed the https://spec.commonmark.org/0.30/#indented-code-blocks into a Markdown document!
Imagine you have a file where every line is indented by at least 4 spaces: /** * Just a silly example */ class Cat extends Animal { //... }
Note how the leading 4 spaces are removed but all other indentation (like in the docblock and method body) is preserved.