MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / Xml

Class Xml

src/__init__.py:2120–2712  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2118
2119
2120class Xml:
2121
2122 def __enter__(self):
2123 return self
2124
2125 def __exit__(self, *args):
2126 pass
2127
2128 def __init__(self, rhs):
2129 if isinstance(rhs, mupdf.FzXml):
2130 self.this = rhs
2131 elif isinstance(rhs, str):
2132 buff = mupdf.fz_new_buffer_from_copied_data(rhs)
2133 self.this = mupdf.fz_parse_xml_from_html5(buff)
2134 else:
2135 assert 0, f'Unsupported type for rhs: {type(rhs)}'
2136
2137 def _get_node_tree( self):
2138 def show_node(node, items, shift):
2139 while node is not None:
2140 if node.is_text:
2141 items.append((shift, f'"{node.text}"'))
2142 node = node.next
2143 continue
2144 items.append((shift, f"({node.tagname}"))
2145 for k, v in node.get_attributes().items():
2146 items.append((shift, f"={k} '{v}'"))
2147 child = node.first_child
2148 if child:
2149 items = show_node(child, items, shift + 1)
2150 items.append((shift, f"){node.tagname}"))
2151 node = node.next
2152 return items
2153
2154 shift = 0
2155 items = []
2156 items = show_node(self, items, shift)
2157 return items
2158
2159 def add_bullet_list(self):
2160 """Add bulleted list ("ul" tag)"""
2161 child = self.create_element("ul")
2162 self.append_child(child)
2163 return child
2164
2165 def add_class(self, text):
2166 """Set some class via CSS. Replaces complete class spec."""
2167 cls = self.get_attribute_value("class")
2168 if cls is not None and text in cls:
2169 return self
2170 self.remove_attribute("class")
2171 if cls is None:
2172 cls = text
2173 else:
2174 cls += " " + text
2175 self.set_attribute("class", cls)
2176 return self
2177

Callers 12

bodytagMethod · 0.85
cloneMethod · 0.85
create_elementMethod · 0.85
create_text_nodeMethod · 0.85
findMethod · 0.85
find_nextMethod · 0.85
first_childMethod · 0.85
nextMethod · 0.85
parentMethod · 0.85
previousMethod · 0.85
rootMethod · 0.85
documentMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected