Commits

Georg Brandl committed cb71f22

Add a text-based diff view e.g. for display in emails.

Comments (0)

Files changed (3)

sphinx/websupport/__init__.py

                 username = 'Anonymous'
             else:
                 raise errors.UserNotAuthorizedError()
-        text = self._parse_comment_text(text)
-        comment = self.storage.add_comment(text, displayed, username,
+        parsed = self._parse_comment_text(text)
+        comment = self.storage.add_comment(parsed, displayed, username,
                                            time, proposal, node_id,
                                            parent_id, moderator)
+        comment['original_text'] = text
         if not displayed and self.moderation_callback:
             self.moderation_callback(comment)
         return comment

sphinx/websupport/storage/differ.py

     """
     highlight_regex = re.compile(r'([\+\-\^]+)')
 
-    def make_html(self, source, proposal):
+    def __init__(self, source, proposal):
+        proposal = escape(proposal)
+
+        differ = Differ()
+        self.diff = list(differ.compare(source.splitlines(1),
+                                        proposal.splitlines(1)))
+
+    def make_text(self):
+        return '\n'.join(self.diff)
+
+    def make_html(self):
         """Return the HTML representation of the differences between
         `source` and `proposal`.
 
         :param source: the original text
         :param proposal: the proposed text
         """
-        proposal = escape(proposal)
-
-        differ = Differ()
-        diff = list(differ.compare(source.splitlines(1),
-                                   proposal.splitlines(1)))
         html = []
+        diff = self.diff[:]
         line = diff.pop(0)
         next = diff.pop(0)
         while True:

sphinx/websupport/storage/sqlalchemystorage.py

                     proposal, node_id, parent_id, moderator):
         session = Session()
         proposal_diff = None
+        proposal_diff_text = None
 
         if node_id and proposal:
             node = session.query(Node).filter(Node.id == node_id).one()
-            differ = CombinedHtmlDiff()
-            proposal_diff = differ.make_html(node.source, proposal)
+            differ = CombinedHtmlDiff(node.source, proposal)
+            proposal_diff = differ.make_html()
+            proposal_diff_text = differ.make_text()
         elif parent_id:
             parent = session.query(Comment.displayed).\
                 filter(Comment.id == parent_id).one()
         session.commit()
         d = comment.serializable()
         d['document'] = comment.node.document
+        d['proposal_diff_text'] = proposal_diff_text
         session.close()
         return d