@@ -46,11 +46,16 @@ def parse():
4646 else :
4747 p = html5parser .HTMLParser (tree = treebuilder )
4848
49+ if opts .fragment :
50+ parseMethod = p .parseFragment
51+ else :
52+ parseMethod = p .parse
53+
4954 if opts .profile :
5055 import hotshot
5156 import hotshot .stats
5257 prof = hotshot .Profile ('stats.prof' )
53- prof .runcall (p . parse , f )
58+ prof .runcall (parseMethod , f )
5459 prof .close ()
5560 # XXX - We should use a temp file here
5661 stats = hotshot .stats .load ('stats.prof' )
@@ -60,13 +65,13 @@ def parse():
6065 elif opts .time :
6166 import time
6267 t0 = time .time ()
63- document = p . parse (f )
68+ document = parseMethod (f )
6469 t1 = time .time ()
6570 printOutput (p , document , opts )
6671 t2 = time .time ()
6772 sys .stdout .write ("\n \n Run took: %fs (plus %fs to print the output)" % (t1 - t0 , t2 - t1 ))
6873 else :
69- document = p . parse (f )
74+ document = parseMethod (f )
7075 printOutput (p , document , opts )
7176
7277def printOutput (parser , document , opts ):
@@ -82,12 +87,15 @@ def printOutput(parser, document, opts):
8287 elif opts .hilite :
8388 sys .stdout .write (document .hilite ("utf-8" ))
8489 else :
85- sys .stdout .write (parser .tree .testSerializer (document ).encode ("utf-8" ))
90+ print document
91+ if not hasattr (document ,'__iter__' ): document = [document ]
92+ for fragment in document :
93+ print parser .tree .testSerializer (fragment ).encode ("utf-8" )
8694 if opts .error :
8795 errList = []
8896 for pos , message in parser .errors :
8997 errList .append ("Line %i Col %i" % pos + " " + message )
90- sys .stderr .write ("\n Parse errors:\n " + "\n " .join (errList )+ "\n " )
98+ sys .stdout .write ("\n Parse errors:\n " + "\n " .join (errList )+ "\n " )
9199
92100def getOptParser ():
93101 parser = OptionParser (usage = __doc__ )
@@ -109,6 +117,9 @@ def getOptParser():
109117 parser .add_option ("-e" , "--error" , action = "store_true" , default = False ,
110118 dest = "error" , help = "Print a list of parse errors" )
111119
120+ parser .add_option ("-f" , "--fragment" , action = "store_true" , default = False ,
121+ dest = "fragment" , help = "Parse as a fragment" )
122+
112123 parser .add_option ("-x" , "--xml" , action = "store_true" , default = False ,
113124 dest = "xml" , help = "Output as xml" )
114125
0 commit comments