Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
9.2.2023: Due to updates GitLab will be unavailable for some minutes between 9:00 and 11:00.
Open sidebar
badw-it
DHParser
Commits
7db02298
Commit
7db02298
authored
Jun 22, 2018
by
eckhart
Browse files
- more natural xml-representation
parent
e7dda4f5
Changes
3
Hide whitespace changes
Inline
Side-by-side
DHParser/syntaxtree.py
View file @
7db02298
...
...
@@ -530,15 +530,18 @@ class Node(collections.abc.Sized):
for
child
in
self
.
children
:
subtree
=
child
.
_tree_repr
(
tab
,
open_fn
,
close_fn
,
data_fn
,
density
,
inline
,
inline_fn
)
subtree
=
[
subtree
]
if
inline
else
subtree
.
split
(
'
\n
'
)
content
.
append
((
sep
+
usetab
).
join
(
s
for
s
in
subtree
))
if
subtree
:
subtree
=
[
subtree
]
if
inline
else
subtree
.
split
(
'
\n
'
)
content
.
append
((
sep
+
usetab
).
join
(
s
for
s
in
subtree
))
return
head
+
usetab
+
(
sep
+
usetab
).
join
(
content
)
+
tail
res
=
cast
(
str
,
self
.
result
)
# safe, because if there are no children, result is a string
if
not
inline
and
not
head
:
res
=
res
.
strip
()
if
density
&
1
and
res
.
find
(
'
\n
'
)
<
0
:
# and head[0] == "<":
# except for XML, add a gap between opening statement and content
gap
=
' '
if
not
inline
and
head
and
head
.
rstrip
()[
-
1
:]
!=
'>'
else
''
return
head
.
rstrip
()
+
gap
+
data_fn
(
self
.
result
)
+
tail
.
lstrip
()
return
head
.
rstrip
()
+
gap
+
data_fn
(
res
)
+
tail
.
lstrip
()
else
:
return
head
+
'
\n
'
.
join
([
usetab
+
data_fn
(
s
)
for
s
in
res
.
split
(
'
\n
'
)])
+
tail
...
...
examples/XML/XMLCompiler.py
View file @
7db02298
...
...
@@ -682,7 +682,7 @@ class XMLCompiler(Compiler):
def
on_document
(
self
,
node
):
self
.
tree
.
omit_tags
.
add
(
'CharData'
)
self
.
tree
.
inline_tags
.
update
({
'to'
,
'from'
,
'heading'
,
'body'
,
'remark'
})
return
self
.
fallback_compiler
(
node
)
def
extract_attributes
(
self
,
node_sequence
):
...
...
test/test_syntaxtree.py
View file @
7db02298
...
...
@@ -255,6 +255,20 @@ class TestSerialization:
tree3
=
parse_sxpr
(
'(A `(attr "value") `(attr2 "value2") "B")'
)
assert
tree
.
as_sxpr
()
==
tree3
.
as_sxpr
()
def
test_sexpr
(
self
):
tree
=
parse_sxpr
(
'(A (B "C") (D "E"))'
)
s
=
tree
.
as_sxpr
()
assert
s
==
'(A
\n
(B
\n
"C"
\n
)
\n
(D
\n
"E"
\n
)
\n
)'
,
s
tree
=
parse_sxpr
(
'(A (B (C "D") (E "F")) (G "H"))'
)
s
=
tree
.
as_sxpr
()
assert
s
==
'(A
\n
(B
\n
(C
\n
"D"
\n
)
\n
(E
\n
"F"
\n
)'
\
'
\n
)
\n
(G
\n
"H"
\n
)
\n
)'
,
s
tree
=
parse_sxpr
(
'(A (B (C "D
\n
X") (E "F")) (G " H
\n
Y "))'
)
s
=
tree
.
as_sxpr
()
assert
s
==
'(A
\n
(B
\n
(C
\n
"D"
\n
"X"
\n
)'
\
'
\n
(E
\n
"F"
\n
)
\n
)
\n
(G
\n
" H "
\n
" Y "
\n
)
\n
)'
,
s
def
test_xml_inlining
(
self
):
tree
=
parse_sxpr
(
'(A (B "C") (D "E"))'
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment