Skip to content

Commit

Permalink
Merge pull request #84 from emillon/bodywidth-kwarg
Browse files Browse the repository at this point in the history
Make bodywidth kwarg overridable using config

Thanks @emillon
  • Loading branch information
Alir3z4 committed Jul 4, 2015
2 parents b8415f8 + 4a93656 commit e71752d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The AUTHORS/Contributors are (and/or have been):
* Arjoonn Sharma <gh: theSage21>
* Ali Mohammad <gh: alawibaba>
* Albert Berger <gh: nbdsp>
* Etienne Millon <[email protected]>


Maintainer:
Expand Down
1 change: 1 addition & 0 deletions ChangeLog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Fix #61: Functionality added for optional use of automatic links.
* Feature #80: ``title`` attribute is preserved in both inline and reference links.
* Feature #82: More command line options. See docs.
* Fix #84: Make bodywidth kwarg overridable using config.


2015.6.12
Expand Down
4 changes: 3 additions & 1 deletion html2text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,9 @@ def optwrap(self, text):
return result


def html2text(html, baseurl='', bodywidth=config.BODY_WIDTH):
def html2text(html, baseurl='', bodywidth=None):
if bodywidth is None:
bodywidth = config.BODY_WIDTH
h = HTML2Text(baseurl=baseurl, bodywidth=bodywidth)

return h.handle(html)
Expand Down
24 changes: 21 additions & 3 deletions test/test_html2text.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ def test_command(fn, *args):
return result, actual


def test_function(fn, **kwargs):
with open(fn) as inf:
actual = html2text.html2text(inf.read(), **kwargs)
result = get_baseline(fn)
return result, actual


def get_dump_name(fn, suffix):
return '%s-%s_output.md' % (os.path.splitext(fn)[0], suffix)

Expand Down Expand Up @@ -98,8 +105,13 @@ def test_cmd(self):
result, actual = test_command(fn, *cmdline_args)
self.assertEqual(result, actual)

def test_func(self):
result, actual = test_function(fn, **func_args)
self.assertEqual(result, actual)

module_args = {}
cmdline_args = []
func_args = {}
base_fn = os.path.basename(fn).lower()

if base_fn.startswith('google'):
Expand All @@ -126,6 +138,7 @@ def test_cmd(self):
# module_args['unicode_snob'] = True
module_args['body_width'] = 0
cmdline_args.append('--body-width=0')
func_args['bodywidth'] = 0

if base_fn.startswith('protect_links'):
module_args['protect_links'] = True
Expand All @@ -152,18 +165,23 @@ def test_cmd(self):
if base_fn.startswith('mark_code'):
module_args['mark_code'] = True
cmdline_args.append('--mark-code')

return test_mod, test_cmd

if base_fn not in ['bodywidth_newline.html', 'abbr_tag.html']:
test_func = None

return test_mod, test_cmd, test_func

# Originally from http://stackoverflow.com/questions/32899/\
# how-to-generate-dynamic-parametrized-unit-tests-in-python
test_dir_name = os.path.dirname(os.path.realpath(__file__))
for fn in glob.glob("%s/*.html" % test_dir_name):
test_name = 'test_%s' % os.path.splitext(os.path.basename(fn))[0].lower()
test_m, test_c = generate_test(fn)
test_m, test_c, test_func = generate_test(fn)
setattr(TestHTML2Text, test_name + "_mod", test_m)
if test_c:
setattr(TestHTML2Text, test_name + "_cmd", test_c)
if test_func:
setattr(TestHTML2Text, test_name + "_func", test_func)

if __name__ == "__main__":
unittest.main()

0 comments on commit e71752d

Please sign in to comment.