forked from w3c/csswg-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-html.py
executable file
·66 lines (57 loc) · 2.12 KB
/
make-html.py
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
#!/usr/bin/python
# HTMLMake
# Converts all files of specified extension from XHTML to HTML
# Written by fantasai
# Joint copyright 2010 W3C and Microsoft
# Licensed under BSD 3-Clause: <http://www.w3.org/Consortium/Legal/2008/03-bsd-license>
srcExt = '.xht'
dstExt = '.htm'
skipDirs = ('contributors/microsoft/submitted/support', # XXXfixme files should be .xht
'incoming', '.svn', 'CVS', '.hg')
import os.path
from os.path import join, getmtime
import sys
import re
import os
from w3ctestlib.Sources import XHTMLSource
def xhtml2html(source, dest):
"""Convert XHTML file given by path `source` into HTML file at path `dest`."""
# Parse and serialize
xs = XHTMLSource(source, dest)
o = xs.serializeHTML()
# Report errors
if xs.error:
print >>sys.stderr, "Error parsing XHTML file %s: %s" % (source, xs.error)
# Write
f = open(dest, 'w')
f.write(o.encode(xs.encoding, 'xmlcharrefreplace'))
f.close()
if len(sys.argv) == 3:
clobber = sys.argv[1] == '--clobber'
force = sys.argv[1] == '-f'
root = sys.argv[2]
elif len(sys.argv) == 2 and (sys.argv[1] != '--clobber' and sys.argv[1] != '-f'):
clobber = False;
force = False;
root = sys.argv[1]
else:
print "make-html converts all %s XHTML files to %s HTML files." % (srcExt, dstExt)
print "Only changed files are converted, unless you specify -f."
print "To use, specify the root directory of the files you want converted, e.g."
print " make-html ."
print "To delete all files with extension %s, specify the --clobber option." % dstExt
exit()
for root, dirs, files in os.walk(root):
for skip in skipDirs:
if skip in dirs:
dirs.remove(skip)
for file in files:
if clobber:
if file.endswith(dstExt):
os.remove(join(root, file))
elif file.endswith(srcExt):
source = join(root, file)
dest = join(root, file[0:-1*len(srcExt)] + dstExt)
if not os.path.exists(dest) or getmtime(source) > getmtime(dest) or force:
# print "Processing %s" % source
xhtml2html(source, dest)