# Path: nbdiff/comparable.py
# class CellComparator():
# 
#     def __init__(self, data, check_modified=False):
#         self.data = data
#         self.check_modified = check_modified
# 
#     def __eq__(self, other):
#         return self.equal(self.data, other.data)
# 
#     def equal(self, cell1, cell2):
#         if not cell1["cell_type"] == cell2["cell_type"]:
#             return False
#         elif cell1["cell_type"] == "heading":
#             if not cell1["level"] == cell2["level"]:
#                 return False
#             else:
#                 if cell1['source'] == cell2['source']:
#                     return True
#                 result = diff(
#                     cell1["source"].split(' '),
#                     cell2["source"].split(' ')
#                 )
#                 modified = 0.0
#                 unchanged = 0.0
#                 for dict in result:
#                     if dict['state'] == "added" or dict['state'] == "deleted":
#                         modified += 1
#                     elif dict['state'] == "unchanged":
#                         unchanged += 1
#                 modifiedness = unchanged/(modified + unchanged)
#                 if modifiedness >= 0.6:
#                     return BooleanPlus(True, True)
#                 else:
#                     return False
#         elif self.istextcell(cell1):
#             return cell1["source"] == cell2["source"]
#         else:
#             return self.compare_cells(cell1, cell2)
# 
#     def istextcell(self, cell):
#         return "source" in cell
# 
#     def equaloutputs(self, output1, output2):
#         if not len(output1) == len(output2):
#             return False
#         for i in range(0, len(output1)):
#             if not output1[i] == output2[i]:
#                 return False
#         return True
# 
#     def compare_cells(self, cell1, cell2):
#         '''
#         return true if exactly equal or if equal but modified,
#         otherwise return false
#         return type: BooleanPlus
#         '''
#         eqlanguage = cell1["language"] == cell2["language"]
#         eqinput = cell1["input"] == cell2["input"]
#         eqoutputs = self.equaloutputs(cell1["outputs"], cell2["outputs"])
# 
#         if eqlanguage and eqinput and eqoutputs:
#             return BooleanPlus(True, False)
#         elif not self.check_modified:
#             return BooleanPlus(False, False)
# 
#         input1 = u"".join(cell1['input'])
#         input2 = u"".join(cell2['input'])
#         similarity_percent = Levenshtein.ratio(input1, input2)
#         if similarity_percent >= 0.65:
#             return BooleanPlus(True, True)
#         return BooleanPlus(False, False)
# 
# Path: nbdiff/notebook_diff.py
# def cells_diff(before_cells, after_cells, check_modified=False):
#     '''Diff two arrays of cells.'''
#     before_comps = [
#         CellComparator(cell, check_modified=check_modified)
#         for cell in before_cells
#     ]
#     after_comps = [
#         CellComparator(cell, check_modified=check_modified)
#         for cell in after_cells
#     ]
#     diff_result = diff(
#         before_comps,
#         after_comps,
#         check_modified=check_modified
#     )
#     return diff_result
# 
# def words_diff(before_words, after_words):
#     '''Diff the words in two strings.
# 
#     This is intended for use in diffing prose and other forms of text
#     where line breaks have little semantic value.
# 
#     Parameters
#     ----------
#     before_words : str
#         A string to be used as the baseline version.
#     after_words : str
#         A string to be compared against the baseline.
# 
#     Returns
#     -------
#     diff_result : A list of dictionaries containing diff information.
#     '''
#     before_comps = before_words.split()
#     after_comps = after_words.split()
# 
#     diff_result = diff(
#         before_comps,
#         after_comps
#     )
#     return diff_result
# 
# def lines_diff(before_lines, after_lines, check_modified=False):
#     '''Diff the lines in two strings.
# 
#     Parameters
#     ----------
#     before_lines : iterable
#         Iterable containing lines used as the baseline version.
#     after_lines : iterable
#         Iterable containing lines to be compared against the baseline.
# 
#     Returns
#     -------
#     diff_result : A list of dictionaries containing diff information.
#     '''
#     before_comps = [
#         LineComparator(line, check_modified=check_modified)
#         for line in before_lines
#     ]
#     after_comps = [
#         LineComparator(line, check_modified=check_modified)
#         for line in after_lines
#     ]
#     diff_result = diff(
#         before_comps,
#         after_comps,
#         check_modified=check_modified
#     )
#     return diff_result
# 
# def diff_modified_items(cellslist):
#     result = {}
#     for i in range(len(cellslist)):
#         if cellslist[i]['state'] == 'modified':
#             if cellslist[i]['originalvalue'].data['cell_type'] == 'heading':
#                 result[i] = diff(
#                     cellslist[i]['originalvalue'].data["source"].split(),
#                     cellslist[i]['modifiedvalue'].data["source"].split(),
#                 )
#             else:
#                 result[i] = diff(
#                     cellslist[i]['originalvalue'].data["input"].splitlines(),
#                     cellslist[i]['modifiedvalue'].data["input"].splitlines(),
#                 )
#     return result


