Skip to content

Commit 6874196

Browse files
authored
Merge pull request #721 from Fortran-FOSS-Programmers/exclude-dirs-pagetree
Automatically exclude Ford directories from `pages`
2 parents ee80707 + b7c25a0 commit 6874196

3 files changed

Lines changed: 60 additions & 6 deletions

File tree

ford/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ def main(proj_data: ProjectSettings, proj_docs: str):
479479
proj_data.copy_subdir,
480480
proj_data.output_dir,
481481
md,
482+
settings=proj_data,
482483
encoding=proj_data.encoding,
483484
progress=progress,
484485
)

ford/pagetree.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from textwrap import dedent
3131
from ford.console import warn
3232
from ford.utils import meta_preprocessor, ProgressBar
33-
from ford.settings import EntitySettings
33+
from ford.settings import EntitySettings, ProjectSettings
3434
from ford._markdown import MetaMarkdown
3535

3636

@@ -106,11 +106,25 @@ def __iter__(self):
106106
return iter(retlist)
107107

108108

109+
def is_excluded_dir(path: Path, settings: ProjectSettings) -> bool:
110+
"""Should `path` be excluded from the pagetree?"""
111+
paths_to_exclude: set[Path] = {
112+
dir
113+
for dir in [settings.graph_dir, settings.media_dir, settings.output_dir]
114+
if dir is not None
115+
}
116+
paths_to_exclude.update(settings.html_template_dir)
117+
paths_to_exclude.update(settings.src_dir)
118+
119+
return path in paths_to_exclude
120+
121+
109122
def get_page_tree(
110123
topdir: Path,
111124
proj_copy_subdir: Sequence[Path],
112125
output_dir: Path,
113126
md: MetaMarkdown,
127+
settings: ProjectSettings,
114128
progress: Optional[ProgressBar] = None,
115129
parent=None,
116130
encoding: str = "utf-8",
@@ -124,7 +138,11 @@ def get_page_tree(
124138
index_file = topdir / "index.md"
125139

126140
if not index_file.exists():
127-
warn(f"'{index_file}' does not exist")
141+
# Don't warn if this is a Ford input directory (media_dir, graph_dir, etc)
142+
if not is_excluded_dir(topdir, settings):
143+
warn(
144+
f"Skipping creating page for '{topdir}' becase it does not contain 'index.md'"
145+
)
128146
return None
129147

130148
if progress is not None:
@@ -164,7 +182,14 @@ def get_page_tree(
164182
continue
165183

166184
if subnode := get_page_tree(
167-
filename, proj_copy_subdir, output_dir, md, progress, node, encoding
185+
filename,
186+
proj_copy_subdir,
187+
output_dir,
188+
md,
189+
settings,
190+
progress,
191+
node,
192+
encoding,
168193
):
169194
node.subpages.append(subnode)
170195
elif filename.suffix == ".md":

test/test_pagetree.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import Path
2+
from ford.settings import ProjectSettings
13
from ford.pagetree import get_page_tree
24
from ford._markdown import MetaMarkdown
35

@@ -29,8 +31,9 @@ def test_footnotes_on_one_page(tmp_path):
2931
"""))
3032

3133
md = MetaMarkdown()
34+
settings = ProjectSettings()
3235
result_dir = tmp_path / "result"
33-
nodes = get_page_tree(tmp_path, result_dir, tmp_path / "doc", md)
36+
nodes = get_page_tree(tmp_path, result_dir, tmp_path / "doc", md, settings)
3437

3538
assert len(nodes.subpages) == 2
3639
assert "This is the footnote on page A" in nodes.subpages[0].contents
@@ -61,8 +64,9 @@ def test_footnotes_on_one_page_parse_failure(tmp_path):
6164
"""))
6265

6366
md = MetaMarkdown()
67+
settings = ProjectSettings()
6468
result_dir = tmp_path / "result"
65-
nodes = get_page_tree(tmp_path, result_dir, tmp_path / "doc", md)
69+
nodes = get_page_tree(tmp_path, result_dir, tmp_path / "doc", md, settings)
6670

6771
assert len(nodes.subpages) == 1
6872
assert "This is the footnote on page A" not in nodes.subpages[0].contents
@@ -88,7 +92,31 @@ def test_non_utf8_encoding(tmp_path):
8892
""").encode(encoding))
8993

9094
md = MetaMarkdown()
95+
settings = ProjectSettings()
9196
result_dir = tmp_path / "result"
92-
nodes = get_page_tree(tmp_path, result_dir, tmp_path / "doc", md, encoding=encoding)
97+
nodes = get_page_tree(
98+
tmp_path, result_dir, tmp_path / "doc", md, settings, encoding=encoding
99+
)
93100

94101
assert "本文档是" in nodes.contents
102+
103+
104+
def test_exclude_ford_dirs(tmp_path, capsys):
105+
"""Test that we exclude some user-settable directories"""
106+
107+
(tmp_path / "index.md").write_text("title: Index")
108+
109+
subdir = tmp_path / "subdir"
110+
subdir.mkdir(parents=True)
111+
112+
media_dir: Path = tmp_path / "media"
113+
media_dir.mkdir(parents=True)
114+
115+
md = MetaMarkdown()
116+
settings = ProjectSettings(media_dir=media_dir)
117+
result_dir = tmp_path / "result"
118+
get_page_tree(tmp_path, result_dir, tmp_path / "doc", md, settings)
119+
120+
captured = capsys.readouterr().out.replace("\n", " ").replace(" ", " ")
121+
assert str(media_dir) not in captured
122+
assert str(subdir) in captured

0 commit comments

Comments
 (0)