Skip to content

Commit 96e2ea1

Browse files
authored
Merge pull request #104 from harlong/urls
URLs support
2 parents da22ced + d4a570a commit 96e2ea1

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/Html/HtmlFormatter.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ public function Format(Document $document)
5454
array_push($this->states, $this->state);
5555

5656
// Keep track of opened html tags
57-
$this->openedTags = array('span' => false, 'p' => false);
57+
$this->openedTags = array('span' => false, 'p' => false, 'a' => false);
5858
// Create the first paragraph
5959
$this->OpenTag('p');
6060
// Begin format
6161
$this->ProcessGroup($document->root);
6262
// Instead of removing opened tags, we close them
6363
$append = $this->openedTags['span'] ? '</span>' : '';
64+
$append .= $this->openedTags['a'] ? '</a>' : '';
6465
$append .= $this->openedTags['p'] ? '</p>' : '';
6566

6667
return $this->output . $append;
@@ -240,6 +241,11 @@ protected function ProcessGroup($group)
240241
// Pictures extraction not yet supported
241242
//if(substr($group->GetType(), 0, 4) == "pict") return;
242243

244+
// If a destination was a HYPERLINK
245+
if ($this->state->href) {
246+
$this->OpenTag('a','target="_blank" href='.$this->state->href);
247+
}
248+
243249
// Push a new state onto the stack:
244250
$this->state = clone $this->state;
245251
array_push($this->states, $this->state);
@@ -262,6 +268,10 @@ protected function ProcessDestination($dest)
262268
for ($i=2;$i<$c;$i++)
263269
$this->FormatEntry($dest[$i]);
264270
}
271+
if ($dest[1]->word == "fldinst" && count($dest)>=2 && substr($dest[2]->text,0,10)=="HYPERLINK " ) {
272+
$url=substr($dest[2]->text,10);
273+
$this->state->href=$url;
274+
}
265275
}
266276

267277
protected function FormatEntry($entry)
@@ -452,9 +462,11 @@ protected function CloseTag($tag)
452462

453463
protected function CloseTags()
454464
{
455-
// Close all opened tags
465+
// Close all opened tags but p
456466
foreach ($this->openedTags as $tag => $b)
457-
$this->CloseTag($tag);
467+
if ($tag!='p') $this->CloseTag($tag);
468+
// And than close p as a last one
469+
if ($this->openedTags['p']) $this->CloseTag('p');
458470
}
459471

460472
protected function FormatControlSymbol($symbol)

src/Html/State.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function Reset($defaultFont = null)
6060
$this->background = null;
6161
$this->hcolor = null;
6262
$this->font = isset($defaultFont) ? $defaultFont : null;
63+
$this->href = null;
6364
}
6465

6566
public function PrintStyle()
@@ -122,6 +123,9 @@ public function equals($state)
122123
// Compare fonts
123124
if ($this->font != $state->font) return false;
124125

126+
// Compare urls
127+
if ($this->href != $state->href) return false;
128+
125129
return true;
126130
}
127131

@@ -304,4 +308,4 @@ public function setFont($font): self
304308

305309
return $this;
306310
}
307-
}
311+
}

0 commit comments

Comments
 (0)