Skip to content

Commit 4fdc7b8

Browse files
committed
Merge pull request #254 from ivanlanin/develop
Basic HTML Reader
2 parents 4f93998 + 022cdeb commit 4fdc7b8

File tree

13 files changed

+522
-255
lines changed

13 files changed

+522
-255
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers
44

55
## 0.11.0 - Not yet released
66

7-
This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3. Four new elements were added: TextBox, ListItemRun, Field, and Line. Relative and absolute positioning for images and textboxes were added. Writer classes were refactored into parts, elements, and styles. ODT and RTF features were enhanced. Ability to add elements to PHPWord object via HTML were implemeted. RTF reader were initiated.
7+
This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3. Four new elements were added: TextBox, ListItemRun, Field, and Line. Relative and absolute positioning for images and textboxes were added. Writer classes were refactored into parts, elements, and styles. ODT and RTF features were enhanced. Ability to add elements to PHPWord object via HTML were implemeted. RTF and HTML reader were initiated.
88

99
### Features
1010

@@ -33,6 +33,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3. Four
3333
- RTF Reader: Basic RTF reader - @ivanlanin GH-72 GH-252
3434
- Element: New `Line` element - @basjan GH-253
3535
- Title: Ability to apply numbering in heading - @ivanlanin GH-193
36+
- HTML Reader: Basic HTML reader - @ivanlanin GH-80 GH-254
3637

3738
### Bugfixes
3839

docs/intro.rst

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -117,61 +117,61 @@ Writers
117117
Readers
118118
~~~~~~~
119119

