-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathqa-formatter-overrides.php
More file actions
177 lines (156 loc) · 4.21 KB
/
qa-formatter-overrides.php
File metadata and controls
177 lines (156 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
function qa_viewer_html($content, $format, $options = array())
{
if (qa_to_override(__FUNCTION__)) { $args = func_get_args(); $html = qa_call_override(__FUNCTION__, $args); }
else $html = $content;
if (
qa_opt('qa-codecogs-feed-enable') &&
function_exists('qa_mathjax_is_feed_request') &&
qa_mathjax_is_feed_request() &&
function_exists('qa_mathjax_convert_to_codecogs')
) {
$html = qa_mathjax_convert_to_codecogs($html, 'img');
}
return $html;
}
function qa_string_to_words1($string, $tolowercase = true, $delimiters = false, $splitideographs = true, $splithyphens = true)
{
if (qa_to_override(__FUNCTION__)) { $args=func_get_args(); return qa_call_override(__FUNCTION__, $args); }
global $qa_utf8punctuation;
if ($tolowercase)
$string = qa_strtolower($string);
$string = strtr($string, $qa_utf8punctuation);
$separator = QA_PREG_INDEX_WORD_SEPARATOR;
if ($splithyphens)
$separator .= '|\-';
$separator .= '|\$';
if ($delimiters) {
if ($splitideographs)
$separator .= '|' . QA_PREG_CJK_IDEOGRAPHS_UTF8;
} else {
$string = preg_replace("/(\S)'(\S)/", '\1\2', $string); // remove apostrophes in words
if ($splitideographs) // put spaces around CJK ideographs so they're treated as separate words
$string = preg_replace('/' . QA_PREG_CJK_IDEOGRAPHS_UTF8 . '/', ' \0 ', $string);
}
return preg_split('/(' . $separator . '+)/', $string, -1, PREG_SPLIT_NO_EMPTY | ($delimiters ? PREG_SPLIT_DELIM_CAPTURE : 0));
}
function qa_shorten_string_line($string,$length=0, $ellipsis = ' ... ')
{
global $counter;
$string = strtr($string, "\r\n\t", ' ');
//return $string;
$string = str_replace("$$", "$", $string);
if (qa_strlen($string) > $length) {
$remaining = $length - qa_strlen($ellipsis);
$words = qa_string_to_words1($string, false, true);
$stringwords = count($words);
$prefix = '';
$suffix = '';
$prefixdollar = 0; $suffixdollar = 0;
$sslashon = $stexton = $stextmode = false;
$pslashon = $ptexton = $ptextmode = false;
for ($addword = 0; $addword < $stringwords; $addword++) {
$tosuffix = $addword % 3 == 1; // order: prefix, suffix, prefix, prefix, suffix, prefix, ...
$word = $tosuffix ? array_pop($words) : array_shift($words);
$wordLength = qa_strlen($word);
if(($word == '$') && ($wordLength <= $remaining))
{
if($tosuffix && !$stextmode)
{
if($suffixdollar == 0)$suffixtemp = $suffix;
$suffixdollar++;
if(($suffixdollar%2) == 0)
{
$suffixtemp = $word. $suffix;
}
}
else if(!$ptextmode)
{
if($prefixdollar == 0)$prefixtemp = $prefix;
$prefixdollar++;
if(($prefixdollar%2) == 0)
{
$prefixtemp = $prefix.$word;
}
}
}
if($tosuffix) {
if($suffixdollar%2) {
if($stextmode && $word == "}") {
$stextmode = false;
}
if($stexton && trim($word)) {
if($word == "{") {
$stextmode = true;
}
else {
$stextmode = false;
}
$stexton = false;
}
if($sslashon && trim($word)) {
if( $word == "text") {
$stexton = true;
}
else {
$sslashon = false;
$stexton = false;
}
$sslason = false;
}
if($sslashon && $word == "text") {
$stexton = true;
$sslashon = false;
}
if ($word == "\\") {
$sslashon = true;
}
}
}
else {
if($prefixdollar%2) {
if($ptextmode && $word == "}") {
$ptextmode = false;
}
if($ptexton && trim($word)) {
if($word == "{") {
$ptextmode = true;
}
else {
$ptextmode = false;
}
$ptexton = false;
}
if($pslashon && trim($word)) {
if( $word == "text") {
$ptexton = true;
}
else {
$ptexton = false;
}
$pslason = false;
}
if($pslashon && $word == "text") {
$ptexton = true;
$pslashon = false;
}
if ($word == "\\") {
$pslashon = true;
}
}
}
if ($wordLength > $remaining)
break;
if ($tosuffix)
$suffix = $word . $suffix;
else
$prefix .= $word;
$remaining -= $wordLength;
}
if(($suffixdollar%2) == 1) $suffix = $suffixtemp;
if(($prefixdollar%2) == 1) $prefix = $prefixtemp;
$string = $prefix . $ellipsis . $suffix;
}
return $string;
}
?>