1. jacobmason
  2. sphinx-web-support

Commits

DasIch  committed e3f214e

Fix get_ratio for empty strings

  • Participants
  • Parent commits de63799
  • Branches default

Comments (0)

Files changed (2)

File sphinx/versioning.py

View file
 
 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
             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
     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):

File tests/test_versioning.py

View file
 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
 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