Commit 42b7de5a authored by Chris Lamb's avatar Chris Lamb 💬

presenters: Add markdown output support. (Closes: #848141)

Signed-off-by: Chris Lamb's avatarChris Lamb <lamby@debian.org>
parent b22c4ffa
......@@ -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)')
......
# -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2017 Chris Lamb <lamby@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)
......@@ -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,
......
......@@ -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:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment