wikify individual files
[freeside.git] / bin / pod2x
1 #!/usr/bin/perl -w
2
3 use strict;
4 use WWW::Mediawiki::Client;
5 #sub WWW::Mediawiki::Client::pagename_to_url {
6 #    my ($self, $name, $action) = @_;
7 #    WWW::Mediawiki::Client::URLConstructionException->throw(
8 #            error => 'No action supplied.',
9 #        ) unless $action;
10 #    WWW::Mediawiki::Client::URLConstructionException->throw(
11 #            error => "Page name $name ends with '.wiki'.",
12 #        ) if $name =~ /.wiki$/;
13 #    my $char = $self->space_substitute;
14 #    $name =~ s/ /$char/;
15 #    my $lang = $self->language_code;
16 #    my $host = $self->host;
17 #    $host =~ s/__LANG__/$lang/g;
18 #    my $wiki_path = $self->wiki_path;
19 #    $wiki_path =~ s/__LANG__/$lang/g;
20 #    my $protocol = $self->protocol;
21 #    return "$protocol://$host/$wiki_path?" . ACTION . "=$action&" . TITLE . "=$name" . '&wpRecreate=1';
22 #}
23
24 my $mw_username = 'ivan';
25 chomp( my $mw_password = `cat .mw-password` );
26
27 my $site_perl = "./FS";
28 #my $html = "Freeside:1.7:Documentation:Developer";
29 my $html = "Freeside:1.9:Documentation:Developer";
30
31 foreach my $dir (
32   $html,
33   map "$html/$_", qw( bin FS FS/UI FS/part_export FS/part_pkg
34                       FS/part_event FS/part_event/Condition FS/part_event/Action
35                       FS/ClientAPI FS/Cron FS/Misc FS/Report FS/Report/Table
36                       FS/TicketSystem FS/UI
37                       FS/SelfService
38                     )
39 ) {
40   -d $dir or mkdir $dir;
41 }
42
43 $|=1;
44
45 die "Can't find $site_perl" unless -d $site_perl;
46 #die "Can't find $catman" unless -d $catman;
47 -d $html or mkdir $html;
48
49 my $count = 0;
50
51 #make some useless links
52 foreach my $file (
53   glob("$site_perl/bin/freeside-*"),
54 ) {
55   next if $file =~ /\.pod$/;
56   #symlink $file, "$file.pod"; # or die "link $file to $file.pod: $!";
57   #system("cp $file $file.pod");
58   -e "$file.pod" or system("cp $file $file.pod");
59 }
60
61 my $mvs = WWW::Mediawiki::Client->new(
62             'host'           => 'www.sisd.com',
63             'wiki_path'      => 'mediawiki/index.php',
64             'username'       => $mw_username,
65             'password'       => $mw_password,
66             #'commit_message' => 'import from POD'
67           );
68
69 $mvs->do_login;
70
71 my @files;
72 if ( @ARGV ) {
73   @files = @ARGV;
74 } else {
75   @files =
76     glob("$site_perl/*.pm"),
77     glob("$site_perl/*/*.pm"),
78     glob("$site_perl/*/*/*.pm"),
79     glob("$site_perl/*/*/*/*.pm"),
80     glob("$site_perl/bin/*.pod"),
81     glob("./fs_selfservice/FS-SelfService/*.pm"),
82     glob("./fs_selfservice/FS-SelfService/*/*.pm"),
83   ;
84 }
85
86 foreach my $file (@files) {
87   next if $file =~ /(^|\/)blib\//;
88   next if $file =~ /(^|\/)CVS\//;
89   #$file =~ /\/([\w\-]+)\.pm$/ or die "oops file $file";
90   my $name;
91   if ( $file =~ /fs_\w+\/FS\-\w+\/(.*)\.pm$/ ) {
92     $name = "FS/$1";
93   } elsif ( $file =~ /$site_perl\/(.*)\.(pm|pod)$/ ) {
94     $name = $1;
95   } else {
96     die "oops file $file";
97   }
98
99   #exit if $count++ == 10;
100
101   my $htmlroot = join('/', map '..',1..(scalar($file =~ tr/\///)-2)) || '.';
102
103   system "pod2wiki  --style mediawiki $file >$html/$name.rawwiki";
104
105   if ( -e "$html/$name.rawwiki" ) {
106     print "processing $name\n";
107   } else {
108     print "skipping $name\n";
109     next;
110   };
111
112   $mvs->do_update("$html/$name.wiki");
113
114   open(RAW, "<$html/$name.rawwiki") or die $!;
115   open(WIKI,">$html/$name.wiki"   ) or die $!;
116   while (<RAW>) {
117     s/\[\[([^#p][^\]]*)\]\]/"[[$html\/". w_e($1). "|$1]]"/ge;
118     print WIKI $_;
119   }
120   close RAW;
121   close WIKI;
122
123   print "  uploading to ". $mvs->filename_to_pagename("$html/$name.wiki"). "\n";
124   $mvs->commit_message( 'import from POD' );
125   $mvs->do_commit("$html/$name.wiki");
126
127 }
128
129 sub w_e {
130   my $s = shift;
131   $s =~ s/_/ /g;
132   $s =~ s/::/\//g;
133   $s =~ s/^freeside-/bin\/freeside-/g;
134   $s;
135 }
136
137
138 ##  system "pod2text $file >$catman/$name.txt"; 
139 ##
140 #  system "pod2html --podroot=$site_perl --podpath=./FS:./FS/UI:.:./bin --norecurse --htmlroot=$htmlroot $file >$html/$name.html";
141 #  #system "pod2html --podroot=$site_perl --htmlroot=$htmlroot $file >$html/$name.html";
142 ##  system "pod2html $file >$html/$name.html";
143 ##
144
145 #remove the useless links
146 unlink glob("$site_perl/bin/*.pod");
147