from nbdiff.comparable import CellComparator
from nbdiff.notebook_diff import (
    cells_diff,
    words_diff,
    lines_diff,
    diff_modified_items,
)


def test_diff_modified_items():
    header_item = {
        'state': 'modified',
        'originalvalue': CellComparator({
            'cell_type': 'heading',
            'source': 'This is a header',
        }),
        'modifiedvalue': CellComparator({
            'cell_type': 'heading',
            'source': 'This is a different header',
        }),
    }
    code_item = {
        'state': 'modified',
        'originalvalue': CellComparator({
            'cell_type': 'code',
            'input': 'x = 10\ny = 10\n',
        }),
        'modifiedvalue': CellComparator({
            'cell_type': 'code',
            'input': 'x = 11\ny = 10\n',
        }),
    }
    cellslist = [
        {'state': 'added', 'value': 'foo'},
        header_item,
        code_item,
    ]

# Path: nbdiff/comparable.py
# class CellComparator():
# 
#     def __init__(self, data, check_modified=False):
#         self.data = data
#         self.check_modified = check_modified
# 
#     def __eq__(self, other):
#         return self.equal(self.data, other.data)
# 
#     def equal(self, cell1, cell2):
#         if not cell1["cell_type"] == cell2["cell_type"]:
#             return False
#         elif cell1["cell_type"] == "heading":
#             if not cell1["level"] == cell2["level"]:
#                 return False
#             else:
#                 if cell1['source'] == cell2['source']:
#                     return True
#                 result = diff(
#                     cell1["source"].split(' '),
#                     cell2["source"].split(' ')
#                 )
#                 modified = 0.0
#                 unchanged = 0.0
#                 for dict in result:
#                     if dict['state'] == "added" or dict['state'] == "deleted":
#                         modified += 1
#                     elif dict['state'] == "unchanged":
#                         unchanged += 1
#                 modifiedness = unchanged/(modified + unchanged)
#                 if modifiedness >= 0.6:
#                     return BooleanPlus(True, True)
#                 else:
#                     return False
#         elif self.istextcell(cell1):
#             return cell1["source"] == cell2["source"]
#         else:
#             return self.compare_cells(cell1, cell2)
# 
#     def istextcell(self, cell):
#         return "source" in cell
# 
#     def equaloutputs(self, output1, output2):
#         if not len(output1) == len(output2):
#             return False
#         for i in range(0, len(output1)):
#             if not output1[i] == output2[i]:
#                 return False
#         return True
# 
#     def compare_cells(self, cell1, cell2):
#         '''
#         return true if exactly equal or if equal but modified,
#         otherwise return false
#         return type: BooleanPlus
#         '''
#         eqlanguage = cell1["language"] == cell2["language"]
#         eqinput = cell1["input"] == cell2["input"]
#         eqoutputs = self.equaloutputs(cell1["outputs"], cell2["outputs"])
# 
#         if eqlanguage and eqinput and eqoutputs:
#             return BooleanPlus(True, False)
#         elif not self.check_modified:
#             return BooleanPlus(False, False)
# 
#         input1 = u"".join(cell1['input'])
#         input2 = u"".join(cell2['input'])
#         similarity_percent = Levenshtein.ratio(input1, input2)
#         if similarity_percent >= 0.65:
#             return BooleanPlus(True, True)
#         return BooleanPlus(False, False)
# 
# Path: nbdiff/notebook_diff.py
# def cells_diff(before_cells, after_cells, check_modified=False):
#     '''Diff two arrays of cells.'''
#     before_comps = [
#         CellComparator(cell, check_modified=check_modified)
#         for cell in before_cells
#     ]
#     after_comps = [
#         CellComparator(cell, check_modified=check_modified)
#         for cell in after_cells
#     ]
#     diff_result = diff(
#         before_comps,
#         after_comps,
#         check_modified=check_modified
#     )
#     return diff_result
# 
# def words_diff(before_words, after_words):
#     '''Diff the words in two strings.
# 
#     This is intended for use in diffing prose and other forms of text
#     where line breaks have little semantic value.
# 
#     Parameters
#     ----------
#     before_words : str
#         A string to be used as the baseline version.
#     after_words : str
#         A string to be compared against the baseline.
# 
#     Returns
#     -------
#     diff_result : A list of dictionaries containing diff information.
#     '''
#     before_comps = before_words.split()
#     after_comps = after_words.split()
# 
#     diff_result = diff(
#         before_comps,
#         after_comps
#     )
#     return diff_result
# 
# def lines_diff(before_lines, after_lines, check_modified=False):
#     '''Diff the lines in two strings.
# 
#     Parameters
#     ----------
#     before_lines : iterable
#         Iterable containing lines used as the baseline version.
#     after_lines : iterable
#         Iterable containing lines to be compared against the baseline.
# 
#     Returns
#     -------
#     diff_result : A list of dictionaries containing diff information.
#     '''
#     before_comps = [
#         LineComparator(line, check_modified=check_modified)
#         for line in before_lines
#     ]
#     after_comps = [
#         LineComparator(line, check_modified=check_modified)
#         for line in after_lines
#     ]
#     diff_result = diff(
#         before_comps,
#         after_comps,
#         check_modified=check_modified
#     )
#     return diff_result
# 
# def diff_modified_items(cellslist):
#     result = {}
#     for i in range(len(cellslist)):
#         if cellslist[i]['state'] == 'modified':
#             if cellslist[i]['originalvalue'].data['cell_type'] == 'heading':
#                 result[i] = diff(
#                     cellslist[i]['originalvalue'].data["source"].split(),
#                     cellslist[i]['modifiedvalue'].data["source"].split(),
#                 )
#             else:
#                 result[i] = diff(
#                     cellslist[i]['originalvalue'].data["input"].splitlines(),
#                     cellslist[i]['modifiedvalue'].data["input"].splitlines(),
#                 )
#     return result


# Path: tests/test_notebook_diff.py
from nbdiff.comparable import CellComparator
from nbdiff.notebook_diff import (
    cells_diff,
    words_diff,
    lines_diff,
    diff_modified_items,
)



def test_diff_modified_items():
    header_item = {
        'state': 'modified',
        'originalvalue': CellComparator({
            'cell_type': 'heading',
            'source': 'This is a header',
        }),
        'modifiedvalue': CellComparator({
            'cell_type': 'heading',
            'source': 'This is a different header',
        }),
    }
    code_item = {
        'state': 'modified',
        'originalvalue': CellComparator({
            'cell_type': 'code',
            'input': 'x = 10\ny = 10\n',
        }),
        'modifiedvalue': CellComparator({
            'cell_type': 'code',
            'input': 'x = 11\ny = 10\n',
        }),
    }
    cellslist = [
        {'state': 'added', 'value': 'foo'},
        header_item,
        code_item,
    ]

    result = diff_modified_items(cellslist)