Moved unwrap to its own standalone package.
This commit is contained in:
parent
0fe5618d22
commit
3db0d7b1e6
|
@ -12,6 +12,9 @@ classifier =
|
|||
Development Status :: 4 - Beta
|
||||
Programming Language :: Python
|
||||
|
||||
install_requires =
|
||||
textunwrap
|
||||
|
||||
[entry_points]
|
||||
# Add here console scripts like:
|
||||
# console_scripts =
|
||||
|
|
|
@ -18,6 +18,7 @@ import sys
|
|||
import re
|
||||
|
||||
|
||||
from textunwrap import unwrap
|
||||
from licensename import __version__
|
||||
from licensename.known_licenses import KNOWN_FIRST_LINES
|
||||
|
||||
|
@ -38,36 +39,6 @@ BULLET_ITEM = r'(?:{spaces}*{bullet_marker}{spaces}+)'.format(
|
|||
spaces=SPACES)
|
||||
|
||||
|
||||
def unwrap(text):
|
||||
in_bullet_list = False
|
||||
def from_same_paragraph(line_a, line_b, median_len=70):
|
||||
nonlocal in_bullet_list
|
||||
if re.match(BULLET_ITEM, line_b):
|
||||
in_bullet_list = True
|
||||
return False
|
||||
if line_b.startswith(' ') and in_bullet_list:
|
||||
return True
|
||||
if not line_b and in_bullet_list:
|
||||
in_bullet_list = False
|
||||
if not line_a.strip():
|
||||
return False
|
||||
if ((median_len * .72 < len(line_a) < median_len * 2.5 and
|
||||
3 < len(line_b) < median_len * 2.5)):
|
||||
return True
|
||||
return False
|
||||
|
||||
lines = text.split('\n')
|
||||
paragraphs = [[]]
|
||||
previous_line = ''
|
||||
for line in lines:
|
||||
if from_same_paragraph(previous_line, line):
|
||||
paragraphs[-1].append(line)
|
||||
elif line:
|
||||
paragraphs.append([line])
|
||||
previous_line = line
|
||||
return '\n\n'.join(' '.join(lines) for lines in paragraphs if lines)
|
||||
|
||||
|
||||
def line_match_pattern(line, patterns):
|
||||
if isinstance(patterns, str):
|
||||
return patterns
|
||||
|
|
|
@ -26,9 +26,3 @@ FIXTURE_DIR = os.path.join(
|
|||
glob.glob(os.path.join(FIXTURE_DIR, '*.txt'))])
|
||||
def test_files(license_file, license_name):
|
||||
assert from_file(license_file) == license_name
|
||||
|
||||
|
||||
def test_unwrap():
|
||||
assert unwrap("") == ""
|
||||
assert unwrap("foo") == "foo"
|
||||
assert unwrap("foo\nbar") == "foo\n\nbar"
|
||||
|
|
Loading…
Reference in New Issue
Block a user