From 89b577e91efa61df0cc4ca9a7672d7d24d27f1f7 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Fri, 21 May 2021 12:21:21 +0200 Subject: [PATCH] ordering functions alphabetically --- markdownify/__init__.py | 56 ++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 4e442bd..7271c69 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -198,6 +198,12 @@ class MarkdownConverter(object): else: return ' \n' + def convert_code(self, el, text, convert_as_inline): + prefix, suffix, text = chomp(text) + if not text: + return '' + return '%s`%s`%s' % (prefix, text, suffix) + def convert_em(self, el, text, convert_as_inline): em_tag = self.options['strong_em_symbol'] prefix, suffix, text = chomp(text) @@ -205,17 +211,7 @@ class MarkdownConverter(object): return '' return '%s%s%s%s%s' % (prefix, em_tag, text, em_tag, suffix) - def convert_code(self, el, text, convert_as_inline): - prefix, suffix, text = chomp(text) - if not text: - return '' - return '%s`%s`%s' % (prefix, text, suffix) - - def convert_samp(self, el, text, convert_as_inline): - return self.convert_code(el, text, convert_as_inline) - - def convert_kbd(self, el, text, convert_as_inline): - return self.convert_code(el, text, convert_as_inline) + convert_kbd = convert_code def convert_hn(self, n, el, text, convert_as_inline): if convert_as_inline: @@ -231,9 +227,22 @@ class MarkdownConverter(object): return '%s %s %s\n\n' % (hashes, text, hashes) return '%s %s\n\n' % (hashes, text) + def convert_hr(self, el, text, convert_as_inline): + return '\n\n---\n\n' + def convert_i(self, el, text, convert_as_inline): return self.convert_em(el, text, convert_as_inline) + def convert_img(self, el, text, convert_as_inline): + alt = el.attrs.get('alt', None) or '' + src = el.attrs.get('src', None) or '' + title = el.attrs.get('title', None) or '' + title_part = ' "%s"' % title.replace('"', r'\"') if title else '' + if convert_as_inline: + return alt + + return '![%s](%s%s)' % (alt, src, title_part) + def convert_list(self, el, text, convert_as_inline): # Converting a list to inline is undefined. @@ -286,19 +295,17 @@ class MarkdownConverter(object): return '' return '%s%s%s%s%s' % (prefix, strong_tag, text, strong_tag, suffix) - def convert_img(self, el, text, convert_as_inline): - alt = el.attrs.get('alt', None) or '' - src = el.attrs.get('src', None) or '' - title = el.attrs.get('title', None) or '' - title_part = ' "%s"' % title.replace('"', r'\"') if title else '' - if convert_as_inline: - return alt - - return '![%s](%s%s)' % (alt, src, title_part) + convert_samp = convert_code def convert_table(self, el, text, convert_as_inline): return '\n\n' + text + '\n' + def convert_td(self, el, text, convert_as_inline): + return ' ' + text + ' |' + + def convert_th(self, el, text, convert_as_inline): + return ' ' + text + ' |' + def convert_tr(self, el, text, convert_as_inline): cells = el.find_all(['td', 'th']) is_headrow = all([cell.name == 'th' for cell in cells]) @@ -314,15 +321,6 @@ class MarkdownConverter(object): overline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n' return overline + '|' + text + '\n' + underline - def convert_th(self, el, text, convert_as_inline): - return ' ' + text + ' |' - - def convert_td(self, el, text, convert_as_inline): - return ' ' + text + ' |' - - def convert_hr(self, el, text, convert_as_inline): - return '\n\n---\n\n' - def markdownify(html, **options): return MarkdownConverter(**options).convert(html)