diff --git a/mkcodes.py b/mkcodes.py index 32ff3ab..37afdcc 100644 --- a/mkcodes.py +++ b/mkcodes.py @@ -100,18 +100,19 @@ def main(inputs, output, github, safe): collect_codeblocks = github_codeblocks if github else markdown_codeblocks for filepath, depth in get_files(inputs): - code = '\n\n'.join(collect_codeblocks(filepath, safe)) + codeblocks = collect_codeblocks(filepath, safe) - filename = os.path.splitext(filepath)[0] - outputname = os.sep.join(filename.split(os.sep)[-1-depth:]) + if codeblocks: + filename = os.path.splitext(filepath)[0] + outputname = os.sep.join(filename.split(os.sep)[-1-depth:]) - outputfilename = output.format(name=outputname) + outputfilename = output.format(name=outputname) - outputdir = os.path.dirname(outputfilename) - if not os.path.exists(outputdir): - os.makedirs(outputdir) - with open(os.path.join(outputdir, '__init__.py'), 'w'): - pass + outputdir = os.path.dirname(outputfilename) + if not os.path.exists(outputdir): + os.makedirs(outputdir) + with open(os.path.join(outputdir, '__init__.py'), 'w'): + pass - with open(outputfilename, 'w') as outputfile: - outputfile.write(code) + with open(outputfilename, 'w') as outputfile: + outputfile.write('\n\n'.join(codeblocks)) diff --git a/tests/data/nocode.md b/tests/data/nocode.md new file mode 100644 index 0000000..e69de29 diff --git a/tests/test.py b/tests/test.py index 3fd404f..6454e56 100644 --- a/tests/test.py +++ b/tests/test.py @@ -73,6 +73,11 @@ def test_file(self): self.call() self.assertTrue(os.path.exists(self.output)) + def test_file_without_code(self): + """Code files should not be written for markdown files with no code.""" + self.call(inputfile='tests/data/nocode.md') + self.assertFalse(os.path.exists('tests/nocode.py')) + def test_directory(self): self.call(inputfile='tests/data') self.assertTrue(os.path.exists(self.output))