Skip to content

Commit

Permalink
add ability to prefix file names
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Matt Katz authored and ryneeverett committed Aug 4, 2020
1 parent 81db53c commit 0042efb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
49 changes: 26 additions & 23 deletions mkcodes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pathlib import Path
import re
import glob
import warnings
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 9 additions & 0 deletions tests/data/nest/more/why.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 10 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0042efb

Please sign in to comment.