Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Patch *.md files from Contao's hook documentation to match the ATX style
#!/usr/bin/perl
use strict;
use warnings;
my $dir = 'docs/api/hooks';
my $patcheddir = "hooks_patched";
opendir(DIR, $dir) || die "Can't open directory: $!\n";
while (my $file = readdir(DIR)) {
next if $file =~ /^\.+$/;
patch_md_file($file);
}
closedir(DIR);
sub patch_md_file {
my $file = shift;
print "file='$file'\n";
my $lastrow;
my $currentrow;
open(my $fh_in, '<:encoding(UTF-8)', "$dir/$file") or die "Could not <open file $dir/$file $!";
open(my $fh_out, '>:encoding(UTF-8)', "$patcheddir/$file") or die "Could not >open $patcheddir/$file $!";
while (my $row = <$fh_in>) {
chomp $row;
$lastrow = $currentrow;
$currentrow = $row;
# h1 mit "Unterstreichung" zu h1 mit '#'
if ($currentrow =~ /^-+$/) {
$lastrow = "# $lastrow";
$currentrow = '';
}
# sollte eigentlich h2 mit '##' sein, ist aber h3 mit '### text ###'
if ($currentrow =~ /###([^#]+)###/) {
$currentrow = "##$1";
}
# '## More Information' ## vor '## References' einfügen
if ($currentrow =~ /^## References/) {
$currentrow = "## More Information\n\n\n### References";
}
# '## See Also' wieder zu einem h3 machen
if ($currentrow =~ /## See Also/) {
$currentrow = "### See Also";
}
print $fh_out "$lastrow\n" unless not defined $lastrow;
}
print $fh_out "$currentrow\n" unless not defined $currentrow;
close($fh_in);
close($fh_out);
}
## EOF ##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment