00001 <?php
00002 require_once dirname(__FILE__) . '/html.converter.php';
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 class ConverterHtmlEx extends ConverterHtml {
00013
00014
00015
00016
00017
00018
00019 protected function process_paragraph($text, $params) {
00020 if (String::length($text) <= 70 && String::right($text, 1) != '.') {
00021 $level = intval(Arr::get_item($params, 'h', 2));
00022 return html::tag('h' . $level, $text);
00023 }
00024 else {
00025 return parent::process_paragraph($text, $params);
00026 }
00027 }
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 public function decode($value, $params = false) {
00043
00044 if (!preg_match('@<\w+.*?>@', $value)) {
00045 return $value;
00046 }
00047
00048
00049 $value = $this->decode_anchors($value, Arr::get_item($params, 'a', '$title$: $url$'));
00050
00051
00052 $value = str_replace("\r", "\n", $value);
00053 $value = String::preg_replace('|\n+|', " ", $value);
00054
00055
00056 $value = preg_replace('@\s*<p>\s*@', "<p>", $value);
00057 $after_p = Arr::get_item($params, 'p', "\n");
00058 $value = preg_replace('@\s*</p>\s*@', "</p>$after_p", $value);
00059 $after_br = Arr::get_item($params, 'br', "\n");
00060 $value = preg_replace('@<br.*?>\s*@', $after_br, $value);
00061
00062
00063 $value = strip_tags($value);
00064 $value = String::unescape($value);
00065 $value = String::preg_replace('| +|', ' ', $value);
00066
00067 return $value;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 protected function decode_anchors($value, $link_format) {
00077 $value = $this->relative_to_absolute($value, Config::get_url(Config::URL_BASEURL));
00078 $link_format = str_replace('$title$', '$2', $link_format);
00079 $link_format = str_replace('$url$', '$1', $link_format);
00080 return String::preg_replace('|<a.*?href="(.*?)".*?>(.*?)</a>|', $link_format, $value);
00081 }
00082
00083
00084
00085
00086
00087
00088
00089 protected function relative_to_absolute($text, $base) {
00090 if (empty($base)) {
00091 return $text;
00092 }
00093
00094 if (substr($base, -1, 1) != "/") { $base .= "/"; }
00095 $domain = Url::create($base)->clear_query()->set_path('')->build(Url::ABSOLUTE);
00096
00097
00098 $pattern = '|<a(.*?) href="/(.*?)"|';
00099 $replace = '<a$1 href="' . $domain . '$2"';
00100 $text = preg_replace($pattern, $replace, $text);
00101
00102
00103 $pattern = '|<a(.*?) href="(?!\w+://)(.*?)"|';
00104 $replace = '<a$1 href="' . $base . '$2"';
00105 $text = preg_replace($pattern, $replace, $text);
00106
00107 return $text;
00108 }
00109 }