Skip to content

Commit

Permalink
Don't copy by default, add option to enable instead. (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
athackst authored Aug 24, 2023
1 parent 85c8b9f commit 2e3310e
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 24 deletions.
3 changes: 0 additions & 3 deletions examples/ok-mkdocs-docs-merge/clean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@

# Get the directory of the script
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)"

rm ${DIR}/docs/README.md
rm ${DIR}/docs/test.md
3 changes: 3 additions & 0 deletions examples/ok-with-macros/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ plugins:
include:
- ".yml"
- macros:
include_dir: "."
verbose: True
extra:
test: True
Expand Down Expand Up @@ -91,6 +92,7 @@ plugins:
include:
- .yml
- macros:
include_dir: .
verbose: true
edit_uri: ''
extra:
Expand All @@ -113,6 +115,7 @@ plugins:
include:
- .yml
- macros:
include_dir: .
verbose: true
edit_uri: ''
extra:
Expand Down
1 change: 1 addition & 0 deletions examples/ok-with-macros/example.grepout
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins:
include:
- .yml
- macros:
include_dir: .
verbose: true
edit_uri: ''
extra:
Expand Down
1 change: 1 addition & 0 deletions examples/ok-with-macros/mkdocs-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins:
include:
- ".yml"
- macros:
include_dir: "."
verbose: True
extra:
test: True
1 change: 1 addition & 0 deletions examples/ok-with-macros/module.grepout
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins:
include:
- .yml
- macros:
include_dir: .
verbose: true
edit_uri: ''
extra:
Expand Down
11 changes: 10 additions & 1 deletion mkdocs_simple_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,18 @@ class SimplePlugin(BasePlugin):
# Otherwise, the build docs directory will be a temporary directory.
('build_dir', config_options.Type(str, default='')),
#
# #### copy
#
# If set, docs will be copied to the build_docs_dir.
# Otherwise, files will be used in place.
('copy', config_options.Type(bool, default=False)),
#
# ### include_extensions (renamed)
#
# Renamed [include](#include)
('include_extensions', config_options.Deprecated(
moved_to="include", message="", removed=False)),
#
# ### include
#
# Any file in the searched directories whose name contains a string in
Expand Down Expand Up @@ -358,7 +365,9 @@ def on_files(self, files: Files, *, config):
simple = Simple(**self.config)

# Save paths to add to watch if serving
self.paths = simple.build_docs(self.dirty, self.last_build_time)
do_copy = self.config["copy"]
self.paths = simple.build_docs(
self.dirty, self.last_build_time, do_copy)
self.last_build_time = time.time()

if not self.config["merge_docs_dir"]:
Expand Down
35 changes: 23 additions & 12 deletions mkdocs_simple_plugin/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(
"""
self.build_dir = build_dir
self.folders = set(folders)
self.copy_glob = set(include)
self.doc_glob = set(include)
self.ignore_glob = set(ignore)
self.ignore_paths = set(ignore_paths)
self.semiliterate = []
Expand Down Expand Up @@ -109,12 +109,12 @@ def is_path_ignored(self, path: str = None) -> bool:
return True
return False

def should_copy_file(self, name: str) -> bool:
"""Check if file should be copied."""
def is_doc_file(self, name: str) -> bool:
"""Check if file is a desired doc file."""
def match_pattern(name, pattern):
return fnmatch.fnmatch(name, pattern) or pattern in name

return any(match_pattern(name, pattern) for pattern in self.copy_glob)
return any(match_pattern(name, pattern) for pattern in self.doc_glob)

def should_extract_file(self, name: str):
"""Check if file should be extracted."""
Expand Down Expand Up @@ -149,7 +149,11 @@ def merge_docs(self, from_dir, dirty=False):
source_file, destination_file)
self.ignore_paths.add(from_dir)

def build_docs(self, dirty=False, last_build_time=None) -> list:
def build_docs(
self,
dirty=False,
last_build_time=None,
do_copy=False) -> list:
"""Build the docs directory from workspace files."""
paths = []
files = self.get_files()
Expand All @@ -164,8 +168,9 @@ def build_docs(self, dirty=False, last_build_time=None) -> list:
build_prefix = os.path.normpath(
os.path.join(self.build_dir, from_dir))

copy_paths = self.try_copy_file(from_dir, name, build_prefix)
if copy_paths:
doc_paths = self.get_doc_file(
from_dir, name, build_prefix, do_copy)
if doc_paths:
paths.append(
SimplePath(
output_root=".",
Expand Down Expand Up @@ -207,17 +212,23 @@ def try_extract(self, from_dir: str, name: str, to_dir: str) -> list:

return []

def try_copy_file(self, from_dir: str, name: str, to_dir: str) -> list:
def get_doc_file(
self,
from_dir: str,
name: str,
to_dir: str,
do_copy: bool) -> list:
"""Copy file with the same name to a new directory.
Returns true if file copied.
"""
original = os.path.join(from_dir, name)
destination = os.path.join(to_dir, name)

if not self.should_copy_file(os.path.join(from_dir, name)):
if not self.is_doc_file(os.path.join(from_dir, name)):
return []

os.makedirs(to_dir, exist_ok=True)
copy(original, destination)
if (do_copy):
destination = os.path.join(to_dir, name)
os.makedirs(to_dir, exist_ok=True)
copy(original, destination)
return [original]
16 changes: 8 additions & 8 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ def test_ignored_mkdocsignore_empty(self):
self.assertIn("directory/*", simple_test.ignore_glob)
self.assertEqual(1, len(simple_test.ignore_glob))

def test_should_copy(self):
"""Test should_copy."""
def test_is_doc_file(self):
"""Test if doc file."""
simple_test = simple.Simple(**self.default_settings)
simple_test.copy_glob = ["*.md"]
self.assertTrue(simple_test.should_copy_file(name="helloworld.md"))
self.assertFalse(simple_test.should_copy_file(name="md.helloworld"))
simple_test.doc_glob = ["*.md"]
self.assertTrue(simple_test.is_doc_file(name="helloworld.md"))
self.assertFalse(simple_test.is_doc_file(name="md.helloworld"))

simple_test.copy_glob = [".pages"]
self.assertTrue(simple_test.should_copy_file(name=".pages"))
simple_test.doc_glob = [".pages"]
self.assertTrue(simple_test.is_doc_file(name=".pages"))

def test_get_files(self):
"""Test getting all files."""
Expand Down Expand Up @@ -248,7 +248,7 @@ def test_build_docs(self):

simple_test.ignore_glob = set(["foo/bat/**"])
simple_test.folders = set(["foo/"])
simple_test.copy_glob = set(["*.md", ".pages"])
simple_test.doc_glob = set(["*.md", ".pages"])

paths = []
built_paths = simple_test.build_docs()
Expand Down

0 comments on commit 2e3310e

Please sign in to comment.