Skip to content

Commit

Permalink
Only write code files when there are code blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryneeverett committed Jan 5, 2016
1 parent 17d6208 commit 4e2fc3f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
23 changes: 12 additions & 11 deletions mkcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Empty file added tests/data/nocode.md
Empty file.
5 changes: 5 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 4e2fc3f

Please sign in to comment.