1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
#!/usr/local/bin/python
""" HTML - tag a HTML string (Version 0.6)
Copyright (c) 2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
Copyright (c) 2000-2014, eGenix.com Software GmbH; mailto:info@egenix.com
See the documentation for further information on copyrights,
or contact the author. All Rights Reserved.
"""
import sys
# constants + engine
from mx.TextTools import *
# ErrorTag
error = '*syntax error' # error tag obj
tagname_charset = CharSet(alpha+'\-'+number)
tagattrname_charset = CharSet(alpha+'\-'+number)
tagvalue_charset = CharSet('^"\'> ')
white_charset = CharSet(' \r\n\t')
tagname_charset = CharSet(alpha+'\-'+number)
tagattrname_charset = CharSet(alpha+'\-'+number)
tagvalue_charset = CharSet('^"\'> ')
white_charset = CharSet(' \r\n\t')
tagattr = (
# name
('name',AllInCharSet,tagattrname_charset),
# with value ?
(None,Is,'=',MatchOk),
# skip junk
(None,AllInCharSet,white_charset,+1),
# unquoted value
('value',AllInCharSet,tagvalue_charset,+1,MatchOk),
# double quoted value
(None,Is,'"',+5),
('value',AllNotIn,'"',+1,+2),
('value',Skip,0),
(None,Is,'"'),
(None,Jump,To,MatchOk),
# single quoted value
(None,Is,'\''),
('value',AllNotIn,'\'',+1,+2),
('value',Skip,0),
(None,Is,'\'')
)
valuetable = (
# ignore whitespace + '='
(None,AllInCharSet,CharSet(' \r\n\t='),+1),
# unquoted value
('value',AllInCharSet,tagvalue_charset,+1,MatchOk),
# double quoted value
(None,Is,'"',+5),
('value',AllNotIn,'"',+1,+2),
('value',Skip,0),
(None,Is,'"'),
(None,Jump,To,MatchOk),
# single quoted value
(None,Is,'\''),
('value',AllNotIn,'\'',+1,+2),
('value',Skip,0),
(None,Is,'\'')
)
allattrs = (
# look for attributes
(None,AllInCharSet,white_charset,+4),
(None,Is,'>',+1,MatchOk),
('tagattr',Table,tagattr),
(None,Jump,To,-3),
(None,Is,'>',+1,MatchOk),
# handle incorrect attributes
(error,AllNotIn,'> \r\n\t'),
(None,Jump,To,-6)
)
# NOTE: The htmltag tag table assumes that the input text is given
# in upper case letters (see <XMP> handling).
htmltag = (
(None,Is,'<'),
# is this a closing tag ?
('closetag',Is,'/',+1),
# a coment ?
('comment',Is,'!',+8),
(None,Word,'--',+4),
('text',WordStart,'-->',+1),
(None,Skip,3),
(None,Jump,To,MatchOk),
# a SGML-Tag ?
('other',AllNotIn,'>',+1),
(None,Is,'>'),
(None,Jump,To,MatchOk),
# XMP-Tag ?
('tagname',Word,'XMP',+5),
(None,Is,'>'),
('text',WordStart,'</XMP>'),
(None,Skip,len('</XMP>')),
(None,Jump,To,MatchOk),
# get the tag name
('tagname',AllInCharSet,tagname_charset),
# look for attributes
(None,AllInCharSet,white_charset,+4),
(None,Is,'>',+1,MatchOk),
('tagattr',Table,tagattr),
(None,Jump,To,-3),
(None,Is,'>',+1,MatchOk),
# handle incorrect attributes
(error,AllNotIn,'> \n\r\t'),
(None,Jump,To,-6)
)
htmltable = (# HTML-Tag
('htmltag',Table,htmltag,+1,+4),
# not HTML, but still using this syntax: error or inside XMP-tag !
(error,Is,'<',+3),
(error,AllNotIn,'>',+1),
(error,Is,'>'),
# normal text
('text',AllNotIn,'<',+1),
# end of file
('eof',EOF,Here,-5),
)
if __name__ == '__main__':
t = TextTools._timer()
# read file
f = open(sys.argv[1])
text = f.read()
try:
count = int(sys.argv[2])
except:
count = 1000
print 'Starting to parse the file %i times...' % count
# parse file
t.start()
for i in range(count):
utext = upper(text)
result, taglist, nextindex = tag(utext,htmltable)
if not result:
print ' parsing failed; aborting'
break
t = t.stop()[0]
mean = t/count
print result, nextindex, mean*1000,'msec',nextindex/mean,'bytes/sec.'
print
print 'Hit return to see the tags...'
raw_input()
print
print_tags(text,taglist)
|