17 namespace OPiQuotations;
40 public function __construct($id,
$text,
44 $subject=null, $nation_author=null, $work=null) {
46 assert(
'is_int($id)');
48 assert(
'is_string($text)');
49 assert(
'is_bool($is_maxim)');
50 assert(
'is_bool($is_marked)');
51 assert(
'($translation === null) || is_string($translation)');
52 assert(
'($subject === null) || is_string($subject)');
53 assert(
'($nation_author === null) || is_string($nation_author)');
54 assert(
'($work === null) || is_string($work)');
59 $this->maxim = $is_maxim;
60 $this->marked = $is_marked;
61 $this->translation = $translation;
62 $this->subject = $subject;
65 assert(
'$work === null');
68 $this->nation = $nation_author;
71 $this->author = $nation_author;
92 assert(
'is_string($s)');
93 # assert('preg_match(\'/ $/\', $s) === 0'); // check if no final space
96 $s = preg_replace(
'/<(b|c|i|sub|sup|\/b|\/c|\/i|\/sub|\/sup)>/',
'@@@#$1#@@@', $s);
98 $s = htmlspecialchars($s);
101 $s = preg_replace(
'/@@@#(b|i|sub|sup|\/b|\/i|\/sub|\/sup)#@@@/',
'<$1>', $s);
102 $s = preg_replace(
'/@@@#c#@@@/',
'<span class="block-center">', $s);
103 $s = preg_replace(
'/@@@#c#@@@/',
'<span class="block-center">', $s);
104 $s = preg_replace(
'/@@@#\/c#@@@\n?/',
'</span>', $s);
107 $s = preg_replace(
'/¨(.+)\n?/',
'<span class="block-right">$1</span>', $s);
110 $s = preg_replace(
'/« /',
'« ', $s);
111 $s = preg_replace(
'/ (»|!|\?)/',
' $1', $s);
114 $s = preg_replace(
'/ (:|;)/',
'<span class="nowrap"> $1</span>', $s);
117 $s = preg_replace(
'/(^|\n) /',
'$1  ', $s);
118 $s = preg_replace(
'/ /',
' ', $s);
119 $s = preg_replace(
'/(^|\n) /',
' ', $s);
122 $s = preg_replace(
'/\n/',
'<br>', $s);
125 # assert('preg_match(\'/¨/\', $s) === 0'); // check if no character ¨
126 # assert('preg_match(\'/\\\'/\', $s) === 0'); // check if no character ' (use ’ instead)
145 assert(
'is_string($s)');
146 assert(
'($search === null) || is_string($search)');
152 $pieces = preg_split(
'/(<.+?()>|&.+?;)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE);
153 foreach ($pieces as &$piece) {
154 if ((preg_match(
'/^<.+>$/', $piece) !== 1) && (preg_match(
'/^&.+;$/', $piece)) !== 1) {
155 $piece = preg_replace(
'/('.preg_quote(
$search,
'/').
')/i',
'<span class="highlight">$1</span>', $piece);
159 return implode($pieces);
187 public function id() {
212 return $this->marked;
236 return $this->subject;
299 public function to_html(
$search=null, $add_link=
true, $link_target=null,
300 $subject_tag=
'h2', $uneOPiCitation_link=
'uneOPiCitation.php') {
302 assert(
'($search === null) || is_string($search)');
303 assert(
'is_bool($add_link)');
304 assert(
'($link_target === null) || is_string($link_target)');
305 assert(
'is_string($subject_tag)');
306 assert(
'is_string($uneOPiCitation_link)');
309 $link_target = ($link_target
310 ?
' target="'.$link_target.
'"'
314 $is_marked = ($this->is_marked()
318 $a = array($this->is_maxim()
319 ?
'<section id="maxim-'.$this->
id().
'" class="maxim'.$is_marked.
'">'
320 :
'<section id="quotation-'.$this->id().
'" class="quotation'.$is_marked.
'">');
327 if ($this->is_marked()) {
328 $a[] =
' <div class="mark"></div>';
332 $a[] =
' <div class="id">'.($add_link
333 ? (
'<a href="./?id='.$this->id().
'"'.$link_target.
'>'.$this->id()
334 .
'</a><a href="'.$uneOPiCitation_link.
'?id='.$this->id()
335 .
'" target="_blank">↗❶</a>')
336 : $this->id()).
'</div>';
338 if ($this->subject() !== null) {
339 $html = $this->_highlight_html(htmlspecialchars($this->subject()),
$search);
340 $a[] =
' <'.$subject_tag.
' class="subject">'.($add_link
341 ?
'<a href="./?subject='.rawurlencode($this->subject()).
'"'.$link_target.
'>'.$html.
'</a>'
342 : $html).
'</'.$subject_tag.
'>';
347 if ($this->translation() === null) {
348 $a[] =
' <div class="text"><cite>'.$this->_highlight_html($this->_format_correct_html($this->text()),
$search).
'</cite></div>';
351 $a[] =
' <div class="text_translation">
352 <div class="text"><cite>'.$this->_highlight_html($this->_format_correct_html($this->text()),
$search).
'</cite></div>
353 <div class="translation"><cite>'.$this->_highlight_html($this->_format_correct_html($this->translation()),
$search).
'</cite></div>
354 <div class="clear"></div>
360 if ($this->is_maxim()) {
362 assert(
'$this->author() === null');
363 assert(
'$this->work() === null');
366 if ($this->nation() !== null) {
367 $html = $this->_highlight_html(htmlspecialchars($this->nation()),
$search);
368 $a[] =
' <div class="nation">'.($add_link
369 ?
'<a href="./?nation='.rawurlencode($this->nation()).
'"'.$link_target.
'>nation '.$html.
'</a>'
370 :
'nation '.$html).
'</div>';
375 assert(
'$this->nation() === null');
378 if ($this->author() !== null) {
379 $html = $this->_highlight_html(htmlspecialchars($this->author()),
$search);
380 $a[] =
' <div class="author">'.($add_link
381 ?
'<a href="./?author='.rawurlencode($this->author()).
'"'.$link_target.
'>'.$html.
'</a>'
385 if ($this->work() !== null) {
386 $html = $this->_highlight_html(htmlspecialchars($this->work()),
$search);
387 $a[] =
' <div class="work">'.($add_link
388 ?
'<a href="./?work='.rawurlencode($this->work()).
'"'.$link_target.
'>'.$html.
'</a>'
411 $quot = trim($this->text());
413 $quot = preg_replace(
'/<(b|c|i|sub|sup|\/b|\/c|\/i|\/sub|\/sup)>/',
'', $quot);
414 $quot = preg_replace(
'/¨/',
'', $quot);
416 $quot =
'"'.$quot.
'"';
418 if ($this->is_maxim()) {
419 if ($this->nation() !== null) {
421 (nation '.$this->nation().
')';
425 if ($this->author() !== null) {
426 if ($this->work() !== null) {
428 ('.$this->author().
'/
433 ('.$this->author().
')';
436 else if ($this->work() !== null) {
438 ('.$this->work().
')';
460 assert(
'is_string($url)');
463 $quot = $this->to_text();
500 assert(
'($url === null) || is_string($url)');
501 assert(
'($short_url_length === null) || is_int($short_url_length)');
502 assert(
'($short_url_length === null) || ($short_url_length >= 0)');
503 assert(
'is_int($max_length)');
504 assert(
'$max_length > 0');
507 $quot = $this->to_text();
530 return $this->translation;