120-
+---------------------------+----------------------+--------+-------+-------+
121-
| Features | | DOCX | ODT | RTF |
122-
+===========================+======================+========+=======+=======+
123-
| **Document Properties** | Standard || | |
124-
+---------------------------+----------------------+--------+-------+-------+
125-
| | Custom || | |
126-
+---------------------------+----------------------+--------+-------+-------+
127-
| **Element Type** | Text ||||
128-
+---------------------------+----------------------+--------+-------+-------+
129-
| | Text Run || | |
130-
+---------------------------+----------------------+--------+-------+-------+
131-
| | Title ||| |
132-
+---------------------------+----------------------+--------+-------+-------+
133-
| | Link || | |
134-
+---------------------------+----------------------+--------+-------+-------+
135-
| | Preserve Text || | |
136-
+---------------------------+----------------------+--------+-------+-------+
137-
| | Text Break || | |
138-
+---------------------------+----------------------+--------+-------+-------+
139-
| | Page Break || | |
140-
+---------------------------+----------------------+--------+-------+-------+
141-
| | List ||| |
142-
+---------------------------+----------------------+--------+-------+-------+
143-
| | Table || | |
144-
+---------------------------+----------------------+--------+-------+-------+
145-
| | Image || | |
146-
+---------------------------+----------------------+--------+-------+-------+
147-
| | Object | | | |
148-
+---------------------------+----------------------+--------+-------+-------+
149-
| | Watermark | | | |
150-
+---------------------------+----------------------+--------+-------+-------+
151-
| | Table of Contents | | | |
152-
+---------------------------+----------------------+--------+-------+-------+
153-
| | Header || | |
154-
+---------------------------+----------------------+--------+-------+-------+
155-
| | Footer || | |
156-
+---------------------------+----------------------+--------+-------+-------+
157-
| | Footnote || | |
158-
+---------------------------+----------------------+--------+-------+-------+
159-
| | Endnote || | |
160-
+---------------------------+----------------------+--------+-------+-------+
161-
| **Graphs** | 2D basic graphs | | | |
162-
+---------------------------+----------------------+--------+-------+-------+
163-
| | 2D advanced graphs | | | |
164-
+---------------------------+----------------------+--------+-------+-------+
165-
| | 3D graphs | | | |
166-
+---------------------------+----------------------+--------+-------+-------+
167-
| **Math** | OMML support | | | |
168-
+---------------------------+----------------------+--------+-------+-------+
169-
| | MathML support | | | |
170-
+---------------------------+----------------------+--------+-------+-------+
171-
| **Bonus** | Encryption | | | |
172-
+---------------------------+----------------------+--------+-------+-------+
173-
| | Protection | | | |
174-
+---------------------------+----------------------+--------+-------+-------+
120+
+---------------------------+----------------------+--------+-------+-------+-------+
121+
| Features | | DOCX | ODT | RTF | HTML |
122+
+===========================+======================+========+=======+=======+=======+
123+
| **Document Properties** | Standard || | | |
124+
+---------------------------+----------------------+--------+-------+-------+-------+
125+
| | Custom || | | |
126+
+---------------------------+----------------------+--------+-------+-------+-------+
127+
| **Element Type** | Text |||||
128+
+---------------------------+----------------------+--------+-------+-------+-------+
129+
| | Text Run || | | |
130+
+---------------------------+----------------------+--------+-------+-------+-------+
131+
| | Title ||| | |
132+
+---------------------------+----------------------+--------+-------+-------+-------+
133+
| | Link || | | |
134+
+---------------------------+----------------------+--------+-------+-------+-------+
135+
| | Preserve Text || | | |
136+
+---------------------------+----------------------+--------+-------+-------+-------+
137+
| | Text Break || | | |
138+
+---------------------------+----------------------+--------+-------+-------+-------+
139+
| | Page Break || | | |
140+
+---------------------------+----------------------+--------+-------+-------+-------+
141+
| | List ||| ||
142+
+---------------------------+----------------------+--------+-------+-------+-------+
143+
| | Table || | ||
144+
+---------------------------+----------------------+--------+-------+-------+-------+
145+
| | Image || | | |
146+
+---------------------------+----------------------+--------+-------+-------+-------+
147+
| | Object | | | | |
148+
+---------------------------+----------------------+--------+-------+-------+-------+
149+
| | Watermark | | | | |
150+
+---------------------------+----------------------+--------+-------+-------+-------+
151+
| | Table of Contents | | | | |
152+
+---------------------------+----------------------+--------+-------+-------+-------+
153+
| | Header || | | |
154+
+---------------------------+----------------------+--------+-------+-------+-------+
155+
| | Footer || | | |
156+
+---------------------------+----------------------+--------+-------+-------+-------+
157+
| | Footnote || | | |
158+
+---------------------------+----------------------+--------+-------+-------+-------+
159+
| | Endnote || | | |
160+
+---------------------------+----------------------+--------+-------+-------+-------+
161+
| **Graphs** | 2D basic graphs | | | | |
162+
+---------------------------+----------------------+--------+-------+-------+-------+
163+
| | 2D advanced graphs | | | | |
164+
+---------------------------+----------------------+--------+-------+-------+-------+
165+
| | 3D graphs | | | | |
166+
+---------------------------+----------------------+--------+-------+-------+-------+
167+
| **Math** | OMML support | | | | |
168+
+---------------------------+----------------------+--------+-------+-------+-------+
169+
| | MathML support | | | | |
170+
+---------------------------+----------------------+--------+-------+-------+-------+
171+
| **Bonus** | Encryption | | | | |
172+
+---------------------------+----------------------+--------+-------+-------+-------+
173+
| | Protection | | | | |
174+
+---------------------------+----------------------+--------+-------+-------+-------+
175175

176176
Contributing
177177
------------

docs/src/documentation.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -111,34 +111,34 @@ Below are the supported features for each file formats.
111111

112112
### Readers
113113

114-
| Features | | DOCX | ODT | RTF |
115-
|-------------------------|--------------------|------|-----|-----|
116-
| **Document Properties** | Standard || | |
117-
| | Custom || | |
118-
| **Element Type** | Text ||||
119-
| | Text Run || | |
120-
| | Title ||| |
121-
| | Link || | |
122-
| | Preserve Text || | |
123-
| | Text Break || | |
124-
| | Page Break || | |
125-
| | List ||| |
126-
| | Table || | |
127-
| | Image || | |
128-
| | Object | | | |
129-
| | Watermark | | | |
130-
| | Table of Contents | | | |
131-
| | Header || | |
132-
| | Footer || | |
133-
| | Footnote || | |
134-
| | Endnote || | |
135-
| **Graphs** | 2D basic graphs | | | |
136-
| | 2D advanced graphs | | | |
137-
| | 3D graphs | | | |
138-
| **Math** | OMML support | | | |
139-
| | MathML support | | | |
140-
| **Bonus** | Encryption | | | |
141-
| | Protection | | | |
114+
| Features | | DOCX | ODT | RTF | HTML|
115+
|-------------------------|--------------------|------|-----|-----|-----|
116+
| **Document Properties** | Standard || | | |
117+
| | Custom || | | |
118+
| **Element Type** | Text |||||
119+
| | Text Run || | | |
120+
| | Title ||| | |
121+
| | Link || | | |
122+
| | Preserve Text || | | |
123+
| | Text Break || | | |
124+
| | Page Break || | | |
125+
| | List ||| ||
126+
| | Table || | ||
127+
| | Image || | | |
128+
| | Object | | | | |
129+
| | Watermark | | | | |
130+
| | Table of Contents | | | | |
131+
| | Header || | | |
132+
| | Footer || | | |
133+
| | Footnote || | | |
134+
| | Endnote || | | |
135+
| **Graphs** | 2D basic graphs | | | | |
136+
| | 2D advanced graphs | | | | |
137+
| | 3D graphs | | | | |
138+
| **Math** | OMML support | | | | |
139+
| | MathML support | | | | |
140+
| **Bonus** | Encryption | | | | |
141+
| | Protection | | | | |
142142

