From b219eb8bfe5f4be17cbef220497376c0b8a4bacf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kasper=20Sk=C3=A5rh=C3=B8j?= Date: Fri, 2 Jan 2004 08:56:02 +0000 Subject: [PATCH] Added syntax highlighting class git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@113 709f56b5-9817-0410-a4d7-c38de5d9e867 --- TODO.txt | 4 +- t3lib/class.t3lib_parsehtml.php | 7 +- t3lib/class.t3lib_syntaxhl.php | 249 ++++++++++++++++++++++++++++++++ typo3/stylesheet.css | 1 + 4 files changed, 258 insertions(+), 3 deletions(-) create mode 100644 t3lib/class.t3lib_syntaxhl.php diff --git a/TODO.txt b/TODO.txt index fc4a01d38591..745f2f0f345d 100755 --- a/TODO.txt +++ b/TODO.txt @@ -73,6 +73,8 @@ TCEmain/TCEforms for FlexForms: - Affects: "lowlevel" ext/DBint , "impexp" extension. Must/Should be extended. - Permissions handled per-field in FlexForms? - What to do if data structure was not found? (Both TCEmain, TCEforms, templavoila_pi1, t3lib_transferdata) +- block the creation of FlexForm in FlexForm inside TCEforms. +- ['pi_flexform']['config']['ds'] must be set to some default message making sense! - TCEmain features: - caching of references/files in meta data (for analysis tools)? - mapping values to a plain-text search field(s) @@ -86,7 +88,7 @@ TCEmain/TCEforms for FlexForms: - NiceToHave: Attaching an image to the Data Structure. This image is a preview of what the data structure can create. When rolling over the image you can have the form fields highlighted which affects the area. (by a red border or so...) - visual: Table with condition in top header, condition value column + block column. Not user supplied. -> OR just a selector box deciding what is shown below!? (possibly "ALL") - Displaying partial branch of the data structure (and to a certain level) - + - FlexForms cannot be possible if the record is not saved? No, should be ok, BUT a data source can of course not be referenced from another field since that fields value is NOT actually there! So only for fixed references to flex form definitions can this be done. TCEforms: - Support for other backend charsets / right-aligned charsets!? diff --git a/t3lib/class.t3lib_parsehtml.php b/t3lib/class.t3lib_parsehtml.php index 2398a2f37a2b..e43bec86f7f2 100644 --- a/t3lib/class.t3lib_parsehtml.php +++ b/t3lib/class.t3lib_parsehtml.php @@ -349,11 +349,14 @@ class t3lib_parsehtml { * Returns the NAME of the first tag in $str * * @param string HTML tag (The element name MUST be separated from the attributes by a space character! Just *whitespace* will not do) + * @param boolean If set, then the tag is NOT converted to uppercase by case is preserved. * @return string Tag name in upper case * @see getFirstTag() */ - function getFirstTagName($str) { - list($tag) = split('[[:space:]]',substr(strtoupper(trim($this->getFirstTag($str))),1,-1), 2); + function getFirstTagName($str,$preserveCase=FALSE) { + list($tag) = split('[[:space:]]',substr(trim($this->getFirstTag($str)),1,-1), 2); + if (!$preserveCase) $tag = strtoupper($tag); + return trim($tag); } diff --git a/t3lib/class.t3lib_syntaxhl.php b/t3lib/class.t3lib_syntaxhl.php new file mode 100644 index 000000000000..300eb69b8d43 --- /dev/null +++ b/t3lib/class.t3lib_syntaxhl.php @@ -0,0 +1,249 @@ + + */ +/** + * [CLASS/FUNCTION INDEX of SCRIPT] + * + * + * + * 76: class t3lib_syntaxhl + * 109: function highLight_DS($str) + * 133: function getAllTags($str) + * 164: function splitXMLbyTags($tagList,$str) + * 192: function markUpRecursively($struct,$parent='',$app='') + * + * TOTAL FUNCTIONS: 4 + * (This index is automatically created/updated by the extension "extdeveval") + * + */ + + + + + + + + + + + + + + + + + +require_once(PATH_t3lib.'class.t3lib_parsehtml.php'); + + +/** + * Syntax Highlighting class. + * + * @author Kasper Skaarhoj + * @package TYPO3 + * @subpackage t3lib + */ +class t3lib_syntaxhl { + + // Internal, dynamic: + var $htmlParse; // Parse object. + + // External, static: + var $wrapTags = array( + 'T3DataStructure' => array('',''), + 'type' => array('',''), + 'section' => array('',''), + 'el' => array('',''), + 'meta' => array('',''), + '_unknown' => array('',''), + + '_applicationTag' => array('',''), + '_applicationContents' => array('',''), + + 'sheets' => array('',''), + 'parent:sheets' => array('',''), + + 'ROOT' => array('',''), + 'parent:el' => array('',''), + + 'langDisable' => array('',''), + 'langChildren' => array('',''), + ); + + /** + * Makes syntax highlighting of a Data Structure, + * + * @param string Data Structure XML, must be valid since it's parsed. + * @return string HTML code with highlighted content. Must be wrapped in
 tags
+	 */
+	function highLight_DS($str)	{
+		
+			// Parse DS to verify that it is valid:
+		$DS = t3lib_div::xml2array($str);
+		if (is_array($DS))	{
+			$completeTagList = array_unique($this->getAllTags($str));	// Complete list of tags in DS
+			
+				// Highlighting source:
+			$this->htmlParse = t3lib_div::makeInstance('t3lib_parsehtml');	// Init parser object
+			$struct = $this->splitXMLbyTags(implode(',',$completeTagList),$str);	// Split the XML by the found tags, recursively into LARGE array.
+			$markUp = $this->markUpRecursively($struct);	// Perform color-markup on the parsed content. Markup preserves the LINE formatting of the XML. 
+
+				// Return content:
+			return $markUp;
+		} else $error = 'ERROR: The input content failed XML parsing: '.$DS;
+		return $error;
+	}
+	
+	/**
+	 * Returning all tag names found in XML/HTML input string
+	 * 
+	 * @param	string		HTML/XML input
+	 * @return	array		Array with all found tags (starttags only)
+	 */
+	function getAllTags($str)	{
+	
+			// Init:
+		$tags = array();
+		$token = md5(microtime());
+		
+			// Markup all tag names with token.
+		$markUpStr = ereg_replace('<([[:alnum:]_]+)[^>]*>',$token.'\1'.$token,$str);
+		
+			// Splitting by token:
+		$parts = explode($token,$markUpStr);
+		
+			// Traversing parts:
+		foreach($parts as $k => $v)	{
+			if ($k%2)	{
+				$tags[]=$v;
+			}
+		}
+		
+			// Returning tags:
+		return $tags;
+	}
+
+	/**
+	 * Splitting the input source by the tags listing in $tagList.
+	 * Called recursively.
+	 * 
+	 * @param	string		Commalist of tags to split source by (into blocks, ALL being block-tags!)
+	 * @param	string		Input string.
+	 * @return	array		Array with the content arranged hierarchically.
+	 */
+	function splitXMLbyTags($tagList,$str)	{
+		$struct = $this->htmlParse->splitIntoBlock($tagList,$str);
+		
+			// Traverse level:
+		foreach($struct as $k => $v)	{
+			if ($k%2)	{
+				$tag = $this->htmlParse->getFirstTag($v);
+				$tagName = $this->htmlParse->getFirstTagName($tag,TRUE);
+				$struct[$k] = array(
+					'tag' => $tag,
+					'tagName' => $tagName,
+					'sub' => $this->splitXMLbyTags($tagList,$this->htmlParse->removeFirstAndLastTag($struct[$k]))
+				);
+			}
+		}
+		
+		return $struct;
+	}
+
+	/**
+	 * Making syntax highlighting of the parsed Data Structure XML.
+	 * Called recursively.
+	 * 
+	 * @param	array		The structure, see splitXMLbyTags()
+	 * @param	string		Parent tag.
+	 * @param	string		"Application" - used to denote if we are 'inside' a sectionl
+	 * @return	string		HTML
+	 */
+	function markUpRecursively($struct,$parent='',$app='')	{
+		$output='';
+		foreach($struct as $k => $v)	{
+			if ($k%2)	{
+				$nextApp = $app;
+				$innerWrap = $wrap = array('','');
+				
+				switch($app)	{
+					case 'TCEforms':
+					case 'tx_templavoila':
+						$wrap = $this->wrapTags['_applicationContents'];
+					break;
+					case 'el':
+					default:
+						if ($parent=='el')	{
+							$wrap = $this->wrapTags['parent:el'];
+							$nextApp = 'el';
+						} elseif ($parent=='sheets')	{
+							$wrap = $this->wrapTags['parent:sheets'];
+						} else {
+							$wrap = $this->wrapTags[$v['tagName']];
+							$nextApp = '';
+						}
+				
+						if (!is_array($wrap))	{
+							$wrap = $this->wrapTags['_unknown'];
+						}
+						
+						if ($app=='el' || $parent=='ROOT')	{
+							switch($v['tagName'])	{
+								case 'TCEforms':
+								case 'tx_templavoila':
+									$nextApp = $v['tagName'];
+									$wrap = $this->wrapTags['_applicationTag'];
+									#$innerWrap = $this->wrapTags['_applicationContents'];
+								break;
+							}
+						}
+					break;
+				}
+
+				$output.=$wrap[0].htmlspecialchars($v['tag']).$wrap[1];
+				$output.=$innerWrap[0].$this->markUpRecursively($v['sub'],$v['tagName'],$nextApp).$innerWrap[1];
+				$output.=$wrap[0].htmlspecialchars('').$wrap[1];
+			} else {
+				$output.=htmlspecialchars($v);
+			}
+		}
+		
+		return $output;
+	}
+}
+
+
+if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_syntaxhl.php'])	{
+	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_syntaxhl.php']);
+}
+?>
\ No newline at end of file
diff --git a/typo3/stylesheet.css b/typo3/stylesheet.css
index c24a564272d6..85ccebc65466 100755
--- a/typo3/stylesheet.css
+++ b/typo3/stylesheet.css
@@ -421,3 +421,4 @@ PRE.ts-hl .ts-linenum {background-color: #eeeeee;}
 /*
 H2,H3,H4,DIV {border: 1px dotted #666666;}
 */
+
-- 
2.20.1