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
85e9b79a
Commit
85e9b79a
authored
May 17, 2018
by
eckhart
Browse files
- syntaxtree.Node.as_xml: support '_inline' pseudo attribute
parent
8b148543
Changes
2
Hide whitespace changes
Inline
Side-by-side
DHParser/syntaxtree.py
View file @
85e9b79a
...
...
@@ -496,12 +496,15 @@ class Node(collections.abc.Sized):
tail
=
tail
.
lstrip
(
None
if
density
&
2
else
''
)
sep
,
inner_tab
=
(
''
,
''
)
if
hasattr
(
self
,
'_xml_attr'
)
and
'_inline'
in
self
.
attributes
\
else
(
'
\n
'
,
tab
)
if
self
.
children
:
content
=
[]
for
child
in
self
.
children
:
subtree
=
child
.
_tree_repr
(
tab
,
open_fn
,
close_fn
,
data_fn
,
density
).
split
(
'
\n
'
)
content
.
append
(
'
\n
'
.
join
((
tab
+
s
)
for
s
in
subtree
))
return
head
+
'
\n
'
.
join
(
content
)
+
tail
content
.
append
(
(
sep
+
inner_tab
).
join
(
s
for
s
in
subtree
))
return
head
+
tab
+
(
sep
+
inner_tab
)
.
join
(
content
)
+
tail
res
=
cast
(
str
,
self
.
result
)
# safe, because if there are no children, result is a string
if
density
&
1
and
res
.
find
(
'
\n
'
)
<
0
:
# and head[0] == "<":
...
...
@@ -534,7 +537,8 @@ class Node(collections.abc.Sized):
txt
=
[
left_bracket
,
node
.
tag_name
]
# s += " '(pos %i)" % node.add_pos
if
hasattr
(
node
,
'_xml_attr'
):
txt
.
extend
(
' `(%s "%s")'
%
(
k
,
v
)
for
k
,
v
in
node
.
attributes
.
items
())
txt
.
extend
(
' `(%s "%s")'
%
(
k
,
v
)
for
k
,
v
in
node
.
attributes
.
items
()
if
k
!=
'_inline'
)
if
src
:
txt
.
append
(
" `(pos %i %i %i)"
%
(
node
.
pos
,
*
line_col
(
lbreaks
,
node
.
pos
)))
# if node.error_flag: # just for debugging error collecting
...
...
@@ -566,30 +570,24 @@ class Node(collections.abc.Sized):
column.
"""
inline
=
False
def
opening
(
node
)
->
str
:
"""Returns the opening string for the representation of `node`."""
txt
=
[
'<'
,
node
.
tag_name
]
has_reserved_attrs
=
hasattr
(
node
,
'_xml_attr'
)
\
and
any
(
r
in
node
.
attributes
for
r
in
{
'err'
,
'line'
,
'col'
})
nonlocal
inline
inline
=
False
if
hasattr
(
node
,
'_xml_attr'
):
if
'_inline'
in
node
.
attributes
:
inline
=
True
del
node
.
attributes
[
'_inline'
]
txt
.
extend
(
' %s="%s"'
%
(
k
,
v
)
for
k
,
v
in
node
.
attributes
.
items
())
txt
.
extend
(
' %s="%s"'
%
(
k
,
v
)
for
k
,
v
in
node
.
attributes
.
items
()
if
k
!=
'_inline'
)
if
src
and
not
has_reserved_attrs
:
txt
.
append
(
' line="%i" col="%i"'
%
line_col
(
line_breaks
,
node
.
pos
))
if
showerrors
and
node
.
errors
and
not
has_reserved_attrs
:
txt
.
append
(
' err="%s"'
%
''
.
join
(
str
(
err
).
replace
(
'"'
,
r
'\"'
)
for
err
in
node
.
errors
))
return
""
.
join
(
txt
+
[
">"
if
inline
else
">
\n
"
])
return
""
.
join
(
txt
+
[
">
\n
"
])
def
closing
(
node
):
"""Returns the closing string for the representation of `node`."""
return
(
'</'
if
inline
else
'
\n
</'
)
+
node
.
tag_name
+
'>'
return
(
'
\n
</'
)
+
node
.
tag_name
+
'>'
line_breaks
=
linebreaks
(
src
)
if
src
else
[]
return
self
.
_tree_repr
(
' '
*
indentation
,
opening
,
closing
,
density
=
1
)
...
...
test/test_syntaxtree.py
View file @
85e9b79a
...
...
@@ -219,7 +219,6 @@ class TestNodeFind():
def
test_getitem
(
self
):
tree
=
parse_sxpr
(
'(a (b X) (X (c d)) (e (X F)))'
)
# print(tree.as_sxpr())
assert
tree
[
0
]
==
parse_sxpr
(
'(b X)'
)
assert
tree
[
2
]
==
parse_sxpr
(
'(e (X F))'
)
try
:
...
...
@@ -256,6 +255,12 @@ class TestSerialization:
tree3
=
parse_sxpr
(
'(A `(attr "value") `(attr2 "value2") "B")'
)
assert
tree
.
as_sxpr
()
==
tree3
.
as_sxpr
()
def
test_xml_inlining
(
self
):
tree
=
parse_sxpr
(
'(A (B "C") (D "E"))'
)
tree
.
attributes
[
'_inline'
]
=
"1"
xml
=
tree
.
as_xml
()
assert
xml
==
"<A>
\n
<B>C</B><D>E</D>
\n
</A>"
if
__name__
==
"__main__"
:
...
...
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