diff options
author | Daniel Neuhäuser <ich@danielneuhaeuser.de> | 2010-08-16 08:18:10 +0200 |
---|---|---|
committer | Daniel Neuhäuser <ich@danielneuhaeuser.de> | 2010-08-16 08:18:10 +0200 |
commit | 97bf9b01af8d00d5bc7aaa56ed4ad0e963d286dd (patch) | |
tree | 3fb26b5c32a707ccdc0fa9edcece0d84026cb946 | |
parent | 36a0aa1667639818a39d43c79bceaa7ccee0392a (diff) | |
download | sphinx-git-97bf9b01af8d00d5bc7aaa56ed4ad0e963d286dd.tar.gz |
Fix get_ratio for empty strings
-rw-r--r-- | sphinx/versioning.py | 7 | ||||
-rw-r--r-- | tests/test_versioning.py | 6 |
2 files changed, 11 insertions, 2 deletions
diff --git a/sphinx/versioning.py b/sphinx/versioning.py index 9a7cb2da6..06acc63dc 100644 --- a/sphinx/versioning.py +++ b/sphinx/versioning.py @@ -20,6 +20,9 @@ except ImportError: from sphinx.util import PeekableIterator +# anything below that ratio is considered equal/changed +VERSIONING_RATIO = 65 + def add_uids(doctree, condition): """ Adds a unique id to every node in the `doctree` which matches the condition @@ -88,7 +91,7 @@ def merge_doctrees(old, new, condition): continue else: seen.add(new_node) - if ratio < 65: + if ratio < VERSIONING_RATIO: new_node.uid = old_node.uid else: new_node.uid = uuid4().hex @@ -104,6 +107,8 @@ def get_ratio(old, new): Returns a "similiarity ratio" representing the similarity between the two strings where 0 is equal and anything above less than equal. """ + if not all([old, new]): + return VERSIONING_RATIO return levenshtein_distance(old, new) / (len(old) / 100.0) def levenshtein_distance(a, b): diff --git a/tests/test_versioning.py b/tests/test_versioning.py index 06c4ff2f1..923da2035 100644 --- a/tests/test_versioning.py +++ b/tests/test_versioning.py @@ -16,7 +16,7 @@ from docutils.statemachine import ViewList from docutils.parsers.rst.directives.html import MetaBody from sphinx import addnodes -from sphinx.versioning import add_uids, merge_doctrees +from sphinx.versioning import add_uids, merge_doctrees, get_ratio def setup_module(): global app, original, original_uids @@ -39,6 +39,10 @@ def on_doctree_resolved(app, doctree, docname): def is_paragraph(node): return node.__class__.__name__ == 'paragraph' +def test_get_ratio(): + assert get_ratio('', 'a') + assert get_ratio('a', '') + def test_add_uids(): assert len(original_uids) == 3 |