FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Tools/scripts/parseentities.py at 3.8 · python/cpython · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
python
/
cpython
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
35.3k
Star
74.9k
Code
Issues
5k+
Pull requests
2.5k
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Tools
/
scripts
/
parseentities.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
64 lines (52 loc) · 1.7 KB
Breadcrumbs
cpython
/
Tools
/
scripts
/
parseentities.py
Copy path
File metadata and controls
executable file
·
64 lines (52 loc) · 1.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python3
""" Utility for parsing HTML entity definitions available from:
http://www.w3.org/ as e.g.
http://www.w3.org/TR/REC-html40/HTMLlat1.ent
Input is read from stdin, output is written to stdout in form of a
Python snippet defining a dictionary "entitydefs" mapping literal
entity name to character or numeric entity.
Marc-Andre Lemburg, mal@lemburg.com, 1999.
Use as you like. NO WARRANTIES.
"""
import
re
,
sys
entityRE
=
re
.
compile
(
r'<!ENTITY +(\w+) +CDATA +"([^"]+)" +-- +((?:.|\n)+?) *-->'
)
def
parse
(
text
,
pos
=
0
,
endpos
=
None
):
pos
=
0
if
endpos
is
None
:
endpos
=
len
(
text
)
d
=
{}
while
1
:
m
=
entityRE
.
search
(
text
,
pos
,
endpos
)
if
not
m
:
break
name
,
charcode
,
comment
=
m
.
groups
()
d
[
name
]
=
charcode
,
comment
pos
=
m
.
end
()
return
d
def
writefile
(
f
,
defs
):
f
.
write
(
"entitydefs = {
\n
"
)
items
=
sorted
(
defs
.
items
())
for
name
, (
charcode
,
comment
)
in
items
:
if
charcode
[:
2
]
==
'&#'
:
code
=
int
(
charcode
[
2
:
-
1
])
if
code
<
256
:
charcode
=
r"'\%o'"
%
code
else
:
charcode
=
repr
(
charcode
)
else
:
charcode
=
repr
(
charcode
)
comment
=
' '
.
join
(
comment
.
split
())
f
.
write
(
" '%s':
\t
%s,
\t
# %s
\n
"
%
(
name
,
charcode
,
comment
))
f
.
write
(
'
\n
}
\n
'
)
if
__name__
==
'__main__'
:
if
len
(
sys
.
argv
)
>
1
:
with
open
(
sys
.
argv
[
1
])
as
infile
:
text
=
infile
.
read
()
else
:
text
=
sys
.
stdin
.
read
()
defs
=
parse
(
text
)
if
len
(
sys
.
argv
)
>
2
:
with
open
(
sys
.
argv
[
2
],
'w'
)
as
outfile
:
writefile
(
outfile
,
defs
)
else
:
writefile
(
sys
.
stdout
,
defs
)
Back
|
FazBrowse Home
|
New Git URL