From 891a4a8d085cd2a1aac3ad05455721feaa69d67c Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Wed, 31 Jul 2013 22:17:22 -0400 Subject: [PATCH] Add "heading_style" option Allow the user to specify a heading style. --- markdownify/__init__.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 32c397c..43383b7 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -9,6 +9,13 @@ FRAGMENT_ID = '__MARKDOWNIFY_WRAPPER__' wrapped = '
%%s
' % FRAGMENT_ID +# Heading styles +ATX = 'atx' +ATX_CLOSED = 'atx_closed' +UNDERLINED = 'underlined' +SETEXT = UNDERLINED + + def escape(text): if not text: return '' @@ -24,6 +31,7 @@ class MarkdownConverter(object): strip = None convert = None autolinks = True + heading_style = UNDERLINED class Options(DefaultOptions): pass @@ -67,7 +75,7 @@ class MarkdownConverter(object): return escape(whitespace_re.sub(' ', text or '')) def __getattr__(self, attr): - # Handle heading levels > 2 + # Handle headings m = convert_heading_re.match(attr) if m: n = int(m.group(1)) @@ -117,14 +125,16 @@ class MarkdownConverter(object): def convert_em(self, el, text): return '*%s*' % text if text else '' - def convert_h1(self, el, text): - return self.underline(text, '=') - - def convert_h2(self, el, text): - return self.underline(text, '-') - def convert_hn(self, n, el, text): - return '%s %s\n\n' % ('#' * n, text.rstrip()) if text else '' + style = self.options['heading_style'] + text = text.rstrip() + if style == UNDERLINED and n <= 2: + line = '=' if n == 1 else '-' + return self.underline(text, line) + hashes = '#' * n + if style == ATX_CLOSED: + return '%s %s %s\n\n' % (hashes, text, hashes) + return '%s %s\n\n' % (hashes, text) def convert_i(self, el, text): return self.convert_em(el, text)