rename to pod2diogenes
[diogenes.git] / scripts / pod2diogenes
diff --git a/scripts/pod2diogenes b/scripts/pod2diogenes
new file mode 100755 (executable)
index 0000000..21e6a32
--- /dev/null
@@ -0,0 +1,160 @@
+#!/usr/bin/php
+<?php
+ini_set("include_path", "/etc/diogenes:/usr/share/diogenes/include:/usr/share/php");
+require_once("diogenes.common.inc.php");
+require_once("diogenes.script.inc.php");
+require_once("System.php");
+require_once("Barrel.php");
+
+/** Import a single Perl POD file.
+ *
+ * @param $caller
+ * @param $pod
+ * @param $docdir
+ * @param $docbase
+ * @param $template
+ */
+function importPod(&$caller, $pod, $docdir, $docbase, $template = '')
+{
+  global $globals;
+  $barrel =& $caller->barrel;
+
+  $pid = $barrel->makePath($docdir, $caller);
+  $page = Diogenes_Barrel_Page::fromDb($barrel, $pid);
+  if (!$page->props['PID']) {
+    echo "failed to get Page $pid\n";
+    exit(1);
+  }
+
+  # produce HTML from POD
+  $pod = realpath($pod);
+  if (($tmpdir = System::mktemp('-d')) == false) {
+    $this->kill("Error : could not create temporary directory!");
+  }
+  $content = shell_exec("cd $tmpdir && pod2html --htmlroot=FOODOCBASE --infile=".escapeshellarg($pod));
+  $content = str_replace('<hr />', '', $content);
+  $content = preg_replace('/FOODOCBASE(.*).html/', "/$docbase$1/", $content);
+  
+  # extract title
+  if (preg_match("/<title>(.*)<\/title>/si", $content, $matches))
+  {
+    $page->props['title'] = addslashes($matches[1]);
+    if ($template)
+      $page->props['template'] = $template;
+    $page->toDb(0, $caller);
+  }
+  # strip un-needed info
+  $rcs = $caller->getRcs();
+  $content = $rcs->importHtmlString($content); 
+  if (preg_match("/<h1><a name=\"synopsis\">.*/si", $content, $matches))
+    $content = $matches[0];
+
+  $content = str_replace("h1>", "h2>", $content);
+  $rcs->commit($pid,$globals->htmlfile,$content,"automatic import");
+}
+
+
+/** Import a set of Perl POD files.
+ *
+ * @param $caller
+ * @param $docarray
+ * @param $docbase
+ * @param $template
+ */
+function importPods(&$caller, $docarray, $docbase, $template = '')
+{
+  foreach ($docarray as $pod => $docdir)
+  {
+    importPod($caller, $pod, $docdir, $docbase, $template);
+  }
+}
+
+
+/** Print program usage and exit.
+ */
+function usage()
+{
+  echo "Usage: pod2diogenes [options] barrel podmap\n\n";
+  echo "Options\n\n";
+  echo "  -b<docbase>   the base URL for Perl docs (default: '')\n";
+  echo "  -h            display help message\n";
+  echo "  -t<template>  set page template to <template>\n";
+  echo "  -u<user>      make commits as <user> (default : current user)\n";
+  exit(1);
+}
+
+
+/** Parse a podmap file.
+ */
+function parsePodmap($mapfile)
+{
+  if (!($fp = fopen($mapfile, "r")))
+  {
+    echo "could not open '$mapfile'\n";
+    return;
+  }
+  $podmap = array();
+  while ($line = fgets($fp))
+  {
+    $bits = preg_split('/\s+/', $line, -1, PREG_SPLIT_NO_EMPTY);
+    $podmap[$bits[0]] = $bits[1];
+    
+  }
+  fclose($fp);
+  return $podmap;
+}
+
+
+function main()
+{
+  global $argv;
+
+  // set defaults
+  $docbase = '';
+  $user = get_current_user();
+  $template = '';
+  
+  // parse options
+  $opts = Console_GetOpt::getopt($argv, "b:hu:t:");
+  if ( PEAR::isError($opts) ) {
+    echo $opts->getMessage();
+    usage();
+  } else {
+    $argv = $opts[1];
+    $opts = $opts[0];
+    foreach ( $opts as $opt) {
+      switch ($opt[0]) {
+      case "h":
+        usage();
+        break;
+      case "b":
+        $docbase = $opt[1];
+        break;
+      case "u":
+        $user = $opt[1];
+        break;
+      case "t":
+        $template = $opt[1];
+        break;
+      }
+    }
+  }
+  if (sizeof($argv) != 2) 
+    usage();
+  list($alias, $mapfile) = $argv;
+
+  // parse the podmap file
+  if (!($podmap = parsePodmap($mapfile)))
+  {
+    echo "failed to parse '$mapfile'\n";
+    exit(1);
+  }
+
+  // perform the actual work
+  $script = new Diogenes_Script($alias, $user);
+  importPods($script, $podmap, $docbase, $template);
+}
+
+main();
+?>