Hey guys,

Here a patch for the bugs #47406 and #48264.
Both bugs are about using an external CSS in the xhtml based formats in PhD,
so the patch implements the --css option.

comments?

--Moacir
Index: phpdotnet/phd/Package/Generic/XHTML.php
===================================================================
--- phpdotnet/phd/Package/Generic/XHTML.php     (revision 288345)
+++ phpdotnet/phd/Package/Generic/XHTML.php     (working copy)
@@ -432,6 +432,8 @@
         'dbhtml'        => 'PI_DBHTMLHandler',
     );
 
+    protected $stylesheets = array();
+
     public function __construct() {
         parent::__construct();
         $this->registerPIHandlers($this->pihandlers);
@@ -514,6 +516,30 @@
         return $retval;
     }
 
+    protected function fetchStylesheet() {
+        $stylesDir = $this->getOutputDir();
+        if (!$stylesDir) {
+            $stylesDir = Config::output_dir();
+        }
+        $stylesDir .= 'styles/';
+        if (file_exists($stylesDir)) {
+            if (!is_dir($stylesDir)) {
+                v("The styles/ directory is a file?", E_USER_ERROR);
+            }
+        } else {
+            if (!mkdir($stylesDir)) {
+                v("Can't create the styles/ directory", E_USER_ERROR);
+            }
+        }
+        foreach ((array)Config::css() as $css) {
+            $dest = $stylesDir . basename($css);
+            if (@copy($css, $dest)) {
+                $this->stylesheets[] = basename($css);
+            } else {
+                v(sprintf('Impossible to copy the %s file.', $css), 
E_USER_WARNING);
+            }
+        }
+    }
  
 /* Functions format_* */     
     public function format_suppressed_tags($open, $name, $attrs, $props) {
Index: phpdotnet/phd/Package/Generic/BigXHTML.php
===================================================================
--- phpdotnet/phd/Package/Generic/BigXHTML.php  (revision 288345)
+++ phpdotnet/phd/Package/Generic/BigXHTML.php  (working copy)
@@ -36,6 +36,10 @@
 
     public function header() {
         $root = Format::getRootIndex();
+        $cssLinks = '';
+        foreach ((array)$this->stylesheets as $css) {
+            $cssLinks .= "    <link media=\"all\" rel=\"stylesheet\" 
type=\"text/css\" href=\"styles/".$css."\" />\n";
+        }
         return <<<HEADER
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
@@ -43,6 +47,7 @@
 <head>
     <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
     <title>{$root["ldesc"]}</title>
+{$cssLinks}
 </head>
 <body>
 HEADER;
@@ -74,9 +79,12 @@
             if ($val) {
                 if (!is_resource($this->getFileStream())) {
                     $filename = Config::output_dir() . 
strtolower($this->getFormatName()) . '.' . $this->getExt();
+                    $this->postConstruct();
+                    if (Config::css()) {
+                        $this->fetchStylesheet();
+                    }
                     $this->setFileStream(fopen($filename, "w+"));
                     fwrite($this->getFileStream(), $this->header());
-                    $this->postConstruct();
                 }
             } 
             break;
Index: phpdotnet/phd/Package/Generic/ChunkedXHTML.php
===================================================================
--- phpdotnet/phd/Package/Generic/ChunkedXHTML.php      (revision 288345)
+++ phpdotnet/phd/Package/Generic/ChunkedXHTML.php      (working copy)
@@ -78,6 +78,9 @@
                     v("Can't create output directory", E_USER_ERROR);
                 }
             }
+            if (Config::css()) {
+                $this->fetchStylesheet();
+            }
             break;
         case Render::VERBOSE:
                v("Starting %s rendering", $this->getFormatName(), 
VERBOSE_FORMAT_RENDERING);
@@ -90,6 +93,11 @@
         $lang = Config::language();
         $root = Format::getRootIndex();
 
+        $cssLinks = '';
+        foreach ((array)$this->stylesheets as $css) {
+            $cssLinks .= "    <link media=\"all\" rel=\"stylesheet\" 
type=\"text/css\" href=\"styles/".$css."\" />\n";
+        }
+
         $prev = $next = $parent = array("href" => null, "desc" => null);
 
         if ($parentId = $this->getParent($id)) {
@@ -112,6 +120,7 @@
 <head>
     <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
     <title>'.(($title != $root["ldesc"]) ? $root["ldesc"].': ' : 
"").$title.'</title>
+'.$cssLinks.'
 </head>
 <body>
 <table width="100%">
Index: phpdotnet/phd/Config.php
===================================================================
--- phpdotnet/phd/Config.php    (revision 288345)
+++ phpdotnet/phd/Config.php    (working copy)
@@ -36,6 +36,7 @@
         "package"           => array(
             "Generic",
         ),
+        'css'               => array(),
     );
 
     public static function init(array $a) {
Index: phpdotnet/phd/BuildOptionsParser.php
===================================================================
--- phpdotnet/phd/BuildOptionsParser.php        (revision 288345)
+++ phpdotnet/phd/BuildOptionsParser.php        (working copy)
@@ -24,6 +24,7 @@
             "version"      => "V",         // Print out version information
             "help"         => "h",         // Print out help
             "package:"     => "P:",        // The package of formats           
 
+            "css:"         => "C:",        // External CSS 
         );
     }
 
@@ -212,6 +213,9 @@
     }
     public function option_c($k, $v)
     {
+        if ($k == "C") {
+            return $this->option_css($k, $v);
+        }
         $this->option_color($k, $v);
     }
     public function option_color($k, $v)
@@ -234,6 +238,15 @@
             trigger_error("yes/no || on/off || true/false || 1/0 expected", 
E_USER_ERROR);
         }
     }
+    public function option_css($k, $v) {
+        $styles = array();
+        foreach((array)$v as $key => $val) {
+            if (!in_array($val, $styles)) {
+                $styles[] = $val;
+            }
+        }
+        Config::set_css($styles);
+    }
 
     /**
      * Prints out the current PhD and PHP version.

Reply via email to