update_readme.pl 2.8 KB

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