allow BeautifulSoup configuration kwargs to be specified (#224)

Signed-off-by: chrispy <chrispy@synopsys.com>
This commit is contained in:
Chris Papademetrious
2025-06-14 09:06:22 -04:00
committed by GitHub
parent 016251e915
commit 75ab3064dd
4 changed files with 25 additions and 12 deletions

View File

@@ -154,7 +154,7 @@ def _next_block_content_sibling(el):
class MarkdownConverter(object):
class DefaultOptions:
autolinks = True
beautiful_soup_parser = 'html.parser'
bs4_options = 'html.parser'
bullets = '*+-' # An iterable of bullet types.
code_language = ''
code_language_callback = None
@@ -188,11 +188,15 @@ class MarkdownConverter(object):
raise ValueError('You may specify either tags to strip or tags to'
' convert, but not both.')
# If a string or list is passed to bs4_options, assume it is a 'features' specification
if not isinstance(self.options['bs4_options'], dict):
self.options['bs4_options'] = {'features': self.options['bs4_options']}
# Initialize the conversion function cache
self.convert_fn_cache = {}
def convert(self, html):
soup = BeautifulSoup(html, self.options['beautiful_soup_parser'])
soup = BeautifulSoup(html, **self.options['bs4_options'])
return self.convert_soup(soup)
def convert_soup(self, soup):

9
markdownify/main.py Normal file → Executable file
View File

@@ -70,12 +70,11 @@ def main(argv=sys.argv[1:]):
parser.add_argument('-w', '--wrap', action='store_true',
help="Wrap all text paragraphs at --wrap-width characters.")
parser.add_argument('--wrap-width', type=int, default=80)
parser.add_argument('-p', '--beautiful-soup-parser',
dest='beautiful_soup_parser',
parser.add_argument('--bs4-options',
default='html.parser',
help="Specify the Beautiful Soup parser to be used for interpreting HTML markup. Parsers such "
"as html5lib, lxml or even a custom parser as long as it is installed on the execution "
"environment.")
help="Specifies the parser that BeautifulSoup should use to parse "
"the HTML markup. Examples include 'html5.parser', 'lxml', and "
"'html5lib'.")
args = parser.parse_args(argv)
print(markdownify(**vars(args)))