From 0042efb917531671afc9fa4a142609a7ccda91ef Mon Sep 17 00:00:00 2001 From: Matt Katz Date: Sun, 2 Aug 2020 16:40:35 -0400 Subject: [PATCH] add ability to prefix file names You may want to output something like "docs/tests/test_{name}.py" and expect "nest/deep.md" to output "docs/tests/nest/test_deep.py" before this commmit nested files would output: "docs/tests/test_nest/deep.py" Here pathlib makes for less code --- mkcodes.py | 49 ++++++++++++++++++++----------------- tests/data/nest/more/why.md | 9 +++++++ tests/test.py | 10 ++++++++ 3 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 tests/data/nest/more/why.md diff --git a/mkcodes.py b/mkcodes.py index 25e6a09..a41047f 100644 --- a/mkcodes.py +++ b/mkcodes.py @@ -1,4 +1,5 @@ import os +from pathlib import Path import re import glob import warnings @@ -63,29 +64,23 @@ def extendMarkdown(self, md, md_globals): doctestextension = DoctestExtension() markdowner = markdown.Markdown(extensions=[doctestextension]) - markdowner.convertFile(filepath, output=os.devnull) + markdowner.convertFile(str(filepath), output=os.devnull) return codeblocks -def is_markdown(f): - markdown_extensions = ['.markdown', '.mdown', '.mkdn', '.mkd', '.md'] - return os.path.splitext(f)[1] in markdown_extensions - - -def get_nested_files(directory, depth): - for i in glob.iglob(directory + '/*'): - if os.path.isdir(i): - yield from get_nested_files(i, depth+1) - elif is_markdown(i): - yield (i, depth) - - def get_files(inputs): + """ Take in an iterable of paths, yield the files and parent dirs in those paths""" + markdown_extensions = ['.markdown', '.mdown', '.mkdn', '.mkd', '.md'] for i in inputs: - if os.path.isdir(i): - yield from get_nested_files(i, 0) - elif is_markdown(i): - yield (i, 0) + path = Path(i) + if path.is_dir(): + """ let's iterate the directory and yield files """ + for child in path.rglob('*'): + if child.is_file() and child.suffix in markdown_extensions: + yield child, path + else: + if path.suffix in markdown_extensions: + yield path, path.parent def makedirs(directory): @@ -116,15 +111,23 @@ def makedirs(directory): help='Allow code blocks without language hints.') def main(inputs, output, github, safe): collect_codeblocks = github_codeblocks if github else markdown_codeblocks + # to output deep trees with a file pattern + # we should break out the directory and the filename pattern + outputbasedir = Path(output).parent + outputbasename = Path(output).name - for filepath, depth in get_files(inputs): + for filepath, input_path in get_files(inputs): codeblocks = collect_codeblocks(filepath, safe) if codeblocks: - filename = os.path.splitext(filepath)[0] - outputname = os.sep.join(filename.split(os.sep)[-1-depth:]) - - outputfilename = output.format(name=outputname) + #we want the path to the file, and the file without an extension + fp = Path(filepath) + filedir =fp.parent.relative_to(input_path) + filename = fp.stem + + # stitch together the OUTPUT base directory, with the input directories + # add the file format at the end. + outputfilename = outputbasedir / filedir / outputbasename.format(name=filename) outputdir = os.path.dirname(outputfilename) if not os.path.exists(outputdir): diff --git a/tests/data/nest/more/why.md b/tests/data/nest/more/why.md new file mode 100644 index 0000000..fe03402 --- /dev/null +++ b/tests/data/nest/more/why.md @@ -0,0 +1,9 @@ +# why? + +We want to make sure that in more complext documentation structures, which may have multiple sub directories, we are still formatting name and paths correctly. + +```py +print("oh that makes sense") +``` + +Good. diff --git a/tests/test.py b/tests/test.py index e37eb89..83563cf 100644 --- a/tests/test.py +++ b/tests/test.py @@ -94,6 +94,7 @@ def test_directory_recursive(self): self.assertTrue(self._output_path_exists('other.py')) self.assertTrue(self._output_path_exists('nest/deep.py')) self.assertFalse(self._output_path_exists('not_markdown.py')) + self.assertTrue(self._output_path_exists('nest/more/why.py')) def test_multiple(self): self.call( @@ -121,6 +122,15 @@ def test_unexistant_output_directory(self): backticks = range(5, 7) """) + def test_prefixed_deep_blocks(self): + self.call( + '--output', 'tests/output/test_{name}.py', '--github', + 'tests/data') + self.assertTrue(self._output_path_exists('test_some.py')) + self.assertTrue(self._output_path_exists('test_other.py')) + self.assertTrue(self._output_path_exists('nest/test_deep.py')) + self.assertTrue(self._output_path_exists('nest/more/test_why.py')) + @unittest.skip def test_glob(self): raise NotImplementedError