Skip to content

Instantly share code, notes, and snippets.

@wouterj
Last active August 29, 2015 14:01
Show Gist options
  • Save wouterj/fa6fb71d23d703cac7b9 to your computer and use it in GitHub Desktop.
Save wouterj/fa6fb71d23d703cac7b9 to your computer and use it in GitHub Desktop.
<?php
require_once __DIR__.'/../vendor/autoload.php';
use Gnugat\Redaktilo\Filesystem;
use Gnugat\Redaktilo\Editor;
use Gnugat\Redaktilo\File;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
$symfonyFilesystem = new SymfonyFilesystem();
$filesystem = new Filesystem($symfonyFilesystem);
$editor = new Editor($filesystem);
$files = new \RegexIterator(
new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(__DIR__.'/../')
), '/\.rst(?:\.inc)?$/',
\RegexIterator::MATCH
);
foreach ($files as $filename) {
$file = $editor->open($filename);
try {
remove_dollars($editor, $file);
} catch (\Exception $e) {
}
echo $filename, PHP_EOL;
$editor->save($file);
}
function remove_dollars(Editor $editor, File $file) {
$editor->jumpDownTo($file, '.. code-block:: bash');
$lines = $file->readlines();
$editor->moveDown($file);
while ((bool) preg_match('/^ /', $line = $lines[$file->getCurrentLineNumber()]) || '' === $line) {
if ('' !== $line) {
$editor->replaceWith($file, '/\$ /', '');
}
$editor->moveDown($file);
}
remove_dollars($editor, $file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment