aboutsummaryrefslogtreecommitdiffstats
path: root/readme_generator.pl
blob: 20e731293ec9a469880246850a84f0f5197b6a80 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env perl

use strict;
use warnings;

# Goal: extract the comments from the top of each script file, and
# turn them into some sort of markdown-style README.md for github to nom on.
#
# Not sure how it's going to work with multiple files in a dir though. Sections?


use File::Find;
use File::Spec;
use Pod::Markdown;

my @dirs = @ARGV // '.';

find(\&wanted, @dirs);

sub wanted {
    my ($file, $dir, $path) = ($_, $File::Find::dir, $File::Find::name);
    return unless $file =~ m/\.pl$/;

    _err("processing file: $path");
}

sub read_input_file {
    my ($path, $filename) = @_;

    my $filepath = File::Spec->catfile($path, $filename);

    open my $rfh, '<', $filepath or die "Couldn't open $filepath for output: $!";

    _err("reading $filepath");

    my $parser = Pod::Markdown->new;

    $parser->parse($rfh);

    close $rfh;

    create_output_file($path, 'README.md', $parser);
}

sub create_output_file {
    my ($path, $filename, $parser) = @_;

    my $filepath = File::Spec->catfile($path, $filename);

    _err("Writing to $filepath");

    open my $wfh, '>', $filepath or die "Couldn't open $filepath for output: $!";
    print $wfh $parser->as_markdown;
    close $wfh;
}

sub _err {
    my ($msg, @args) = @_;
    my $str = sprintf($msg, @args);
    print STDERR $str;
}