|
4 | 4 | use warnings;
|
5 | 5 | use v5.14;
|
6 | 6 | use Git::Hooks;
|
| 7 | +use File::Slurp qw(read_file write_file); |
| 8 | + |
| 9 | +my $layout_preffix=<<EOT; |
| 10 | +--- |
| 11 | +layout: index |
| 12 | +--- |
| 13 | +
|
| 14 | +EOT |
7 | 15 |
|
8 | 16 | POST_COMMIT {
|
9 | 17 | my ($git) = @_;
|
10 | 18 | my $branch = $git->command(qw/rev-parse --abbrev-ref HEAD/);
|
11 | 19 | if ( $branch =~ /master/ ) {
|
12 |
| - if ( $git->command(qw/show --name-status/) =~ /testeando.ad/ ) { |
13 |
| - #Now change branch and process |
14 |
| - #Inspired by http://stackoverflow.com/questions/15214762/how-can-i-sync-documentation-with-github-pages |
15 |
| - $git->command(qw/checkout gh-pages/); |
16 |
| - $git->command( 'checkout', 'master', '--', 'testeando.ad' ); |
17 |
| - `asciidoc --backend deckjs testeando.ad -o index.html`; |
18 |
| - $git->command('commit','-a', '-m', "Generando diapos en gh-pages"); |
| 20 | + my $changed = $git->command(qw/show --name-status/); |
| 21 | + my @changed_files = ($changed =~ /\s\w\s+(\S+)/g); |
| 22 | + my @mds = grep ( /\.md/, @changed_files ); |
| 23 | + #Now change branch and process |
| 24 | + #Inspired by http://stackoverflow.com/questions/15214762/how-can-i-sync-documentation-with-github-pages |
| 25 | + $git->command(qw/checkout gh-pages/); |
| 26 | + for my $f ( @mds ) { |
| 27 | + $git->command( 'checkout', 'master', '--', $f ); |
| 28 | + my $file_content = read_file( $f ); |
| 29 | + $file_content =~ s/\.md\)/\)/g; # Change links |
| 30 | + $file_content = $layout_preffix.$file_content; |
| 31 | + if ( $f ne 'README.md' ) { |
| 32 | + write_file($f, $file_content); |
| 33 | + $git->command('add', $f ); |
| 34 | + } else { |
| 35 | + write_file('index.md', $file_content ); |
| 36 | + $git->command('add', 'index.md' ); |
| 37 | + unlink('README.md'); |
19 | 38 | }
|
20 |
| - $git->command(qw/checkout master/); #back to original |
| 39 | + $git->command('commit','-a', '-m', "Sync $f from master to gh-pages"); |
| 40 | + say "Processing $f"; |
| 41 | + } |
| 42 | + $git->command(qw/checkout master/); #back to original branch |
21 | 43 | }
|
22 | 44 | };
|
23 | 45 |
|
24 | 46 | run_hook($0, @ARGV);
|
25 | 47 |
|
26 | 48 | =head1 NAME
|
27 | 49 |
|
28 |
| -git-hooks.pl - Genera diapos en otra rama. |
| 50 | +git-hooks.pl - post-commit hooks to sync markdown files with GitHub pages |
29 | 51 |
|
30 | 52 | =head2 SYNOPSIS
|
31 | 53 |
|
|
0 commit comments