Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
108 lines (74 loc) · 2.98 KB
/
Copy pathutils.py
File metadata and controls
108 lines (74 loc) · 2.98 KB
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
'''
Copyright (c) 2015, 2017, 2019 Tim Savannah under terms of LGPLv3. All Rights Reserved.
See LICENSE (https://gnu.org/licenses/lgpl-3.0.txt) for more information.
Some misc utils and regular expressions
'''
import sys
import re
__all__ = ('IE_CONDITIONAL_PATTERN', 'END_HTML', 'START_HTML', 'DOCTYPE_MATCH',
'stripIEConditionals', 'addStartTag', 'escapeQuotes', 'unescapeQuotes', 'tostr', 'isstr',
'stripWordsOnly',
)
IE_CONDITIONAL_PATTERN = re.compile('[<][!][-][-][ \t\r\n]*[\[][ \t\r\n]*if.*-->', re.MULTILINE)
END_HTML = re.compile('.*</[ \t\r\n]*[hH][tT][mM][lL][ \t\r\n]*>.*', re.DOTALL)
START_HTML = re.compile('.*<[ \t\r\n]*[hH][tT][mM][lL][ \t\r\n]*>.*', re.DOTALL)
DOCTYPE_MATCH = re.compile('[\n]*[ \t]*(?P<tag><[!][ \t]*[dD][oO][cC][tT][yY][pP][eE].*[>])')
WORDS_ONLY_RE = re.compile('[ ][ ]+')
def stripWordsOnly(contents):
return WORDS_ONLY_RE.sub(' ', contents.strip())
def stripIEConditionals(contents, addHtmlIfMissing=True):
'''
stripIEConditionals - Strips Internet Explorer conditional statements.
@param contents <str> - Contents String
@param addHtmlIfMissing <bool> - Since these normally encompass the "html" element, optionally add it back if missing.
'''
allMatches = IE_CONDITIONAL_PATTERN.findall(contents)
if not allMatches:
return contents
for match in allMatches:
contents = contents.replace(match, '')
if END_HTML.match(contents) and not START_HTML.match(contents):
contents = addStartTag(contents, '<html>')
return contents
def addStartTag(contents, startTag):
'''
addStartTag - Safetly add a start tag to the document, taking into account the DOCTYPE
@param contents <str> - Contents
@param startTag <str> - Fully formed tag, i.e. <html>
'''
matchObj = DOCTYPE_MATCH.match(contents)
if matchObj:
idx = matchObj.end()
else:
idx = 0
return "%s\n%s\n%s" %(contents[:idx], startTag, contents[idx:])
def escapeQuotes(value):
'''
escapeQuotes - Escape quotes within a value (replaces " with ")
@param value <str>
@return <str> - Escaped value
'''
return value.replace('"', '"')
def unescapeQuotes(value):
'''
unescapeQuotes - Unescapes quotes within a value (replaces " with ")
@param value <str>
@return <str> - Escaped value
'''
return value.replace('"', '"')
# TODO: Use the types in compat.py
# TODO: Evaluate all uses and determine if should be done like this,
# or properly encoded/decoded using the encoding specified in the parser
if sys.version_info.major < 3:
def tostr(value):
if not isinstance(value, (str, unicode)):
value = unicode(value)
return value
def isstr(value):
return isinstance(value, (str, unicode))
else:
def tostr(value):
return str(value)
def isstr(value):
return isinstance(value, str)
# vim: set ts=4 sw=4 st=4 expandtab :