diff --git a/__tests__/integration/fixtures/ok-with-hook/docs/index.md b/__tests__/integration/fixtures/ok-with-hook/docs/index.md new file mode 100644 index 0000000..8ead955 --- /dev/null +++ b/__tests__/integration/fixtures/ok-with-hook/docs/index.md @@ -0,0 +1,53 @@ +# Neptunia vapor + +## Has mente et + +Lorem markdownum aequora Famemque, a ramos regna Ulixem verba, posito qui +nubilus membra. Pendet dixit canisve, hanc quoque animosa **veni**, inducere. +Fer quem, mihi vallem; reposcunt aequoreae Haec, inposita. Eras dicere sic! Ore +ad at nec pius rivi pectora Pandione amari pietas Ulixem. + +> Argenteus sinit. Corpore non Booten Uranie, in hac has dixi herbas. *Oculos +> omnes Dixerat* suae coloribus et antris spernitque silva, dixit. + +Mihi [quamvis](http://caput-latebris.com/), ardua venit nam, de mox in et inquit +incisa relevare reseminet Cycnus forma sororis. In mater artus utque iustis me +vestrae magno datque, quaque multumque oscula iubemur. + +## Aditumque ubi + +Brevibus cervice inmunibus sunt peragit, [sua tanto +insuper](http://ampycuslyncides.org/), arva ubi: torto mixta. Sanguis +conscendunt sumit, utilis illo nec quaecumque ad urbis inpositaque. Alto sic +esse resumere albet, pharetras sola, erat, [non longo +paviunt](http://verba.org/) dives aurem. Nomina genus nulli insignia, carpere +dare quo vident, *nox flemus sed* Telamon auras, erant illuc, tantum. Regia +[duroque opto](http://www.flectathiberi.io/estredeunt), segetes paterna de +crimen! + + var edutainment_php = plain_ring_scan(adfUgcImap * delPanel); + var click_meta_dv = 3 + + systemScrollingDocument.snippetCdAnimated.memoryInstallHost(service( + bezel_trojan_plagiarism, 1, base_resources), intelligence, + umlWiSkin.software_olap.on(quadHocData)); + var bare = jumper_server_solid - rupE + 3; + var timeRegistryStandby = disk_ppc_menu + gigahertzCifsRss; + if (im(correction_desktop, disk_integer_soft(serviceLogic, data_zone, + daw_ssid_web)) > graphicsExpansionBug + active) { + apiSpam = storageVisual + 3; + } + +Mors cum cum proturbat, gente nasci Semiramis sonum, toto est eris facto dapibus +propulit; a! Rogantis ira canat, [in nec +sanguine](http://acceptiordefensus.io/accepto) probro inmunesque molliter +sustineat quem quamquam parentis non. Per **quod nec** rapit ipsa nec, +territaque fallacis fluviis progenies aratro. Colla puer regesta si Haec +silentia omen Paeonia, harenis puer Marmaridae pectora ingens miratur Thisbes +veri. Plaga profugi, iram, praestans, pro hanc vehit, vites. + +Illa per acerris vivit difficile pulveris, faciebat pontus populabile utque? In +flagrant umbrae marito, coniunx parari, **quoque sanguine Nisi**, ego +[saxo](http://cervice-fessusque.com/), fovet, ait unda contigit. Gaudet in, +herba quibus? Ore ne ambo mecumque pectoraque alta: viri illi in puer corpore +expersque pharetra solutum proximitas. Gorgonis adempto, in montes terga quae +nec remoratur nives perque insidias exsiluit tribuitque mille. diff --git a/__tests__/integration/fixtures/ok-with-hook/mkdocs.yml b/__tests__/integration/fixtures/ok-with-hook/mkdocs.yml new file mode 100644 index 0000000..c88c552 --- /dev/null +++ b/__tests__/integration/fixtures/ok-with-hook/mkdocs.yml @@ -0,0 +1,10 @@ +site_name: "Example" + +docs_dir: ./docs + +plugins: + - monorepo + +nav: + - Home: index.md + - project-1: "!include project-2/mkdocs.yml" \ No newline at end of file diff --git a/__tests__/integration/fixtures/ok-with-hook/project-1/mkdocs.yml b/__tests__/integration/fixtures/ok-with-hook/project-1/mkdocs.yml new file mode 100644 index 0000000..67bfa1a --- /dev/null +++ b/__tests__/integration/fixtures/ok-with-hook/project-1/mkdocs.yml @@ -0,0 +1,11 @@ + + +nav: + - Home: index.md + - About: about.md + + +mono_docs_hook: + hook: + - my_hook.py + - $TEMP_DOCS_DIR \ No newline at end of file diff --git a/__tests__/integration/fixtures/ok-with-hook/project-1/my_hook.py b/__tests__/integration/fixtures/ok-with-hook/project-1/my_hook.py new file mode 100644 index 0000000..292fbb2 --- /dev/null +++ b/__tests__/integration/fixtures/ok-with-hook/project-1/my_hook.py @@ -0,0 +1,16 @@ +import sys +from pathlib import Path + +temp_docs_dir = Path(sys.argv[1]) + +with open(temp_docs_dir / "index.md", "w") as md_f: + md_f.writelines([ + "# Hello", + "Welcome to project 1's docs!" + ]) + +with open(temp_docs_dir / "about.md", "w") as md_f: + md_f.writelines([ + "# About", + "This is the about page." + ]) \ No newline at end of file diff --git a/__tests__/integration/fixtures/ok-with-hook/project-2/mkdocs.yml b/__tests__/integration/fixtures/ok-with-hook/project-2/mkdocs.yml new file mode 100644 index 0000000..4326fe0 --- /dev/null +++ b/__tests__/integration/fixtures/ok-with-hook/project-2/mkdocs.yml @@ -0,0 +1,12 @@ + + +nav: + - Home: index.md + - About: about.md + + +mono_docs_hook: + cwd: ../project-1 + hook: + - my_hook.py + - $TEMP_DOCS_DIR \ No newline at end of file diff --git a/__tests__/integration/test.bats b/__tests__/integration/test.bats index c7f133c..94237e2 100644 --- a/__tests__/integration/test.bats +++ b/__tests__/integration/test.bats @@ -198,6 +198,15 @@ teardown() { [[ "$output" == *"This contains a sentence which only exists in the ok-include-wildcard/project-b fixture."* ]] } +@test "builds a mkdocs site with hooks" { + cd ${fixturesDir}/ok-with-hook + assertSuccessMkdocs build + assertFileExists site/project-1/index.html + assertFileExists site/project-1/about.html + assertFileExists site/project-2/index.html + assertFileExists site/project-2/about.html +} + @test "fails if !include path is above current folder" { cd ${fixturesDir}/error-include-path-is-parent assertFailedMkdocs build diff --git a/__tests__/test-local.sh b/__tests__/test-local.sh index d09e759..e99dad1 100755 --- a/__tests__/test-local.sh +++ b/__tests__/test-local.sh @@ -2,8 +2,8 @@ # Lint via flake8 echo "Running flake8 linter -------->" -flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude=setup.py -flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude=setup.py +flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude=setup.py,venv,env,.venv,.env +flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude=setup.py,venv,env,.venv,.env # Running unit-tests python3 -m unittest diff --git a/mkdocs_monorepo_plugin/merger.py b/mkdocs_monorepo_plugin/merger.py index 5a1d318..89d3a8f 100644 --- a/mkdocs_monorepo_plugin/merger.py +++ b/mkdocs_monorepo_plugin/merger.py @@ -19,12 +19,16 @@ import os from os.path import join from pathlib import Path +import subprocess -from mkdocs.utils import warning_filter +from mkdocs.utils import warning_filter, yaml_load log = logging.getLogger(__name__) log.addFilter(warning_filter) + +TEMP_DOCS_DIR = "TEMP_DOCS_DIR" + # This collects the multiple docs/ folders and merges them together. @@ -33,11 +37,11 @@ def __init__(self, config): self.config = config self.root_docs_dir = config['docs_dir'] self.docs_dirs = list() - self.append('', self.root_docs_dir) + self.append('', self.root_docs_dir, getattr(config, "config_file_path", Path("mkdocs.yml"))) self.files_source_dir = dict() - def append(self, alias, docs_dir): - self.docs_dirs.append([alias, docs_dir]) + def append(self, alias, docs_dir, yaml_file): + self.docs_dirs.append([alias, docs_dir, yaml_file]) def merge(self): self.temp_docs_dir = TemporaryDirectory('', 'docs_') @@ -50,7 +54,9 @@ def merge(self): "Current registered site names in the monorepository: {}".format(', '.join(aliases))) raise SystemExit(1) - for alias, docs_dir in self.docs_dirs: + for alias, docs_dir, yaml_file in self.docs_dirs: + with open(yaml_file) as f: + config = yaml_load(f) source_dir = docs_dir if len(alias) == 0: dest_dir = self.temp_docs_dir.name @@ -60,6 +66,20 @@ def merge(self): if os.path.exists(source_dir): copy_tree(source_dir, dest_dir) + mono_docs_hook = config.get("mono_docs_hook", {}) + hook, python_path = mono_docs_hook.get("hook"), mono_docs_hook.get("python_path") + cwd = mono_docs_hook.get("cwd") + project_dir = Path(docs_dir).parent + if cwd and not cwd.is_absolute(): + cwd = project_dir / cwd + if mono_docs_hook and hook: + comp_proc = subprocess.run( + f"{python_path or 'python'} {' '.join(hook)}", + cwd=Path(cwd).resolve() if cwd else project_dir, + env={TEMP_DOCS_DIR: dest_dir, **os.environ.copy()}, + shell=True + ) + comp_proc.check_returncode() for file_abs_path in Path(source_dir).rglob('*.md'): file_abs_path = str(file_abs_path) # python 3.5 compatibility if os.path.isfile(file_abs_path): diff --git a/mkdocs_monorepo_plugin/plugin.py b/mkdocs_monorepo_plugin/plugin.py index 5774805..df085fe 100644 --- a/mkdocs_monorepo_plugin/plugin.py +++ b/mkdocs_monorepo_plugin/plugin.py @@ -48,7 +48,7 @@ def on_config(self, config): self.aliases = {} for alias, docs_dir, yaml_file in resolvedPaths: self.aliases[alias] = { 'docs_dir': docs_dir, 'yaml_file': yaml_file } - self.merger.append(alias, docs_dir) + self.merger.append(alias, docs_dir, yaml_file) new_docs_dir = self.merger.merge() # Update the docs_dir with our temporary one! diff --git a/setup.py b/setup.py index 06dc8dc..0244752 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setuptools.setup( name='mkdocs-monorepo-plugin', - version='1.0.5', + version='1.0.6', description='Plugin for adding monorepository support in Mkdocs.', long_description=""" This introduces support for the !include syntax in mkdocs.yml, allowing you to import additional Mkdocs navigation.