@@ -46,11 +46,16 @@ def parse():
46
46
else :
47
47
p = html5parser .HTMLParser (tree = treebuilder )
48
48
49
+ if opts .fragment :
50
+ parseMethod = p .parseFragment
51
+ else :
52
+ parseMethod = p .parse
53
+
49
54
if opts .profile :
50
55
import hotshot
51
56
import hotshot .stats
52
57
prof = hotshot .Profile ('stats.prof' )
53
- prof .runcall (p . parse , f )
58
+ prof .runcall (parseMethod , f )
54
59
prof .close ()
55
60
# XXX - We should use a temp file here
56
61
stats = hotshot .stats .load ('stats.prof' )
@@ -60,13 +65,13 @@ def parse():
60
65
elif opts .time :
61
66
import time
62
67
t0 = time .time ()
63
- document = p . parse (f )
68
+ document = parseMethod (f )
64
69
t1 = time .time ()
65
70
printOutput (p , document , opts )
66
71
t2 = time .time ()
67
72
sys .stdout .write ("\n \n Run took: %fs (plus %fs to print the output)" % (t1 - t0 , t2 - t1 ))
68
73
else :
69
- document = p . parse (f )
74
+ document = parseMethod (f )
70
75
printOutput (p , document , opts )
71
76
72
77
def printOutput (parser , document , opts ):
@@ -82,12 +87,15 @@ def printOutput(parser, document, opts):
82
87
elif opts .hilite :
83
88
sys .stdout .write (document .hilite ("utf-8" ))
84
89
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" )
86
94
if opts .error :
87
95
errList = []
88
96
for pos , message in parser .errors :
89
97
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 " )
91
99
92
100
def getOptParser ():
93
101
parser = OptionParser (usage = __doc__ )
@@ -109,6 +117,9 @@ def getOptParser():
109
117
parser .add_option ("-e" , "--error" , action = "store_true" , default = False ,
110
118
dest = "error" , help = "Print a list of parse errors" )
111
119
120
+ parser .add_option ("-f" , "--fragment" , action = "store_true" , default = False ,
121
+ dest = "fragment" , help = "Parse as a fragment" )
122
+
112
123
parser .add_option ("-x" , "--xml" , action = "store_true" , default = False ,
113
124
dest = "xml" , help = "Output as xml" )
114
125
0 commit comments