1<?php
2/********************************
3OSBib:
4A collection of PHP classes to create and manage bibliographic formatting for OS bibliography software
5using the OSBib standard.
6
7Released through http://bibliophile.sourceforge.net under the GPL licence.
8Do whatever you like with this -- some credit to the author(s) would be appreciated.
9
10If you make improvements, please consider contacting the administrators at bibliophile.sourceforge.net
11so that your improvements can be added to the release package.
12
13Mark Grimshaw 2005
14http://bibliophile.sourceforge.net
15********************************/
16
17/** Description of class EXPORT
18* Format a bibliographic resource for output.
19*
20* @author	Andrea Rossato
21* @version	1
22*/
23class EXPORTFILTER
24{
25/**
26* $dir is the path to STYLEMAP.php etc.
27*/
28	function EXPORTFILTER(&$ref, $output)
29	{
30	  $this->bibformat =& $ref;
31	  $this->format = $output;
32	}
33/**
34* Format for HTML or RTF/plain?
35*
36* @author	Mark Grimshaw
37* @version	1
38*
39* @param	$data	Input string
40* @param	$patterns	Optional preg pattern (usually used to highlight a search phrase)
41* @param	$class		Option CSS class for highlighting a search phrase
42*/
43	function format($data)
44        {
45		if($this->format == 'html')
46		{
47/**
48* Scan for search patterns and highlight accordingly
49*/
50/**
51* Temporarily replace any URL - works for just one URL in the output string.
52*/
53			if(preg_match("/(<a.*>.*<\/a>)/i", $data, $match))
54			{
55				$url = preg_quote($match[1], '/');
56				$data = preg_replace("/$url/", "OSBIB__URL__OSBIB", $data);
57			}
58			else
59				$url = FALSE;
60			$data = str_replace("\"", "&quot;", $data);
61			$data = str_replace("<", "&lt;", $data);
62			$data = str_replace(">", "&gt;", $data);
63			$data = preg_replace("/&(?![a-zA-Z0-9#]+?;)/", "&amp;", $data);
64			$data = $this->bibformat->patterns ?
65				preg_replace($this->bibformat->patterns,
66				"<span class=\"" . $this->bibformat->patternHighlight . "\">$1</span>", $data) : $data;
67			$data = preg_replace("/\[b\](.*?)\[\/b\]/is", "<strong>$1</strong>", $data);
68        		$data = preg_replace("/\[i\](.*?)\[\/i\]/is", "<em>$1</em>", $data);
69        		$data = preg_replace("/\[sup\](.*?)\[\/sup\]/is", "<sup>$1</sup>", $data);
70        		$data = preg_replace("/\[u\](.*?)\[\/u\]/is",
71				"<span style=\"text-decoration: underline;\">$1</span>", $data);
72// Recover any URL
73			if($url)
74				$data = str_replace("OSBIB__URL__OSBIB", $match[1], $data);
75		}
76		else if($this->format == 'rtf')
77		{
78			$data = preg_replace("/&#(.*?);/", "\\u$1", $data);
79			$data = preg_replace("/\[b\](.*?)\[\/b\]/is", "{{\\b $1}}", $data);
80        		$data = preg_replace("/\[i\](.*?)\[\/i\]/is", "{{\\i $1}}", $data);
81        		$data = preg_replace("/\[u\](.*?)\[\/u\]/is", "{{\\ul $1}}", $data);
82// Need to figure this one out for RTF
83        		$data = preg_replace("/\[sup\](.*?)\[\/sup\]/is", "$1", $data);
84		}
85/**
86* OpenOffice-1.x.
87*/
88		else if($this->format == 'sxw')
89		{
90			$data = $this->bibformat->utf8->decodeUtf8($data);
91			$data = str_replace("\"", "&quot;", $data);
92			$data = str_replace("<", "&lt;", $data);
93			$data = str_replace(">", "&gt;", $data);
94			$data = preg_replace("/&(?![a-zA-Z0-9#]+?;)/", "&amp;", $data);
95			$data = preg_replace("/\[b\](.*?)\[\/b\]/is", "<text:span text:style-name=\"textbf\">$1</text:span>", $data);
96        		$data = preg_replace("/\[i\](.*?)\[\/i\]/is", "<text:span text:style-name=\"emph\">$1</text:span>", $data);
97        		$data = preg_replace("/\[sup\](.*?)\[\/sup\]/is", "<text:span text:style-name=\"superscript\">$1</text:span>", $data);
98        		$data = preg_replace("/\[u\](.*?)\[\/u\]/is",
99				"<text:span text:style-name=\"underline\">$1</text:span>", $data);
100			$data = "<text:p text:style-name=\"Text body\">".$data."</text:p>\n";
101		}
102/**
103* StripBBCode for plain.
104*/
105		else
106			$data = preg_replace("/\[.*\]|\[\/.*\]/U", "", $data);
107		return $data;
108	}
109}
110?>