--- db/prgsrc/drupal/modules/chgk_db/classes/DbField.class.php 2010/03/20 17:23:42 1.5
+++ db/prgsrc/drupal/modules/chgk_db/classes/DbField.class.php 2010/04/24 18:04:15 1.8
@@ -7,6 +7,11 @@ class DbField {
protected $value;
protected $number;
protected $html;
+ protected $paragraphs;
+ protected $fb2;
+ protected $poems;
+ protected $codes;
+
public function __construct($field, $value, $number = false) {
$this->field = $field;
$this->value = $value;
@@ -23,7 +28,127 @@ class DbField {
$this->formatHtml();
return $this->html;
}
-
+
+ protected function updateFirstParagraph() {
+ $this->paragraphs[0] =
+ ''.$this->getName().
+ ': '. $this->paragraphs[0];
+ }
+ public function getFb2() {
+ if ($this->fb2) {
+ return $this->fb2;
+ }
+ $this->fb2 = $this->value;
+ $this->fb2 = html_entity_decode($this->fb2, ENT_COMPAT, 'UTF-8');
+ $this->fb2 = htmlspecialchars($this->fb2, ENT_NOQUOTES, 'UTF-8');
+ $this->split();
+ $this->updateFirstParagraph();
+ $poemStarts = $poemEnds = array();
+ $codeStarts = $codeEnds = array();
+
+ foreach ($this->poems as $p) {
+ list($b, $e) = $p;
+ $poemStarts[] = $b;
+ $poemEnds[] = $e;
+ }
+
+ foreach ($this->codes as $p) {
+ list($b, $e) = $p;
+ $codeStarts[] = $b;
+ $codeEnds[] = $e;
+ }
+
+ $inpoem = FALSE;
+ $incode = FALSE;
+ $result = '';
+ foreach ($this->paragraphs as $k=>$p) {
+ if (in_array($k, $poemStarts)) {
+ $inpoem = TRUE;
+ $result .= "\n";
+ }
+ if (in_array($k, $codeStarts)) {
+ $incode = TRUE;
+ $result .= "\n";
+ }
+
+ if ($incode) {
+ $result .= "$p\n";
+ } elseif ($inpoem) {
+ $result.="$p\n";
+ } else {
+ $result.="$p
\n";
+ }
+ if (in_array($k, $poemEnds)) {
+ $result .= "";
+
+ $inpoem = FALSE;
+ }
+
+ if (in_array($k, $codeEnds)) {
+ $incode = FALSE;
+ $result .= "\n";
+ }
+
+ }
+ $this->fb2 = $result;
+ $this->fb2 = preg_replace('/ -+(\s+)/',' —$1', $this->fb2);
+ $this->fb2 = preg_replace('/\(pic: ([^\)]*)\)/','', $this->fb2);
+ return $this->fb2;
+ }
+
+ protected function split() {
+ $lines = split ("\n", $this->fb2);
+ $this->paragraphs = array();
+ $current = '';
+ foreach ($lines as $l) {
+ if (preg_match('/^[\s\|]/', $l)) {
+ $this->paragraphs[] = $current;
+ $current = $l ."\n";
+ } else {
+ $current .= $l."\n" ;
+ }
+ }
+ $this->paragraphs[] = $current;
+ $sp = '';
+ $begin = $end = 0;
+ $incode = FALSE;
+ $this->poems = array();
+ $this->codes = array();
+ foreach ($this->paragraphs as $k=>$p) {
+ if (preg_match('/^\|/', $p )) {
+ $this->paragraphs[$k] = preg_replace('/^\|/', '',
+ $this->paragraphs[$k]);
+ if (!$incode) {
+ $cbegin = $k;
+ $incode = TRUE;
+ }
+ } else {
+ if ($incode) {
+ $this->codes[] = array($cbegin, $k);
+ }
+ $incode = FALSE;
+ }
+ $csp = preg_replace('/\S.*/', '', $p);
+ if ($csp == $sp) {
+ $end = $k;
+ }
+ else {
+ if ($begin!=$end && $csp) {
+ $this->poems[] = array($begin, $end);
+ }
+ $begin = $end = $k;
+ $sp = $csp;
+ }
+ }
+ if ($incode) {
+ $this->codes[] = array($cbegin, $k);
+ }
+
+ if ($begin!=$end && $csp) {
+ $this->poems[] = array($begin, $end);
+ }
+ }
+
public function formatHtml() {
$this->html = preg_replace('/(\s+)-+(\s+)/','\1—$2', $this->html);
@@ -58,4 +183,11 @@ class DbField {
public function getValue() {
return $this->value;
}
+
+ public function getImages() {
+ $m = preg_match_all('/\(pic:\s*(.*?)\)/', $this->value,
+ $matches, PREG_PATTERN_ORDER);
+ $this->images = $matches[1];
+ return $this->images;
+ }
}