-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtransform-p3-scripts
114 lines (92 loc) · 2.31 KB
/
transform-p3-scripts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#
# Read the p3-scripts list; pull the data from github;
# transform to restructured text and place into the correct location.
#
use strict;
use File::Slurp;
use File::Temp;
use Data::Dumper;
use LWP::UserAgent;
use Pod::POM;
use Pod::POM::View::Restructured;
use File::Basename;
my %link_map = ('P3Utils/ih_options' => ":ref:`cli-input-options`",
'P3Utils::ih_options' => ":ref:`cli-input-options`",
'P3Utils/data_options' => ":ref:`cli-data-options`",
'P3Utils/col_options' => ":ref:`cli-column-options`",
'P3Utils/delim_options' => ":ref:`cli-delimiter-options`",
);
my $ua = LWP::UserAgent->new;
my $url_top = "https://raw.githubusercontent.com/SEEDtk/RASTtk/master";
my $out_base = "../docroot/cli_tutorial/command_list";
my %unmapped_links;
#
# read the module list first to find the moduels we can link to.
#
my %modules;
open(M, "<", "p3-module-list.txt") or die "cannot read p3-module-list.txt: $!";
while (<M>)
{
chomp;
s/\.pm$//;
$modules{$_} = 1;
}
close(M);
transform("p3-script-list.txt", "$url_top/scripts");
transform("p3-module-list.txt", "$url_top/lib");
if (%unmapped_links)
{
print STDERR "Unmapped links:\n";
for my $l (sort keys %unmapped_links)
{
print STDERR "\t$l\n";
}
}
sub transform
{
my($file, $url_base) = @_;
open(S, "<", $file) or die "Cannot open $file $!";
my $conv = Pod::POM::View::Restructured->new(); # {namespace => 'cli'});
while (my $script = <S>)
{
chomp $script;
print "$script\n";
my $url = "$url_base/$script";
my $res = $ua->get($url);
if (!$res->is_success)
{
die "Error " . $res->code . " fetching $url: " . $res->content;
}
my $txt = $res->content;
my $tmp = File::Temp->new();
print $tmp $txt;
close($tmp);
my $base = basename($script, ".pl", ".pm");
my $out_file = "$out_base/$base.rst";
open(my $fh, ">", $out_file) or die "Cannot write $out_file: $!";
print $fh ".. _cli::$base:\n\n";
$conv->convert_file("$tmp", $base, $fh, { link => \&handle_link });
}
}
sub handle_link
{
my($txt) = @_;
my $link = $link_map{$txt};
if (defined($link))
{
return ('', $link);
}
elsif ($modules{$txt})
{
return ('', ":ref:`cli::$txt`");
}
elsif ($txt =~ /^(p3.*)\.pl$/)
{
return ('', ":ref:`cli::$1`");
}
else
{
$unmapped_links{$txt}++;
return $txt;
}
}