From 52b3506bbb5f0bedd8d6b3490e12dbc3bb70b2af Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Wed, 16 Apr 2014 17:17:42 +0700 Subject: [PATCH] HTML Writer: Enable footnotes and endnotes --- CHANGELOG.md | 2 +- docs/intro.rst | 4 +- src/PhpWord/Writer/HTML.php | 104 ++++++++++++++++++++++++++++++------ 3 files changed, 92 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b9cf9a2..759f41e8 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,7 @@ This release marked heavy refactorings on internal code structure with the creat - ListItem: Ability to create custom list and reset list number - @ivanlanin GH-10 GH-198 - ODT Writer: Basic table writing support - @ivanlanin - Image: Keep image aspect ratio if only 1 dimension styled - @japonicus GH-194 -- HTML Writer: Basic HTML writer: text, textrun, link, title, textbreak, table, image (as Base64) - @ivanlanin GH-203 GH-67 GH-147 +- HTML Writer: Basic HTML writer: text, textrun, link, title, textbreak, table, image (as Base64), footnote, endnote - @ivanlanin GH-203 GH-67 GH-147 - PDF Writer: Basic PDF writer using DomPDF: All HTML element except image - @ivanlanin GH-68 ### Bugfixes diff --git a/docs/intro.rst b/docs/intro.rst index 53a352fe..a617777d 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -101,9 +101,9 @@ Writers + +-----------------------+--------+-------+-------+-------+-------+ | | Footer | ✓ | | | | | + +-----------------------+--------+-------+-------+-------+-------+ -| | Footnote | ✓ | | | | | +| | Footnote | ✓ | | | ✓ | | + +-----------------------+--------+-------+-------+-------+-------+ -| | Endnote | ✓ | | | | | +| | Endnote | ✓ | | | ✓ | | +-------------------------+-----------------------+--------+-------+-------+-------+-------+ | **Graphs** | 2D basic graphs | | | | | | + +-----------------------+--------+-------+-------+-------+-------+ diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index 3baa15e0..bd98e893 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -22,7 +22,9 @@ use PhpOffice\PhpWord\Element\Text; use PhpOffice\PhpWord\Element\TextBreak; use PhpOffice\PhpWord\Element\TextRun; use PhpOffice\PhpWord\Element\Title; +use PhpOffice\PhpWord\Endnotes; use PhpOffice\PhpWord\Exception\Exception; +use PhpOffice\PhpWord\Footnotes; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style\Font; @@ -42,6 +44,13 @@ class HTML extends AbstractWriter implements WriterInterface */ protected $isPdf = false; + /** + * Footnotes and endnotes collection + * + * @var array + */ + protected $notes = array(); + /** * Create new instance */ @@ -85,6 +94,7 @@ class HTML extends AbstractWriter implements WriterInterface $html .= '' . PHP_EOL; $html .= '' . PHP_EOL; $html .= $this->writeHTMLBody(); + $html .= $this->writeNotes(); $html .= '' . PHP_EOL; $html .= '' . PHP_EOL; @@ -181,6 +191,32 @@ class HTML extends AbstractWriter implements WriterInterface return $html; } + /** + * Write footnote/endnote contents + */ + private function writeNotes() + { + $footnote = Footnotes::getElements(); + $endnote = Endnotes::getElements(); + $html = ''; + + if (count($this->notes) > 0) { + $html .= "
"; + foreach ($this->notes as $noteId => $noteMark) { + $noteAnchor = "note-{$noteId}"; + list($noteType, $noteTypeId) = explode('-', $noteMark); + $collection = $$noteType; + if (array_key_exists($noteTypeId, $collection)) { + $element = $collection[$noteTypeId]; + $content = "{$noteId}" . $this->writeTextRun($element, true); + $html .= "

{$content}

" . PHP_EOL; + } + } + } + + return $html; + } + /** * Get text * @@ -226,19 +262,20 @@ class HTML extends AbstractWriter implements WriterInterface } /** - * Get text run content + * Write text run content * - * @param TextRun $textrun + * @param TextRun|Footnote|Endnote $textrun * @return string */ - private function writeTextRun($textrun) + private function writeTextRun($textrun, $withoutP = false) { $html = ''; $elements = $textrun->getElements(); if (count($elements) > 0) { $paragraphStyle = $textrun->getParagraphStyle(); $spIsObject = ($paragraphStyle instanceof Paragraph); - $html .= 'writeFootnote($element); } } - $html .= '

' . PHP_EOL; + $html .= $withoutP ? '' : '

'; + $html .= PHP_EOL; } return $html; @@ -279,11 +317,14 @@ class HTML extends AbstractWriter implements WriterInterface { $url = $element->getLinkSrc(); $text = $element->getLinkName(); + if ($text == '') { + $text = $url; + } $html = ''; if (!$withoutP) { $html .= "

" . PHP_EOL; } - $html .= "{$text}" . PHP_EOL; + $html .= "{$text}" . PHP_EOL; if (!$withoutP) { $html .= "

" . PHP_EOL; } @@ -467,7 +508,7 @@ class HTML extends AbstractWriter implements WriterInterface */ private function writeFootnote($element) { - return $this->writeUnsupportedElement($element, true); + return $this->writeNote($element); } /** @@ -478,7 +519,25 @@ class HTML extends AbstractWriter implements WriterInterface */ private function writeEndnote($element) { - return $this->writeUnsupportedElement($element, true); + return $this->writeNote($element); + } + + /** + * Write footnote/endnote marks + * + * @param Footnote|Endnote $element + * @return string + */ + private function writeNote($element) + { + $index = count($this->notes) + 1; + $prefix = ($element instanceof Endnote) ? 'endnote' : 'footnote'; + $noteMark = $prefix . '-' . $element->getRelationId(); + $noteAnchor = "note-{$index}"; + $this->notes[$index] = $noteMark; + $html = "{$index}"; + + return $html; } /** @@ -509,18 +568,33 @@ class HTML extends AbstractWriter implements WriterInterface */ private function writeStyles() { - $bodyCss = array(); $css = '