From e31a3425701f7b8709c16f975356c0b0b8bc7f7b Mon Sep 17 00:00:00 2001 From: =?utf8?q?G=C3=BCnter=20Milde?= Date: Wed, 29 Jun 2022 17:20:25 +0200 Subject: [PATCH] Don't add indentation to blank lines. Only warn if output has same age. No error for same "mtime" of in- and output files Avoid trailing whitespace: do not add whitespace to blank lines. --- pylit.py | 14 +++++++++----- test/pylit_test.py | 40 ++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/pylit.py b/pylit.py index 6985197..312b1bc 100755 --- a/pylit.py +++ b/pylit.py @@ -1063,8 +1063,10 @@ class Code2Text(TextCodeConverter): self._add_code_block_marker = False self.state = "code_block" for line in lines: - yield " "*self.codeindent + line - + if not line.rstrip(): + yield line # don't add indent to blank lines + else: + yield " "*self.codeindent + line # strip_code_block_marker() @@ -1549,7 +1551,10 @@ def open_streams(infile = '-', outfile = '-', overwrite='update', **keyw): elif overwrite == 'no' and os.path.exists(outfile): raise IOError(17, "Output file exists!", outfile) elif overwrite == 'update' and is_newer(outfile, infile) is None: - raise IOError(0, "Output file is as old as input file!", outfile) + print('Output file "%s" is as old as input file!\n' + 'Use "--overwrite=yes", if you want to overwrite it.' % outfile, + file=sys.stderr) + sys.exit() elif overwrite == 'update' and is_newer(outfile, infile): raise IOError(1, "Output file is newer than input file!", outfile) else: @@ -1559,7 +1564,6 @@ def open_streams(infile = '-', outfile = '-', overwrite='update', **keyw): # is_newer() # ~~~~~~~~~~ -# # :: def is_newer(path1, path2): @@ -1781,7 +1785,7 @@ def main(args=sys.argv[1:], **settings): out_stream.write(str(converter)) if out_stream is not sys.stdout: - print('output written to %r' % out_stream.name) + print('Output written to %r' % out_stream.name) out_stream.close() # If input and output are from files, _`set the modification time` (`mtime`) of diff --git a/test/pylit_test.py b/test/pylit_test.py index 6f42305..3cf9b98 100644 --- a/test/pylit_test.py +++ b/test/pylit_test.py @@ -46,18 +46,18 @@ import nose text = """.. #!/usr/bin/env python # -*- coding: iso-8859-1 -*- - \n\ + Leading text in several paragraphs followed by a literal block:: block1 = 'first block' - \n\ + Some more text and the next block. :: block2 = 'second block' print(block1, block2) - \n\ + Trailing text. """ @@ -792,17 +792,17 @@ class test_Code2Text(object): outstr = str(Code2Text(codedata, comment_string="##", strip=True)) print(outstr) assert outstr == "" - data = ["# ::\n", - "\n", - "block1 = 'first block'\n", - "\n", - "## more text"] - soll = "\n".join(['.. # ::', # leading code block as header - ' ', - " block1 = 'first block'", - ' ', - ' more text'] # keep space (not part of comment string) - ) + data = ['# ::\n', + '\n', + 'block1 = "first block"\n', + '\n', + '## more text'] + soll = ('.. # ::\n' # leading code block as header + '\n' + ' block1 = "first block"\n' + '\n' + ' more text' # keep space (not part of comment string) + ) outstr = str(Code2Text(data, comment_string="##")) print("soll:", repr(soll)) print("ist: ", repr(outstr)) @@ -819,7 +819,7 @@ class test_Code2Text(object): soll = ['.. code-block:: python\n', '\n', " block1 = 'first block'\n", - ' \n', + '\n', ' more text\n'] # keep space (not part of comment string) converter = Code2Text(data, code_block_marker='.. code-block::') @@ -903,7 +903,7 @@ block1 = 'first block' """:: block1 = 'first block' - \n\ + more text """) @@ -967,7 +967,7 @@ block1 = 'first block' block1 = 'first block' # commented code - \n\ + text again """, """ @@ -1065,7 +1065,7 @@ codesamples["no matching comment, just code"] = ( print('ende') """, """.. print('hello world') - \n\ + print('ende') """) @@ -1092,7 +1092,7 @@ print('hello world') """, """.. #!/usr/bin/env python # -*- coding: iso-8859-1 -*- - \n\ + a classical example with header:: print('hello world') @@ -1107,7 +1107,7 @@ codesamples["standard header, followed by code"] = ( print('hello world') """, """.. #!/usr/bin/env python - \n\ + print('hello world') """, "") -- 2.11.4.GIT