-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxsltemplate.py
158 lines (117 loc) · 4.56 KB
/
xsltemplate.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
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
#python
#coding=utf-8
import sys
import os
import os.path
from urlparse import urlparse, urljoin
sys.path.insert(0, 'lib/lxml-2.2.4-py2.6-win32.egg')
from lxml import etree
class XSLTemplate():
def __init__(self, urlResolver = lambda url: url):
self.urlResolver = urlResolver
self.url = ''
def relativeURI(self, url1, url2):
''' url2相对于url1的相对路径字符串 '''
import re
# 去掉 ./ 这种表示当前目录的,学习一下 (?<=) 这种前导匹配
url1 = re.sub(r'(^\./|(?<=/)\./)', lambda m: '', url1)
url2 = re.sub(r'(^\./|(?<=/)\./)', lambda m: '', url2)
url1 = url1.split('/')
url2 = url2.split('/')
pos = 0
for i, part in enumerate(url1):
if url2[i] != part: break
else: pos = i
url2 = url2[i:]
url = len(url1[i+1:]) * '../' + '/'.join(url2)
return url
def translatePath(self, url):
url = urljoin(self.url, url)
return self.urlResolver(url)
def compile(self, url, pubdir):
''' 执行编译 '''
self.url = url
# 执行编译xhtml
xmlparser = etree.XMLParser()
xmlparser.resolvers.add(PathResolver(self.translatePath))
path = self.translatePath(url)
xml = etree.parse(path)
pi = xml.getroot().getprevious()
if not isinstance(pi, etree._XSLTProcessingInstruction):
return
print 'Recompiling XHTML'
xsltpath = pi.get('href')
xslt = etree.parse(xsltpath, xmlparser)
try:
transform = etree.XSLT(xslt)
resultTree = transform(xml,\
template_mode = etree.XSLT.strparam('publish'),\
template_uri_prefix = etree.XSLT.strparam(self.relativeURI(urljoin(url, '.'), pubdir)),\
template_lint_uri = etree.XSLT.strparam('true')\
)
result = etree.tostring(resultTree, method='html', encoding='utf-8', pretty_print=True)
except:
print 'transform xhtml error.'
return
filename = url[len(urljoin(url, '.')):-5] + 'html'
path = self.translatePath(urljoin(pubdir, filename))
self.writeFile(path, result)
return resultTree
def writeFile(self, path, txt):
print 'generated:', path
cssfile = open(path, "w")
cssfile.write(txt)
cssfile.close()
def getUrls(url, urlResolver = lambda u: u):
tree = etree.parse(urlResolver(url))
xsltpath = os.path.abspath('d:/works/xn.static/n/template.xsl')
xmlparser = etree.XMLParser()
xmlparser.resolvers.add(PathResolver(urlResolver))
xslt = etree.parse(xsltpath, xmlparser)
try:
transform = etree.XSLT(xslt)
resultTree = transform(tree,\
template_mode = etree.XSLT.strparam('geturls'),\
template_lint_uri = etree.XSLT.strparam('true'),\
template_server_root = etree.XSLT.strparam('/n/')\
)
except:
print 'get urls error'
return []
result = resultTree.findall('//*')
arr = []
for item in result:
arr.append(urljoin(url, item.text))
return arr
def main():
from optparse import OptionParser
parser = OptionParser('usage: python %prog command [options] args')
parser.add_option('-p', '--pppp',
dest="var",
type='string',
default=False,
help="write afsdafsdaf")
opts, args = parser.parse_args()
if len(args) <= 1:
parser.print_help()
return
command = args[0]
url = args[1]
class PathResolver(etree.Resolver):
def __init__(self, urlResolver):
self.urlResolver = urlResolver
def resolve(self, url, id, context):
import re
if re.search(r'^file\:///[a-zA-Z]\:/', url): # file:///d:/works/xn.static/n/apps...
path = re.sub(r'^file\:///', '', url)
elif re.search(r'^file\:///', url): # file:///n/apps...
url = re.sub(r'^file\://', '', url)
path = self.urlResolver(url)
else:
path = self.urlResolver(url)
try:
return self.resolve_file(open(path), context)
except:
pass
if __name__ == '__main__':
main()