update_readme.pl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 24 April 2015
  5. # Website: https://github.com/trizen
  6. # Updated the README.md file by adding new scripts to the summary.
  7. use 5.016;
  8. use strict;
  9. use autodie;
  10. use warnings;
  11. use Cwd qw(getcwd);
  12. use File::Spec::Functions qw(rel2abs curdir);
  13. use File::Basename qw(basename dirname);
  14. use URI::Escape qw(uri_escape);
  15. my %ignore;
  16. if (open my $fh, '<:utf8', '.gitignore') {
  17. while (<$fh>) {
  18. next if /^#/;
  19. chomp;
  20. if (-e $_) {
  21. $ignore{rel2abs($_)} = 1;
  22. }
  23. }
  24. close $fh;
  25. }
  26. sub add_section {
  27. my ($section, $file) = @_;
  28. my ($before, $middle);
  29. open my $fh, '<', $file;
  30. while (defined(my $line = <$fh>)) {
  31. if ($line =~ /^(#+\h*Summary\s*)$/) {
  32. $middle = "$1\n";
  33. last;
  34. }
  35. else {
  36. $before .= $line;
  37. }
  38. }
  39. close $fh;
  40. open my $out_fh, '>', $file;
  41. print {$out_fh} $before . $middle . $section;
  42. close $out_fh;
  43. }
  44. my $summary_file = 'README.md';
  45. my $main_dir = curdir();
  46. {
  47. my @root;
  48. sub make_section {
  49. my ($dir, $spaces) = @_;
  50. my $cwd = getcwd();
  51. chdir $dir;
  52. my @files = sort { $a->{key} cmp $b->{key} }
  53. map { {key => fc(s/\.\w+\z//r), name => $_, path => File::Spec->rel2abs($_)} } glob('*');
  54. chdir $cwd;
  55. my $make_section_url = sub {
  56. my ($name) = @_;
  57. join('/', basename($main_dir), @root, $name);
  58. };
  59. my $section = '';
  60. foreach my $file (@files) {
  61. my $title = $file->{name} =~ tr/_/ /r =~ s/ s /'s /gr;
  62. if ($file->{name} =~ /\.(\w{2,3})\z/) {
  63. next if $1 !~ /^(?:p[lm])\z/i;
  64. }
  65. next if exists $ignore{$file->{path}};
  66. if (-d $file->{path}) {
  67. $section .= (' ' x $spaces) . "* $title\n";
  68. push @root, $file->{name};
  69. $section .= make_section($file->{path}, $spaces + 4);
  70. }
  71. else {
  72. next if $dir eq $main_dir;
  73. my $naked_title = $title =~ s/\.pl\z//ri;
  74. my $url_path = uri_escape($make_section_url->($file->{name}), ' ?');
  75. $section .= (' ' x $spaces) . "* [\u$naked_title]($url_path)\n";
  76. }
  77. }
  78. pop @root;
  79. return $section;
  80. }
  81. }
  82. my $section = make_section($main_dir, 0);
  83. my $section_content = add_section($section, $summary_file);
  84. say "** All done!";