Skip to content

Commit

Permalink
Added postprocess and preprocess functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Divyesh06 committed Jan 10, 2025
1 parent 1223b58 commit 7ff6da8
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions markdownify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class DefaultOptions:
strip = None
strong_em_symbol = ASTERISK
sub_symbol = ''
postprocess_fn = None
preprocess_fn = None
sup_symbol = ''
wrap = False
wrap_width = 80
Expand All @@ -114,6 +116,8 @@ def __init__(self, **options):
self.options = _todict(self.DefaultOptions)
self.options.update(_todict(self.Options))
self.options.update(options)
self.postprocess_fn = self.options['postprocess_fn']
self.preprocess_fn = self.options['preprocess_fn']
if self.options['strip'] is not None and self.options['convert'] is not None:
raise ValueError('You may specify either tags to strip or tags to'
' convert, but not both.')
Expand Down Expand Up @@ -171,13 +175,16 @@ def process_tag(self, node, convert_as_inline, children_only=False):
text = text_strip + newlines + next_text_strip

if not children_only:
convert_all = getattr(self, 'convert_all', None)
if convert_all:
text = convert_all(node, text, convert_as_inline)
if self.preprocess_fn and self.should_convert_tag(node.name):
text = self.preprocess_fn(node, text, convert_as_inline)

convert_fn = getattr(self, 'convert_%s' % node.name, None)
if convert_fn and self.should_convert_tag(node.name):
text = convert_fn(node, text, convert_as_inline)

if self.postprocess_fn and self.should_convert_tag(node.name):
text = self.postprocess_fn(node, text, convert_as_inline)

return text

def process_text(self, el):
Expand Down

0 comments on commit 7ff6da8

Please sign in to comment.