Document Developer - instructions > Scripting > Scripting examples > Rendering rich data with scripting and rich type substitution

Rendering rich data with scripting and rich type substitution
The StoryTeller document in this example renders rich data in a substitution if the input data contains either RTF, XHTML or LXF data.
LoadRichContent() is a user defined function stored in functions.fcn in the resource set. The function uses the URLOpen() and URLreadlln() script functions to check if the input file is in rich format, either RTF, HTML or LXF. If it is, $tempfragment is returned containing the rich content.
Example 32
CodePage UTF8
// *** LoadRichContent ***
// Function reads content from specified file
// RTF, HTML, and LXF are kept as is
// Plain text is converted to XHTML
// Function returns the rich content
func LoadRichContent ()
{
	  $g_rich_format = ""; //Will be set to a format identifier
//depending on the format test result
$file_uri = #1; //the URI
	  //test the 10 first lines of input file for rich content
$err = URLOpen("id1", "$file_uri", "r");
if(num($err) = 0)
{
$linenr = 0;
while( URLReadLn ("id1" , $inbuf) = 0 )
{
			       $rich = 1;
if( stridx( $inbuf, "{\rtf" )=1 )
$g_rich_format= "RTF";
else if( stridx( $inbuf, "<3c>html" ) > 0 )
$g_rich_format = "HTML";
else if( stridx( $inbuf, "<3c>style" ) > 0 )
$g_rich_format = "HTML";
else if( stridx( $inbuf, "<3c>s-lxf" ) > 0 )
$g_rich_format = "LXF";
else $rich = 0;
			       if( num($rich) != 0 ) break;
$linenr++;
if( num($linenr) > 10 ) break;
}
		   $err = URLClose("id1") ;
		   //Process the file
$err = URLOpen("id1", $file_uri, "r") ;
Clear($tempFragment);
$linenr = 0;
while( URLreadln ("id1" , $inbuf) = 0 )
{
			      if( num($linenr) = 0 )
{
				        $tempFragment = $tempFragment + "<A0>"; //append newline
}
					      if( num($rich) = 0 ) //mask plain text with XHTML escapes      
			      {				      
$inbuf = substrrepl($inbuf, "&", "&amp;");
$inbuf = substrrepl($inbuf, "<", "&lt;");
$inbuf = substrrepl($inbuf, ">", "&gt;");
$inbuf = substrrepl($inbuf, "'", "&apos;");
$inbuf = substrrepl($inbuf, "<22>", "&quot;");
}
			      $tempFragment = $tempFragment + $inbuf;
$linenr++;
}
		    $err = URLClose("id1") ;
}
else //file open failed
{
$errtext = ioerrtext($err);
    $tempFragment  = "File Error: <22>" + $uri + "<22>, "+$errtext;
log(0, $tempFragment );
$g_rich_format = "ERR";
}
  	if( num($rich) = 0 )
{
if($g_rich_format = "") $g_rich_format = "PLAIN"; //Wrap plain
//content in
//<html and
//<pre>
//elements
$tempFragment = "<3c>html>" + "<3c>pre>" + $tempFragment +
"<3c>/pre>" + "<3c>/html";

}
  	Clear($file_uri, $err, $linenr, $inbuf,  
$utf8, $rich, $i);
  	return $tempFragment;
}
 
The script below is defined before a Story frame. The Story frame holds a Story with a substitution that points to $tempfragment and that also has the Text type property set to Rich data. The script invokes the function above to load the rich content and then changes the color of a few words in the returned content. Finally it dumps the content to a file using the FileOpen()/FileWrite()/FileClose() functions.
Example 33
$tempFileName = CurrInRealPath();
$tempFragment = LoadRichContent( $tempFileName );
 
//Make StreamServe Orange #ffa500 0,107 1 0,5 
$tempFragment = substrrepl($tempFragment, "StreamServe", "<3c>font color=<22>darkorange<22><3e>StreamServe<3c>/font<3e>");
$tempFragment = substrrepl($tempFragment, "Output Management", "<3c>font style=<22>background-color: yellow<22><3e>Output Management<3c>/font<3e>");
//Dump rich content to file
$tempFileName = "dump.htm";
$err = fileOpen ($tempFileName, "w");
$err = fileWrite($tempFileName, $tempFragment);
$err = fileClose($tempFileName) ;
 
OpenText StreamServe 5.6 Updated: 2013-03-01