143143
## Contributing
144144

samples/Sample_26_Html.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
$section = $phpWord->addSection();
99
$html = '<h1>Adding element via HTML</h1>';
1010
$html .= '<p>Some well formed HTML snippet needs to be used</p>';
11-
$html .= '<p>With for example <strong>some <em>inline</em> formatting</strong></p>';
11+
$html .= '<p>With for example <strong>some<sup>1</sup> <em>inline</em> formatting</strong><sub>1</sub></p>';
12+
$html .= '<p>Unordered (bulleted) list:</p>';
13+
$html .= '<ul><li>Item 1</li><li>Item 2</li><ul><li>Item 2.1</li><li>Item 2.1</li></ul></ul>';
14+
$html .= '<p>Ordered (numbered) list:</p>';
15+
$html .= '<ol><li>Item 1</li><li>Item 2</li></ol>';
1216

1317
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
1418

samples/Sample_30_ReadHTML.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
include_once 'Sample_Header.php';
3+
4+
// Read contents
5+
$name = basename(__FILE__, '.php');
6+
$source = realpath(__DIR__ . "/resources/{$name}.html");
7+
8+
echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
9+
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'HTML');
10+
11+
// Save file
12+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
13+
if (!CLI) {
14+
include_once 'Sample_Footer.php';
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8" />
4+
<title>PHPWord</title>
5+
</head>
6+
<body>
7+
<h1>Adding element via HTML</h1>
8+
<p>Some well formed HTML snippet needs to be used</p>
9+
<p>With for example <strong>some<sup>1</sup> <em>inline</em> formatting</strong><sub>1</sub></p>
10+
<p>Unordered (bulleted) list:</p>
11+
<ul><li>Item 1</li><li>Item 2</li><ul><li>Item 2.1</li><li>Item 2.1</li></ul></ul>
12+
<p>Ordered (numbered) list:</p>
13+
<ol><li>Item 1</li><li>Item 2</li></ol>
14+
</body>
15+
</html>

src/PhpWord/IOFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
5151
*/
5252
public static function createReader($name = 'Word2007')
5353
{
54-
if (!in_array($name, array('ReaderInterface', 'Word2007', 'ODText', 'RTF'))) {
54+
if (!in_array($name, array('ReaderInterface', 'Word2007', 'ODText', 'RTF', 'HTML'))) {
5555
throw new Exception("\"{$name}\" is not a valid reader.");
5656
}
5757

src/PhpWord/Reader/HTML.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Reader;
19+
20+
use PhpOffice\PhpWord\PhpWord;
21+
use PhpOffice\PhpWord\Shared\Html as HTMLParser;
22+
23+
/**
24+
* HTML Reader class
25+
*
26+
* @since 0.11.0
27+
*/
28+
class HTML extends AbstractReader implements ReaderInterface
29+
{
30+
/**
31+
* Loads PhpWord from file
32+
*
33+
* @param string $docFile
34+
* @throws \Exception
35+
* @return \PhpOffice\PhpWord\PhpWord
36+
*/
37+
public function load($docFile)
38+
{
39+
$phpWord = new PhpWord();
40+
41+
if ($this->canRead($docFile)) {
42+
$section = $phpWord->addSection();
43+
HTMLParser::addHtml($section, file_get_contents($docFile), true);
44+
} else {
45+
throw new \Exception("Cannot read {$docFile}.");
46+
}
47+
48+
return $phpWord;
49+
}
50+
}

0 commit comments

Comments
 (0)