rename to pod2diogenes
[diogenes.git] / scripts / pod2diogenes
1 #!/usr/bin/php
2 <?php
3 ini_set("include_path", "/etc/diogenes:/usr/share/diogenes/include:/usr/share/php");
4 require_once("diogenes.common.inc.php");
5 require_once("diogenes.script.inc.php");
6 require_once("System.php");
7 require_once("Barrel.php");
8
9 /** Import a single Perl POD file.
10  *
11  * @param $caller
12  * @param $pod
13  * @param $docdir
14  * @param $docbase
15  * @param $template
16  */
17 function importPod(&$caller, $pod, $docdir, $docbase, $template = '')
18 {
19   global $globals;
20   $barrel =& $caller->barrel;
21
22   $pid = $barrel->makePath($docdir, $caller);
23   $page = Diogenes_Barrel_Page::fromDb($barrel, $pid);
24   if (!$page->props['PID']) {
25     echo "failed to get Page $pid\n";
26     exit(1);
27   }
28
29   # produce HTML from POD
30   $pod = realpath($pod);
31   if (($tmpdir = System::mktemp('-d')) == false) {
32     $this->kill("Error : could not create temporary directory!");
33   }
34   $content = shell_exec("cd $tmpdir && pod2html --htmlroot=FOODOCBASE --infile=".escapeshellarg($pod));
35   $content = str_replace('<hr />', '', $content);
36   $content = preg_replace('/FOODOCBASE(.*).html/', "/$docbase$1/", $content);
37   
38   # extract title
39   if (preg_match("/<title>(.*)<\/title>/si", $content, $matches))
40   {
41     $page->props['title'] = addslashes($matches[1]);
42     if ($template)
43       $page->props['template'] = $template;
44     $page->toDb(0, $caller);
45   }
46  
47   # strip un-needed info
48   $rcs = $caller->getRcs();
49   $content = $rcs->importHtmlString($content); 
50   if (preg_match("/<h1><a name=\"synopsis\">.*/si", $content, $matches))
51     $content = $matches[0];
52
53   $content = str_replace("h1>", "h2>", $content);
54   $rcs->commit($pid,$globals->htmlfile,$content,"automatic import");
55 }
56
57
58 /** Import a set of Perl POD files.
59  *
60  * @param $caller
61  * @param $docarray
62  * @param $docbase
63  * @param $template
64  */
65 function importPods(&$caller, $docarray, $docbase, $template = '')
66 {
67   foreach ($docarray as $pod => $docdir)
68   {
69     importPod($caller, $pod, $docdir, $docbase, $template);
70   }
71 }
72
73
74 /** Print program usage and exit.
75  */
76 function usage()
77 {
78   echo "Usage: pod2diogenes [options] barrel podmap\n\n";
79   echo "Options\n\n";
80   echo "  -b<docbase>   the base URL for Perl docs (default: '')\n";
81   echo "  -h            display help message\n";
82   echo "  -t<template>  set page template to <template>\n";
83   echo "  -u<user>      make commits as <user> (default : current user)\n";
84   exit(1);
85 }
86
87
88 /** Parse a podmap file.
89  */
90 function parsePodmap($mapfile)
91 {
92   if (!($fp = fopen($mapfile, "r")))
93   {
94     echo "could not open '$mapfile'\n";
95     return;
96   }
97   $podmap = array();
98   while ($line = fgets($fp))
99   {
100     $bits = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY);
101     $podmap[$bits[0]] = $bits[1];
102     
103   }
104   fclose($fp);
105   return $podmap;
106 }
107
108
109 function main()
110 {
111   global $argv;
112
113   // set defaults
114   $docbase = '';
115   $user = get_current_user();
116   $template = '';
117   
118   // parse options
119   $opts = Console_GetOpt::getopt($argv, "b:hu:t:");
120   if ( PEAR::isError($opts) ) {
121     echo $opts->getMessage();
122     usage();
123   } else {
124     $argv = $opts[1];
125     $opts = $opts[0];
126     foreach ( $opts as $opt) {
127       switch ($opt[0]) {
128       case "h":
129         usage();
130         break;
131       case "b":
132         $docbase = $opt[1];
133         break;
134       case "u":
135         $user = $opt[1];
136         break;
137       case "t":
138         $template = $opt[1];
139         break;
140       }
141     }
142   }
143   if (sizeof($argv) != 2) 
144     usage();
145   list($alias, $mapfile) = $argv;
146
147   // parse the podmap file
148   if (!($podmap = parsePodmap($mapfile)))
149   {
150     echo "failed to parse '$mapfile'\n";
151     exit(1);
152   }
153
154   // perform the actual work
155   $script = new Diogenes_Script($alias, $user);
156   importPods($script, $podmap, $docbase, $template);
157 }
158
159 main();
160 ?>