Skip to content

Commit

Permalink
Add whitespace even for inline tags
Browse files Browse the repository at this point in the history
Thanks @codinguncut for suggestion. Still needs testing.
re.sub is replicating xpath's normalize-space behaviour.
See GH-1
  • Loading branch information
lopuhin committed May 26, 2017
1 parent c5cc532 commit 6135ba6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 8 additions & 1 deletion html_text/html_text.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import re

import lxml
import lxml.etree
from lxml.html.clean import Cleaner
Expand Down Expand Up @@ -41,8 +43,13 @@ def parse_html(html):

def selector_to_text(sel):
""" Convert a cleaned selector to text.
Almost the same as xpath normalize-space, but this also
adds spaces between inline elements (like <span>) which are
often used as block elements in html markup.
"""
return sel.xpath('normalize-space()').extract_first('')
fragments = (re.sub('\s+', ' ', x.strip())
for x in sel.xpath('//text()').extract())
return ' '.join(x for x in fragments if x)


def cleaned_selector(html):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_html_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ def test_extract_text_from_tree():
html = u'<html><style>.div {}</style><body><p>Hello, world!</body></html>'
tree = parse_html(html)
assert extract_text(tree) == u'Hello, world!'


def test_inline_tags_whitespace():
html = u'<span>field</span><span>value</span>'
assert extract_text(html) == u'field value'

0 comments on commit 6135ba6

Please sign in to comment.