|
1
|
|
|
<?
|
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
/*
|
|
4
|
|
|
* Copyright (c) 2003 Jose Solorzano. All rights reserved.
|
|
5
|
|
|
* Redistribution of source must retain this copyright notice.
|
|
6
|
|
|
*/
|
|
7
|
|
|
|
|
8
|
|
|
include ("htmlparser.inc");
|
|
9
|
|
|
|
|
10
|
|
|
/**
|
|
11
|
|
|
* Class Html2Text. (HtmlParser example.)
|
|
12
|
|
|
* Converts HTML to ASCII attempting to preserve
|
|
13
|
|
|
* document structure.
|
|
14
|
|
|
* To use, create an instance of Html2Text passing
|
|
15
|
|
|
* the text to convert and the desired maximum
|
|
16
|
|
|
* number of characters per line. Then invoke
|
|
17
|
|
|
* convert() which returns ASCII text.
|
|
18
|
|
|
*/
|
|
19
|
|
|
class Html2Text {
|
|
20
|
|
|
|
|
21
|
|
|
// Private fields
|
|
22
|
|
|
|
|
23
|
|
|
var $iCurrentLine = "";
|
|
24
|
|
|
var $iCurrentWord = "";
|
|
25
|
|
|
var $iCurrentWordArray;
|
|
26
|
|
|
var $iCurrentWordIndex;
|
|
27
|
|
|
var $iInScript;
|
|
28
|
|
|
var $iListLevel = 0;
|
|
29
|
|
|
var $iHtmlText;
|
|
30
|
|
|
var $iMaxColumns;
|
|
31
|
|
|
var $iHtmlParser;
|
|
32
|
|
|
|
|
33
|
|
|
// Constants
|
|
34
|
|
|
|
|
35
|
|
|
var $TOKEN_BR = 0;
|
|
36
|
|
|
var $TOKEN_P = 1;
|
|
37
|
|
|
var $TOKEN_LI = 2;
|
|
38
|
|
|
var $TOKEN_AFTERLI = 3;
|
|
39
|
|
|
var $TOKEN_UL = 4;
|
|
40
|
|
|
var $TOKEN_ENDUL = 5;
|
|
41
|
|
|
|
|
42
|
|
|
function Html2Text ($aHtmlText, $aMaxColumns) {
|
|
43
|
|
|
$this->iHtmlText = $aHtmlText;
|
|
44
|
|
|
$this->iMaxColumns = $aMaxColumns;
|
|
45
|
|
|
}
|
|
46
|
|
|
|
|
47
|
|
|
function convert() {
|
|
48
|
|
|
$this->iHtmlParser = new HtmlParser($this->iHtmlText);
|
|
49
|
|
|
$wholeText = "";
|
|
50
|
|
|
while (($line = $this->getLine()) !== false) {
|
|
51
|
|
|
$wholeText .= ($line . "\r\n");
|
|
52
|
|
|
}
|
|
53
|
|
|
return $wholeText;
|
|
54
|
|
|
}
|
|
55
|
|
|
|
|
56
|
|
|
function getLine() {
|
|
57
|
|
|
while (true) {
|
|
58
|
|
|
if (!$this->addWordToLine($this->iCurrentWord)) {
|
|
59
|
|
|
$retvalue = $this->iCurrentLine;
|
|
60
|
|
|
$this->iCurrentLine = "";
|
|
61
|
|
|
return $retvalue;
|
|
62
|
|
|
}
|
|
63
|
|
|
$word = $this->getWord();
|
|
64
|
|
|
if ($word === false) {
|
|
65
|
|
|
if ($this->iCurrentLine == "") {
|
|
66
|
|
|
break;
|
|
67
|
|
|
}
|
|
68
|
|
|
$retvalue = $this->iCurrentLine;
|
|
69
|
|
|
$this->iCurrentLine = "";
|
|
70
|
|
|
$this->iInText = false;
|
|
|
|
|
|
|
71
|
|
|
$this->iCurrentWord = "";
|
|
72
|
|
|
return $retvalue;
|
|
73
|
|
|
}
|
|
74
|
|
|
}
|
|
75
|
|
|
return false;
|
|
76
|
|
|
}
|
|
77
|
|
|
|
|
78
|
|
|
function addWordToLine ($word) {
|
|
79
|
|
|
if ($this->iInScript) {
|
|
80
|
|
|
return true;
|
|
81
|
|
|
}
|
|
82
|
|
|
$prevLine = $this->iCurrentLine;
|
|
83
|
|
|
if ($word === $this->TOKEN_BR) {
|
|
84
|
|
|
$this->iCurrentWord = "";
|
|
85
|
|
|
return false;
|
|
86
|
|
|
}
|
|
87
|
|
|
if ($word === $this->TOKEN_P) {
|
|
88
|
|
|
$this->iCurrentWord = $this->TOKEN_BR;
|
|
|
|
|
|
|
89
|
|
|
return false;
|
|
90
|
|
|
}
|
|
91
|
|
|
if ($word === $this->TOKEN_UL) {
|
|
92
|
|
|
$this->iCurrentWord = $this->TOKEN_BR;
|
|
93
|
|
|
return false;
|
|
94
|
|
|
}
|
|
95
|
|
|
if ($word === $this->TOKEN_ENDUL) {
|
|
96
|
|
|
$this->iCurrentWord = $this->TOKEN_BR;
|
|
97
|
|
|
return false;
|
|
98
|
|
|
}
|
|
99
|
|
|
if ($word === $this->TOKEN_LI) {
|
|
100
|
|
|
$this->iCurrentWord = $this->TOKEN_AFTERLI;
|
|
|
|
|
|
|
101
|
|
|
return false;
|
|
102
|
|
|
}
|
|
103
|
|
|
$toAdd = $word;
|
|
104
|
|
|
if ($word === $this->TOKEN_AFTERLI) {
|
|
105
|
|
|
$toAdd = "";
|
|
106
|
|
|
}
|
|
107
|
|
|
if ($prevLine != "") {
|
|
108
|
|
|
$prevLine .= " ";
|
|
109
|
|
|
}
|
|
110
|
|
|
else {
|
|
111
|
|
|
$prevLine = $this->getIndentation($word === $this->TOKEN_AFTERLI);
|
|
112
|
|
|
}
|
|
113
|
|
|
$candidateLine = $prevLine . $toAdd;
|
|
114
|
|
|
if (strlen ($candidateLine) > $this->iMaxColumns && $prevLine != "") {
|
|
115
|
|
|
return false;
|
|
116
|
|
|
}
|
|
117
|
|
|
$this->iCurrentLine = $candidateLine;
|
|
118
|
|
|
return true;
|
|
119
|
|
|
}
|
|
120
|
|
|
|
|
121
|
|
|
function getWord() {
|
|
122
|
|
|
while (true) {
|
|
123
|
|
|
if ($this->iHtmlParser->iNodeType == NODE_TYPE_TEXT) {
|
|
|
|
|
|
|
124
|
|
|
if (!$this->iInText) {
|
|
125
|
|
|
$words = $this->splitWords($this->iHtmlParser->iNodeValue);
|
|
|
|
|
|
|
126
|
|
|
$this->iCurrentWordArray = $words;
|
|
127
|
|
|
$this->iCurrentWordIndex = 0;
|
|
128
|
|
|
$this->iInText = true;
|
|
129
|
|
|
}
|
|
130
|
|
|
if ($this->iCurrentWordIndex < count($this->iCurrentWordArray)) {
|
|
131
|
|
|
$this->iCurrentWord = $this->iCurrentWordArray[$this->iCurrentWordIndex++];
|
|
132
|
|
|
return $this->iCurrentWord;
|
|
133
|
|
|
}
|
|
134
|
|
|
else {
|
|
135
|
|
|
$this->iInText = false;
|
|
136
|
|
|
}
|
|
137
|
|
|
}
|
|
138
|
|
|
else if ($this->iHtmlParser->iNodeType == NODE_TYPE_ELEMENT) {
|
|
|
|
|
|
|
139
|
|
|
if (strcasecmp ($this->iHtmlParser->iNodeName, "br") == 0) {
|
|
|
|
|
|
|
140
|
|
|
$this->iHtmlParser->parse();
|
|
141
|
|
|
$this->iCurrentWord = $this->TOKEN_BR;
|
|
|
|
|
|
|
142
|
|
|
return $this->iCurrentWord;
|
|
143
|
|
|
}
|
|
144
|
|
|
else if (strcasecmp ($this->iHtmlParser->iNodeName, "p") == 0) {
|
|
|
|
|
|
|
145
|
|
|
$this->iHtmlParser->parse();
|
|
146
|
|
|
$this->iCurrentWord = $this->TOKEN_P;
|
|
|
|
|
|
|
147
|
|
|
return $this->iCurrentWord;
|
|
148
|
|
|
}
|
|
149
|
|
|
else if (strcasecmp ($this->iHtmlParser->iNodeName, "script") == 0) {
|
|
|
|
|
|
|
150
|
|
|
$this->iHtmlParser->parse();
|
|
151
|
|
|
$this->iCurrentWord = "";
|
|
152
|
|
|
$this->iInScript = true;
|
|
153
|
|
|
return $this->iCurrentWord;
|
|
154
|
|
|
}
|
|
155
|
|
|
else if (strcasecmp ($this->iHtmlParser->iNodeName, "ul") == 0 || strcasecmp ($this->iHtmlParser->iNodeName, "ol") == 0) {
|
|
|
|
|
|
|
156
|
|
|
$this->iHtmlParser->parse();
|
|
157
|
|
|
$this->iCurrentWord = $this->TOKEN_UL;
|
|
|
|
|
|
|
158
|
|
|
$this->iListLevel++;
|
|
159
|
|
|
return $this->iCurrentWord;
|
|
160
|
|
|
}
|
|
161
|
|
|
else if (strcasecmp ($this->iHtmlParser->iNodeName, "li") == 0) {
|
|
|
|
|
|
|
162
|
|
|
$this->iHtmlParser->parse();
|
|
163
|
|
|
$this->iCurrentWord = $this->TOKEN_LI;
|
|
|
|
|
|
|
164
|
|
|
return $this->iCurrentWord;
|
|
165
|
|
|
}
|
|
166
|
|
|
}
|
|
167
|
|
|
else if ($this->iHtmlParser->iNodeType == NODE_TYPE_ENDELEMENT) {
|
|
|
|
|
|
|
168
|
|
|
if (strcasecmp ($this->iHtmlParser->iNodeName, "script") == 0) {
|
|
|
|
|
|
|
169
|
|
|
$this->iHtmlParser->parse();
|
|
170
|
|
|
$this->iCurrentWord = "";
|
|
171
|
|
|
$this->iInScript = false;
|
|
172
|
|
|
return $this->iCurrentWord;
|
|
173
|
|
|
}
|
|
174
|
|
|
else if (strcasecmp ($this->iHtmlParser->iNodeName, "ul") == 0 || strcasecmp ($this->iHtmlParser->iNodeName, "ol") == 0) {
|
|
|
|
|
|
|
175
|
|
|
$this->iHtmlParser->parse();
|
|
176
|
|
|
$this->iCurrentWord = $this->TOKEN_ENDUL;
|
|
|
|
|
|
|
177
|
|
|
if ($this->iListLevel > 0) {
|
|
178
|
|
|
$this->iListLevel--;
|
|
179
|
|
|
}
|
|
180
|
|
|
return $this->iCurrentWord;
|
|
181
|
|
|
}
|
|
182
|
|
|
}
|
|
183
|
|
|
if (!$this->iHtmlParser->parse()) {
|
|
184
|
|
|
break;
|
|
185
|
|
|
}
|
|
186
|
|
|
}
|
|
187
|
|
|
return false;
|
|
188
|
|
|
}
|
|
189
|
|
|
|
|
190
|
|
|
function splitWords ($text) {
|
|
191
|
|
|
$words = split ("[ \t\r\n]+", $text);
|
|
192
|
|
|
for ($idx = 0; $idx < count($words); $idx++) {
|
|
|
|
|
|
|
193
|
|
|
$words[$idx] = $this->htmlDecode($words[$idx]);
|
|
194
|
|
|
}
|
|
195
|
|
|
return $words;
|
|
196
|
|
|
}
|
|
197
|
|
|
|
|
198
|
|
|
function htmlDecode ($text) {
|
|
199
|
|
|
// TBD
|
|
200
|
|
|
return $text;
|
|
201
|
|
|
}
|
|
202
|
|
|
|
|
203
|
|
|
function getIndentation ($hasLI) {
|
|
204
|
|
|
$indent = "";
|
|
205
|
|
|
$idx = 0;
|
|
|
|
|
|
|
206
|
|
|
for ($idx = 0; $idx < ($this->iListLevel - 1); $idx++) {
|
|
207
|
|
|
$indent .= " ";
|
|
208
|
|
|
}
|
|
209
|
|
|
if ($this->iListLevel > 0) {
|
|
210
|
|
|
$indent = $hasLI ? ($indent . "- ") : ($indent . " ");
|
|
211
|
|
|
}
|
|
212
|
|
|
return $indent;
|
|
213
|
|
|
}
|
|
214
|
|
|
}
|
|
215
|
|
|
|
Short opening tags are disabled in PHP’s default configuration. In such a case, all content of this file is output verbatim to the browser without being parsed, or executed.
As a precaution to avoid these problems better use the long opening tag
<?php.