Skip to content

Commit 7fec8a2

Browse files
authored
code simplification to remove need for children_only parameter (#174)
Signed-off-by: chrispy <[email protected]>
1 parent 1b33330 commit 7fec8a2

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Diff for: markdownify/__init__.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ def convert(self, html):
124124
return self.convert_soup(soup)
125125

126126
def convert_soup(self, soup):
127-
return self.process_tag(soup, convert_as_inline=False, children_only=True)
127+
return self.process_tag(soup, convert_as_inline=False)
128128

129-
def process_tag(self, node, convert_as_inline, children_only=False):
129+
def process_tag(self, node, convert_as_inline):
130130
text = ''
131131

132132
# markdown headings or cells can't include
@@ -135,7 +135,7 @@ def process_tag(self, node, convert_as_inline, children_only=False):
135135
isCell = node.name in ['td', 'th']
136136
convert_children_as_inline = convert_as_inline
137137

138-
if not children_only and (isHeading or isCell):
138+
if isHeading or isCell:
139139
convert_children_as_inline = True
140140

141141
# Remove whitespace-only textnodes just before, after or
@@ -171,14 +171,18 @@ def process_tag(self, node, convert_as_inline, children_only=False):
171171
newlines = '\n' * max(newlines_left, newlines_right)
172172
text = text_strip + newlines + next_text_strip
173173

174-
if not children_only:
175-
fn_name = 'convert_%s' % node.name.translate(''.maketrans(':-', '__'))
176-
convert_fn = getattr(self, fn_name, None)
177-
if convert_fn and self.should_convert_tag(node.name):
178-
text = convert_fn(node, text, convert_as_inline)
174+
# apply this tag's final conversion function
175+
convert_fn_name = "convert_%s" % re.sub(r"[\[\]:-]", "_", node.name)
176+
convert_fn = getattr(self, convert_fn_name, None)
177+
if convert_fn and self.should_convert_tag(node.name):
178+
text = convert_fn(node, text, convert_as_inline)
179179

180180
return text
181181

182+
def convert__document_(self, el, text, convert_as_inline):
183+
# for BeautifulSoup objects (where node.name == "[document]"), return content results as-is
184+
return text
185+
182186
def process_text(self, el):
183187
text = six.text_type(el) or ''
184188

0 commit comments

Comments
 (0)