Last active
August 29, 2015 14:23
-
-
Save fiedsch/9a76e2410202ff4b30c7 to your computer and use it in GitHub Desktop.
Patch *.md files from Contao's hook documentation to match the ATX style
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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