update_summary.pl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 24 April 2015
  5. # Website: https://github.com/trizen
  6. # Add a given directory to a given section in SUMMARY.md (for gitbooks)
  7. use 5.014;
  8. use strict;
  9. use autodie;
  10. use warnings;
  11. use Cwd qw(getcwd);
  12. use File::Basename qw(basename dirname);
  13. use File::Spec::Functions qw(rel2abs);
  14. sub add_section {
  15. my ($name, $section, $file) = @_;
  16. my ($before, $middle, $after);
  17. open my $fh, '<', $file;
  18. while (defined(my $line = <$fh>)) {
  19. if ($line =~ /^(\*\h+\Q$name\E)\h*$/ || $line =~ m{^(\*\h+\[\Q$name\E\](?:\(.*\))?)\h*$}) {
  20. $middle = "$1\n";
  21. say "** Found section: <<<$1>>>";
  22. while (defined(my $line = <$fh>)) {
  23. if ($line =~ /^\S/) {
  24. $after = $line;
  25. }
  26. }
  27. }
  28. else {
  29. if (defined $after) {
  30. $after .= $line;
  31. }
  32. else {
  33. $before .= $line;
  34. }
  35. }
  36. }
  37. close $fh;
  38. open my $out_fh, '>', $file;
  39. print {$out_fh} $before . $middle . $section . $after;
  40. close $out_fh;
  41. }
  42. my $summary_file = 'SUMMARY.md';
  43. my $main_dir = 'programming_tasks';
  44. my $section_name = 'Programming tasks';
  45. {
  46. my @root;
  47. sub make_section {
  48. my ($name, $dir, $spaces) = @_;
  49. my $cwd = getcwd();
  50. chdir $dir;
  51. my @files = map { {name => $_, path => rel2abs($_)} } glob('*'); # sorting for free
  52. chdir $cwd;
  53. my $make_section_url = sub {
  54. my ($name) = @_;
  55. join('/', basename($main_dir), @root, $name);
  56. };
  57. my %ignored;
  58. my $section = '';
  59. foreach my $file (@files) {
  60. my $title = $file->{name} =~ s/_/ /gr;
  61. if (-d $file->{path}) {
  62. if (-e "$file->{path}.md") {
  63. my $url_path = $make_section_url->("$file->{name}.md");
  64. $section .= (' ' x $spaces) . "* [\u$title]($url_path)\n";
  65. $ignored{"$file->{name}.md"}++; # ignore this file later
  66. }
  67. else {
  68. $section .= (' ' x $spaces) . "* $title\n";
  69. }
  70. push @root, $file->{name};
  71. $section .= make_section($file->{name}, $file->{path}, $spaces + 4);
  72. }
  73. else {
  74. next if $dir eq $main_dir;
  75. next if $ignored{$file->{name}};
  76. my $naked_name = $file->{name} =~ s/\.md\z//ir;
  77. my $naked_title = $title =~ s/\.md\z//ir;
  78. my $url_path = $make_section_url->($file->{name});
  79. $section .= (' ' x $spaces) . "* [\u$naked_title]($url_path)\n";
  80. }
  81. }
  82. pop @root;
  83. return $section;
  84. }
  85. }
  86. my $section = make_section($section_name, $main_dir, 3);
  87. my $section_content = add_section($section_name, $section, $summary_file);
  88. say "** All done!";