1<?php
2
3/*
4	This file is part of ActiveLink PHP DOC Package (www.active-link.com).
5	Copyright (c) 2002-2004 by Zurab Davitiani
6
7	You can contact the author of this software via E-mail at
8	hattrick@mailcan.com
9
10	ActiveLink PHP DOC Package is free software; you can redistribute it and/or modify
11	it under the terms of the GNU Lesser General Public License as published by
12	the Free Software Foundation; either version 2.1 of the License, or
13	(at your option) any later version.
14
15	ActiveLink PHP DOC Package is distributed in the hope that it will be useful,
16	but WITHOUT ANY WARRANTY; without even the implied warranty of
17	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18	GNU Lesser General Public License for more details.
19
20	You should have received a copy of the GNU Lesser General Public License
21	along with ActiveLink PHP DOC Package; if not, write to the Free Software
22	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23*/
24
25import("org.active-link.xml.XML");
26import("org.active-link.doc.PHPClass");
27import("org.active-link.doc.Method");
28
29/**
30  *	DocHTML parses PHP class file comments and generates documentation
31  *	@class		DocHTML
32  *	@package	org.active-link.doc
33  *	@author		Zurab Davitiani
34  *	@version	0.3.4
35  *	@requires	XML, PHPClass, Method
36  *	@see		PHPClass
37  */
38
39class DocHTML {
40
41	var $CSSFile;
42	var $CSSFileTag;
43	var $CSSString;
44	var $CSSStringTag;
45	var $CSSStringDefault;
46
47	/**
48	  *	Constructor, runs when new object instance is created, sets default values
49	  *	@method		DocHTML
50	  */
51	function DocHTML() {
52		$this->CSSStringDefault = "
53		body {background-color: white;}
54		a {font-family: monospace;}
55		ul {list-style-type: none;}
56		.classTitle {color: blue;}
57		.name {color: black;}
58		.version {color: black;}
59		.requires {color: red;}
60		.extends {color: black;}
61		.description {color: black;font-family: sans-serif;}
62		.author {color: blue;}
63		.methodsTitle {color: blue;}
64		.methodList {color: blue;}
65		.methodName {color: blue;font-weight: bold;}
66		.returns {color: black;}
67		.param {color: black;font-weight: bold;font-family: monospace;}
68		";
69	}
70
71	/**
72	  *	Returns class documentation as a string, formatted in HTML
73	  *	If argument is a filename, it parses the file for comments and generates documentation
74	  *	If argument is an object of type PHPClass, then documentation is generated from it
75	  *	@method		getClassDoc
76	  *	@param		mixed argument
77	  *	@returns	string HTML-formatted documentation if successful, false otherwise
78	  */
79	function getClassDoc($argument) {
80		if(is_object($argument) && get_class($argument) == "phpclass")
81			return $this->getClassDocFromClass($argument);
82		elseif(is_string($argument))
83			return $this->getClassDocFromFile($argument);
84		else
85			return false;
86	}
87
88	/**
89	  *	Returns class documentation as a string, formatted in HTML
90	  *	@method		getClassDocFromClass
91	  *	@param		object objClass
92	  *	@returns	string HTML-formatted documentation if successful, false otherwise
93	  */
94	function getClassDocFromClass($objClass) {
95		if(is_object($objClass) && get_class($objClass) == "phpclass") {
96			$classDocXML = new XML("html");
97			// ---------------------- HEAD ---------------------- //
98			$headXML = new XMLBranch("head");
99			$headXML->setTagContent($objClass->getInfo("name"), "head/title");
100			$headXML->setTagContent("", "head/meta");
101			$headXML->setTagAttribute("http-equiv", "content-type", "head/meta");
102			$headXML->setTagAttribute("content", "text/html; charset=ISO-8859-1", "head/meta");
103			$headXML->setTagContent($this->CSSStringDefault, "head/style");
104			$headXML->setTagAttribute("type", "text/css", "head/style");
105			// ---------------------- BODY ---------------------- //
106			$bodyXML = new XMLBranch("body");
107			$classTitleXML = new XMLBranch("h1");
108			$classTitleXML->setTagAttribute("class", "classTitle");
109			$classTitleXML->setTagContent($objClass->getInfo("name") . " Class");
110			$bodyXML->addXMLBranch($classTitleXML);
111			foreach($objClass->info as $infoKey => $infoValue) {
112				$brXML = new XMLBranch("br");
113				$bodyXML->addXMLBranch($brXML);
114				if(is_array($infoValue)) {
115					$spanXML = new XMLBranch("span");
116					$spanXML->setTagAttribute("class", $infoKey);
117					$spanXML->setTagContent(ucfirst($infoKey) . ":");
118					$ulXML = new XMLBranch("ul");
119					$ulXML->setTagAttribute("class", $infoKey);
120					foreach($infoValue as $value) {
121						$liXML = new XMLBranch("li");
122						$liXML->setTagContent($value);
123						$ulXML->addXMLBranch($liXML);
124					}
125					$bodyXML->addXMLBranch($spanXML);
126					$bodyXML->addXMLBranch($ulXML);
127				}
128				else {
129					$spanXML = new XMLBranch("span");
130					$spanXML->setTagAttribute("class", $infoKey);
131					$spanXML->setTagContent(ucfirst($infoKey) . ": " . $infoValue);
132					$bodyXML->addXMLBranch($spanXML);
133				}
134			}
135			$hrXML = new XMLBranch("hr");
136			$bodyXML->addXMLBranch($hrXML);
137			$h2XML = new XMLBranch("h2");
138			$h2XML->setTagAttribute("class", "methodsTitle");
139			$h2XML->setTagContent("All Methods");
140			$bodyXML->addXMLBranch($h2XML);
141			$spanXML = new XMLBranch("span");
142			$spanXML->setTagAttribute("class", "methodList");
143			foreach($objClass->methods as $methodName => $method) {
144				$aMethodXML = new XMLBranch("a");
145				$aMethodXML->setTagAttribute("href", "#" . $methodName);
146				$aMethodXML->setTagContent($methodName);
147				$brXML = new XMLBranch("br");
148				$spanXML->addXMLBranch($aMethodXML);
149				$spanXML->addXMLBranch($brXML);
150			}
151			$bodyXML->addXMLBranch($spanXML);
152			foreach($objClass->methods as $methodName => $method) {
153				$hrXML = new XMLBranch("hr");
154				$bodyXML->addXMLBranch($hrXML);
155				$pMethodXML = new XMLBranch("p");
156				$aMethodXML = new XMLBranch("a");
157				$aMethodXML->setTagAttribute("name", $methodName);
158				$spanXMLName = new XMLBranch("span");
159				$spanXMLName->setTagAttribute("class", "methodName");
160				$spanXMLName->setTagContent($methodName);
161				$spanXMLArgs = new XMLBranch("span");
162				$tagContentArgs = " ( ";
163				if(is_array($method->params) && count($method->params) > 0) {
164					$paramCount = 0;
165					foreach($method->params as $key => $value) {
166						if($paramCount > 0)
167							$tagContentArgs .= ", ";
168						$tagContentArgs .= $key;
169						$paramCount ++;
170					}
171				}
172				$tagContentArgs .= " )";
173				$spanXMLArgs->setTagContent($tagContentArgs);
174				$aMethodXML->addXMLBranch($spanXMLName);
175				$aMethodXML->addXMLBranch($spanXMLArgs);
176				$pMethodXML->addXMLBranch($aMethodXML);
177				$bodyXML->addXMLBranch($pMethodXML);
178				unset($method->info["name"]);
179				foreach($method->info as $infoKey => $infoValue) {
180					if(is_array($infoValue)) {
181						$pXML = new XMLBranch("p");
182						$pXML->setTagAttribute("class", $infoKey);
183						$pXML->setTagContent(ucfirst($infoKey) . ":");
184						$ulXML = new XMLBranch("ul");
185						$ulXML->setTagAttribute("class", $infoKey);
186						foreach($infoValue as $value) {
187							$liXML = new XMLBranch("li");
188							$liXML->setTagContent($value);
189							$ulXML->addXMLBranch($liXML);
190						}
191						$bodyXML->addXMLBranch($pXML);
192						$bodyXML->addXMLBranch($ulXML);
193					}
194					else {
195						$pXML = new XMLBranch("p");
196						$pXML->setTagAttribute("class", $infoKey);
197						$pXML->setTagContent(ucfirst($infoKey) . ": " . $infoValue);
198						$bodyXML->addXMLBranch($pXML);
199					}
200				}
201				if(is_array($method->params) && count($method->params) > 0) {
202					$pParamXML = new XMLBranch("p");
203					//$pParamXML->setTagAttribute("class", "param");
204					$paramTitleXML = new XMLBranch("span");
205					$paramTitleXML->setTagContent("Arguments:");
206					$pParamXML->addXMLBranch($paramTitleXML);
207					$paramListXML = new XMLBranch("ul");
208					foreach($method->params as $key => $value) {
209						$paramItemXML = new XMLBranch("li");
210						$paramItemXML->setTagAttribute("class", "param");
211						$paramItemXML->setTagContent($key);
212						$paramListXML->addXMLBranch($paramItemXML);
213					}
214					$pParamXML->addXMLBranch($paramListXML);
215					$bodyXML->addXMLBranch($pParamXML);
216				}
217			}
218			// ---------------------- END  ---------------------- //
219			$classDocXML->addXMLBranch($headXML);
220			$classDocXML->addXMLBranch($bodyXML);
221			return $classDocXML->getXMLString(0);
222		}
223		else
224			return false;
225	}
226
227	/**
228	  *	Returns class documentation as a string, formatted in HTML
229	  *	@method		getClassDocFromFile
230	  *	@param		string filename
231	  *	@returns	string HTML-formatted documentation if successful, false otherwise
232	  */
233	function getClassDocFromFile($filename) {
234		if(is_string($filename) && file_exists($filename) && is_readable($filename)) {
235			$objClass = new PHPClass($filename);
236			return $this->getClassDocFromClass($objClass);
237		}
238		else
239			return false;
240	}
241
242}
243