[diffoscope] 02/05: presenters: Add markdown output support. (Closes: #848141)

Chris Lamb chris at chris-lamb.co.uk
Sat Jan 21 11:16:05 UTC 2017


This is an automated email from the git hooks/post-receive script.

lamby pushed a commit to branch master
in repository diffoscope.

commit 42b7de5adf56e8c06e166c732f59d22b16931457
Author: Chris Lamb <lamby at debian.org>
Date:   Fri Jan 20 17:01:22 2017 +1100

    presenters: Add markdown output support. (Closes: #848141)
    
    Signed-off-by: Chris Lamb <lamby at debian.org>
---
 diffoscope/main.py                |  2 ++
 diffoscope/presenters/markdown.py | 54 +++++++++++++++++++++++++++++++++++++++
 diffoscope/presenters/utils.py    |  9 +++++++
 tests/test_main.py                |  9 +++++++
 4 files changed, 74 insertions(+)

diff --git a/diffoscope/main.py b/diffoscope/main.py
index bb97b61..e26b83a 100644
--- a/diffoscope/main.py
+++ b/diffoscope/main.py
@@ -93,6 +93,8 @@ def create_parser():
                         '"disable" to disable JavaScript. When omitted '
                         'diffoscope will try to create a symlink to a system '
                         'installation. Known locations: %s' % ', '.join(JQUERY_SYSTEM_LOCATIONS))
+    group1.add_argument('--markdown', metavar='OUTPUT_FILE', dest='markdown_output',
+                        help='Write Markdown text output to given file (use - for stdout)')
     group1.add_argument('--profile', metavar='OUTPUT_FILE', dest='profile_output',
                         help='Write profiling info to given file (use - for stdout)')
 
diff --git a/diffoscope/presenters/markdown.py b/diffoscope/presenters/markdown.py
new file mode 100644
index 0000000..88fc4c6
--- /dev/null
+++ b/diffoscope/presenters/markdown.py
@@ -0,0 +1,54 @@
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2017 Chris Lamb <lamby at debian.org>
+#
+# diffoscope is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# diffoscope is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
+
+import textwrap
+
+from .base import Presenter
+
+
+class MarkdownTextPresenter(Presenter):
+    def __init__(self, print_func):
+        self.print_func = print_func
+        super().__init__()
+
+    def visit_difference(self, difference):
+        if difference.source1 == difference.source2:
+            self.title(difference.source1)
+        else:
+            self.title("Comparing {} & {}".format(
+                difference.source1,
+                difference.source2,
+            ))
+
+        for x in difference.comments:
+            self.print_func(x)
+            self.print_func()
+
+        if difference.unified_diff:
+            self.print_func(textwrap.indent(difference.unified_diff, '    '))
+
+    def title(self, val):
+        prefix = '#' * min(self.depth + 1, 6)
+
+        self.print_func("{} {}".format(prefix, val))
+        self.print_func()
+
+def output_markdown(difference, print_func, color=False):
+    presenter = MarkdownTextPresenter(print_func)
+    presenter.visit(difference)
diff --git a/diffoscope/presenters/utils.py b/diffoscope/presenters/utils.py
index dfbd019..df77806 100644
--- a/diffoscope/presenters/utils.py
+++ b/diffoscope/presenters/utils.py
@@ -26,6 +26,7 @@ from ..profiling import profile
 
 from .text import output_text
 from .html import output_html, output_html_directory
+from .markdown import output_markdown
 
 logger = logging.getLogger(__name__)
 
@@ -47,6 +48,10 @@ def output_all(difference, parsed_args, has_differences):
             'fn': html,
             'target': parsed_args.html_output,
         },
+        'markdown': {
+            'fn': markdown,
+            'target': parsed_args.markdown_output,
+        },
         'html_directory': {
             'fn': html_directory,
             'target': parsed_args.html_output_directory,
@@ -89,6 +94,10 @@ def html(difference, parsed_args, has_differences):
             print_func=fn,
         )
 
+def markdown(difference, parsed_args, has_differences):
+    with make_printer(parsed_args.markdown_output) as fn:
+        output_markdown(difference, print_func=fn)
+
 def html_directory(difference, parsed_args, has_differences):
     output_html_directory(
         parsed_args.html_output_directory,
diff --git a/tests/test_main.py b/tests/test_main.py
index 5431f77..160bda4 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -116,6 +116,15 @@ def test_text_option_with_stdiout(capsys):
     assert err == ''
     assert out.startswith('--- ')
 
+def test_markdown(capsys):
+    args = ['--markdown', '-', *TEST_TARS]
+    with pytest.raises(SystemExit) as excinfo:
+        main(args)
+    assert excinfo.value.code == 1
+    out, err = capsys.readouterr()
+    assert err == ''
+    assert out.startswith('# Comparing')
+
 def test_no_report_option(capsys):
     args = [*TEST_TARS]
     with pytest.raises(SystemExit) as excinfo:

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git



More information about the Reproducible-commits mailing list