123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765 |
- <?php
- foreach (array('LEXER_ENTER', 'LEXER_MATCHED',
- 'LEXER_UNMATCHED', 'LEXER_EXIT',
- 'LEXER_SPECIAL') as $i => $constant) {
- if (! defined($constant)) {
- define($constant, $i + 1);
- }
- }
- class ParallelRegex {
- var $_patterns;
- var $_labels;
- var $_regex;
- var $_case;
-
- function ParallelRegex($case) {
- $this->_case = $case;
- $this->_patterns = array();
- $this->_labels = array();
- $this->_regex = null;
- }
-
- function addPattern($pattern, $label = true) {
- $count = count($this->_patterns);
- $this->_patterns[$count] = $pattern;
- $this->_labels[$count] = $label;
- $this->_regex = null;
- }
-
- function match($subject, &$match) {
- if (count($this->_patterns) == 0) {
- return false;
- }
- if (! preg_match($this->_getCompoundedRegex(), $subject, $matches)) {
- $match = '';
- return false;
- }
- $match = $matches[0];
- for ($i = 1; $i < count($matches); $i++) {
- if ($matches[$i]) {
- return $this->_labels[$i - 1];
- }
- }
- return true;
- }
-
- function _getCompoundedRegex() {
- if ($this->_regex == null) {
- for ($i = 0, $count = count($this->_patterns); $i < $count; $i++) {
- $this->_patterns[$i] = '(' . str_replace(
- array('/', '(', ')'),
- array('\/', '\(', '\)'),
- $this->_patterns[$i]) . ')';
- }
- $this->_regex = "/" . implode("|", $this->_patterns) . "/" . $this->_getPerlMatchingFlags();
- }
- return $this->_regex;
- }
-
- function _getPerlMatchingFlags() {
- return ($this->_case ? "msS" : "msSi");
- }
- }
- class SimpleStateStack {
- var $_stack;
-
- function SimpleStateStack($start) {
- $this->_stack = array($start);
- }
-
- function getCurrent() {
- return $this->_stack[count($this->_stack) - 1];
- }
-
- function enter($state) {
- array_push($this->_stack, $state);
- }
-
- function leave() {
- if (count($this->_stack) == 1) {
- return false;
- }
- array_pop($this->_stack);
- return true;
- }
- }
- class SimpleLexer {
- var $_regexes;
- var $_parser;
- var $_mode;
- var $_mode_handlers;
- var $_case;
-
- function SimpleLexer(&$parser, $start = "accept", $case = false) {
- $this->_case = $case;
- $this->_regexes = array();
- $this->_parser = &$parser;
- $this->_mode = &new SimpleStateStack($start);
- $this->_mode_handlers = array($start => $start);
- }
-
- function addPattern($pattern, $mode = "accept") {
- if (! isset($this->_regexes[$mode])) {
- $this->_regexes[$mode] = new ParallelRegex($this->_case);
- }
- $this->_regexes[$mode]->addPattern($pattern);
- if (! isset($this->_mode_handlers[$mode])) {
- $this->_mode_handlers[$mode] = $mode;
- }
- }
-
- function addEntryPattern($pattern, $mode, $new_mode) {
- if (! isset($this->_regexes[$mode])) {
- $this->_regexes[$mode] = new ParallelRegex($this->_case);
- }
- $this->_regexes[$mode]->addPattern($pattern, $new_mode);
- if (! isset($this->_mode_handlers[$new_mode])) {
- $this->_mode_handlers[$new_mode] = $new_mode;
- }
- }
-
- function addExitPattern($pattern, $mode) {
- if (! isset($this->_regexes[$mode])) {
- $this->_regexes[$mode] = new ParallelRegex($this->_case);
- }
- $this->_regexes[$mode]->addPattern($pattern, "__exit");
- if (! isset($this->_mode_handlers[$mode])) {
- $this->_mode_handlers[$mode] = $mode;
- }
- }
-
- function addSpecialPattern($pattern, $mode, $special) {
- if (! isset($this->_regexes[$mode])) {
- $this->_regexes[$mode] = new ParallelRegex($this->_case);
- }
- $this->_regexes[$mode]->addPattern($pattern, "_$special");
- if (! isset($this->_mode_handlers[$special])) {
- $this->_mode_handlers[$special] = $special;
- }
- }
-
- function mapHandler($mode, $handler) {
- $this->_mode_handlers[$mode] = $handler;
- }
-
- function parse($raw) {
- if (! isset($this->_parser)) {
- return false;
- }
- $length = strlen($raw);
- while (is_array($parsed = $this->_reduce($raw))) {
- list($raw, $unmatched, $matched, $mode) = $parsed;
- if (! $this->_dispatchTokens($unmatched, $matched, $mode)) {
- return false;
- }
- if ($raw === '') {
- return true;
- }
- if (strlen($raw) == $length) {
- return false;
- }
- $length = strlen($raw);
- }
- if (! $parsed) {
- return false;
- }
- return $this->_invokeParser($raw, LEXER_UNMATCHED);
- }
-
- function _dispatchTokens($unmatched, $matched, $mode = false) {
- if (! $this->_invokeParser($unmatched, LEXER_UNMATCHED)) {
- return false;
- }
- if (is_bool($mode)) {
- return $this->_invokeParser($matched, LEXER_MATCHED);
- }
- if ($this->_isModeEnd($mode)) {
- if (! $this->_invokeParser($matched, LEXER_EXIT)) {
- return false;
- }
- return $this->_mode->leave();
- }
- if ($this->_isSpecialMode($mode)) {
- $this->_mode->enter($this->_decodeSpecial($mode));
- if (! $this->_invokeParser($matched, LEXER_SPECIAL)) {
- return false;
- }
- return $this->_mode->leave();
- }
- $this->_mode->enter($mode);
- return $this->_invokeParser($matched, LEXER_ENTER);
- }
-
- function _isModeEnd($mode) {
- return ($mode === "__exit");
- }
-
- function _isSpecialMode($mode) {
- return (strncmp($mode, "_", 1) == 0);
- }
-
- function _decodeSpecial($mode) {
- return substr($mode, 1);
- }
-
- function _invokeParser($content, $is_match) {
- if (($content === '') || ($content === false)) {
- return true;
- }
- $handler = $this->_mode_handlers[$this->_mode->getCurrent()];
- return $this->_parser->$handler($content, $is_match);
- }
-
- function _reduce($raw) {
- $match = array();
- if ($action = $this->_regexes[$this->_mode->getCurrent()]->match($raw, $match)) {
- $unparsed_character_count = strpos($raw, $match);
- $unparsed = substr($raw, 0, $unparsed_character_count);
- $raw = substr($raw, $unparsed_character_count + strlen($match));
- return array($raw, $unparsed, $match, $action);
- }
- return true;
- }
- }
- class SimpleHtmlLexer extends SimpleLexer {
-
- function SimpleHtmlLexer(&$parser) {
- $this->SimpleLexer($parser, 'text');
- $this->mapHandler('text', 'acceptTextToken');
- $this->_addSkipping();
- foreach ($this->_getParsedTags() as $tag) {
- $this->_addTag($tag);
- }
- $this->_addInTagTokens();
- }
-
- function _getParsedTags() {
- return array('a', 'base', 'title', 'form', 'input', 'button', 'textarea', 'select',
- 'option', 'frameset', 'frame', 'label');
- }
-
- function _addSkipping() {
- $this->mapHandler('css', 'ignore');
- $this->addEntryPattern('<style', 'text', 'css');
- $this->addExitPattern('</style>', 'css');
- $this->mapHandler('js', 'ignore');
- $this->addEntryPattern('<script', 'text', 'js');
- $this->addExitPattern('</script>', 'js');
- $this->mapHandler('comment', 'ignore');
- $this->addEntryPattern('<!--', 'text', 'comment');
- $this->addExitPattern('-->', 'comment');
- }
-
- function _addTag($tag) {
- $this->addSpecialPattern("</$tag>", 'text', 'acceptEndToken');
- $this->addEntryPattern("<$tag", 'text', 'tag');
- }
-
- function _addInTagTokens() {
- $this->mapHandler('tag', 'acceptStartToken');
- $this->addSpecialPattern('\s+', 'tag', 'ignore');
- $this->_addAttributeTokens();
- $this->addExitPattern('/>', 'tag');
- $this->addExitPattern('>', 'tag');
- }
-
- function _addAttributeTokens() {
- $this->mapHandler('dq_attribute', 'acceptAttributeToken');
- $this->addEntryPattern('=\s*"', 'tag', 'dq_attribute');
- $this->addPattern("\\\\\"", 'dq_attribute');
- $this->addExitPattern('"', 'dq_attribute');
- $this->mapHandler('sq_attribute', 'acceptAttributeToken');
- $this->addEntryPattern("=\s*'", 'tag', 'sq_attribute');
- $this->addPattern("\\\\'", 'sq_attribute');
- $this->addExitPattern("'", 'sq_attribute');
- $this->mapHandler('uq_attribute', 'acceptAttributeToken');
- $this->addSpecialPattern('=\s*[^>\s]*', 'tag', 'uq_attribute');
- }
- }
- class SimpleHtmlSaxParser {
- var $_lexer;
- var $_listener;
- var $_tag;
- var $_attributes;
- var $_current_attribute;
-
- function SimpleHtmlSaxParser(&$listener) {
- $this->_listener = &$listener;
- $this->_lexer = &$this->createLexer($this);
- $this->_tag = '';
- $this->_attributes = array();
- $this->_current_attribute = '';
- }
-
- function parse($raw) {
- return $this->_lexer->parse($raw);
- }
-
- function &createLexer(&$parser) {
- $lexer = &new SimpleHtmlLexer($parser);
- return $lexer;
- }
-
- function acceptStartToken($token, $event) {
- if ($event == LEXER_ENTER) {
- $this->_tag = strtolower(substr($token, 1));
- return true;
- }
- if ($event == LEXER_EXIT) {
- $success = $this->_listener->startElement(
- $this->_tag,
- $this->_attributes);
- $this->_tag = '';
- $this->_attributes = array();
- return $success;
- }
- if ($token != '=') {
- $this->_current_attribute = strtolower(SimpleHtmlSaxParser::decodeHtml($token));
- $this->_attributes[$this->_current_attribute] = '';
- }
- return true;
- }
-
- function acceptEndToken($token, $event) {
- if (! preg_match('/<\/(.*)>/', $token, $matches)) {
- return false;
- }
- return $this->_listener->endElement(strtolower($matches[1]));
- }
-
- function acceptAttributeToken($token, $event) {
- if ($this->_current_attribute) {
- if ($event == LEXER_UNMATCHED) {
- $this->_attributes[$this->_current_attribute] .=
- SimpleHtmlSaxParser::decodeHtml($token);
- }
- if ($event == LEXER_SPECIAL) {
- $this->_attributes[$this->_current_attribute] .=
- preg_replace('/^=\s*/' , '', SimpleHtmlSaxParser::decodeHtml($token));
- }
- }
- return true;
- }
-
- function acceptEntityToken($token, $event) {
- }
-
- function acceptTextToken($token, $event) {
- return $this->_listener->addContent($token);
- }
-
- function ignore($token, $event) {
- return true;
- }
-
- function decodeHtml($html) {
- return html_entity_decode($html, ENT_QUOTES);
- }
-
- function normalise($html) {
- $text = preg_replace('|<!--.*?-->|', '', $html);
- $text = preg_replace('|<script[^>]*>.*?</script>|', '', $text);
- $text = preg_replace('|<img[^>]*alt\s*=\s*"([^"]*)"[^>]*>|', ' \1 ', $text);
- $text = preg_replace('|<img[^>]*alt\s*=\s*\'([^\']*)\'[^>]*>|', ' \1 ', $text);
- $text = preg_replace('|<img[^>]*alt\s*=\s*([a-zA-Z_]+)[^>]*>|', ' \1 ', $text);
- $text = preg_replace('|<[^>]*>|', '', $text);
- $text = SimpleHtmlSaxParser::decodeHtml($text);
- $text = preg_replace('|\s+|', ' ', $text);
- return trim(trim($text), "\xA0");
- }
- }
- class SimpleSaxListener {
-
- function SimpleSaxListener() {
- }
-
- function startElement($name, $attributes) {
- }
-
- function endElement($name) {
- }
-
- function addContent($text) {
- }
- }
- ?>
|