Skip to content

Commit

Permalink
Merge pull request #614 from Fortran-FOSS-Programmers/fix-broken-rela…
Browse files Browse the repository at this point in the history
…tive-links

Fix broken relative links
  • Loading branch information
ZedThree committed Jan 8, 2024
2 parents e76e1c4 + 3f2fe48 commit 809aa10
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 33 deletions.
2 changes: 1 addition & 1 deletion example/src/ford_test_module.fpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contains
#ifdef HAS_DECREMENT
subroutine decrement(x)
!! Decrease argument by one
!! Decrease argument by one, the opposite of [[increment]]
!!
!! Only publicly accessible if `"HAS_DECREMENT"` preprocessor
!! macro is defined
Expand Down
10 changes: 9 additions & 1 deletion ford/_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,15 @@ def convert(
and context is not None
and (url := context.get_url()) is not None
):
self.current_path = self.base_url / Path(url).parent
# This is a bit of a pain, but when making `[[ford]]` links, we want
# the link to be relative to the page the link is from. However, we
# use the entity description both on the entity page, and the list
# pages, so the same relative url needs to work from both. Luckily
# we know the directory layout, so just make a relative link from a
# (non-existent) sibling directory
self.current_path = (
self.base_url / Path(url).parent.parent / "non-existent dir"
)
else:
self.current_path = path
return super().convert(source)
Expand Down
2 changes: 1 addition & 1 deletion ford/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class ProjectSettings:
predocmark: str = ">"
predocmark_alt: str = "|"
preprocess: bool = True
preprocessor: str = "pcpp -D__GFORTRAN__"
preprocessor: str = "pcpp -D__GFORTRAN__ --passthru-comments"
print_creation_date: bool = False
privacy_policy_url: Optional[str] = None
proc_internals: bool = False
Expand Down
2 changes: 1 addition & 1 deletion ford/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
</li>
{% elif project.programs|length == 1 %}
<li class="nav-item">
<a class="nav-link" href="{{ project.programs[0].get_url() }}">Program</a>
<a class="nav-link" href="{{ project_url }}/{{ project.programs[0].get_url() }}">Program</a>
</li>
{% endif %}
{% if privacy_policy_url %}
Expand Down
8 changes: 4 additions & 4 deletions test/test_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
("https://github.com/fortran-lang/stdlib", "stdlib/API-doc-FORD-file.md", ""),
(
"https://github.com/jacobwilliams/Fortran-Astrodynamics-Toolkit",
"Fortran-Astrodynamics-Toolkit/fortran-astrodynamics-toolkit.md",
"Fortran-Astrodynamics-Toolkit/ford.md",
"",
),
(
"https://github.com/jacobwilliams/bspline-fortran",
"bspline-fortran/bspline-fortran.md",
"bspline-fortran/ford.md",
"",
),
("https://github.com/jacobwilliams/dvode", "dvode/ford.md", ""),
Expand All @@ -49,10 +49,10 @@
),
(
"https://github.com/jacobwilliams/pyplot-fortran",
"pyplot-fortran/pyplot-fortran.md",
"pyplot-fortran/ford.md",
"",
),
("https://github.com/jacobwilliams/quadpack", "quadpack/quadpack.md", ""),
("https://github.com/jacobwilliams/quadpack", "quadpack/ford.md", ""),
("https://github.com/szaghi/FLAP", "FLAP/doc/main_page.md", ""),
("https://github.com/szaghi/FiNeR", "FiNeR/doc/main_page.md", ""),
("https://github.com/szaghi/VTKFortran", "VTKFortran/doc/main_page.md", ""),
Expand Down
45 changes: 20 additions & 25 deletions test/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,14 +1082,14 @@ def test_make_links(copy_fortran_file):
project.markdown(md)

expected_links = {
"a": "module/a.html",
"b": "type/b.html",
"c": "type/b.html#variable-c",
"d": "proc/d.html",
"e": "type/b.html#boundprocedure-e",
"f": "proc/f.html",
"g": "module/a.html#variable-g",
"h": "program/h.html",
"a": "../module/a.html",
"b": "../type/b.html",
"c": "../type/b.html#variable-c",
"d": "../proc/d.html",
"e": "../type/b.html#boundprocedure-e",
"f": "../proc/f.html",
"g": "../module/a.html#variable-g",
"h": "../program/h.html",
}

for item in chain(
Expand All @@ -1100,12 +1100,8 @@ def test_make_links(copy_fortran_file):
):
docstring = BeautifulSoup(item.doc, features="html.parser")
link_locations = {a.string: a.get("href", None) for a in docstring("a")}
base_dir = pathlib.Path(item.get_url()).parent
expected_links_rel = {
k: relpath(v, base_dir) for k, v in expected_links.items()
}

assert link_locations == expected_links_rel, (item, item.name)
assert link_locations == expected_links, (item, item.name)


def test_link_with_context(copy_fortran_file):
Expand Down Expand Up @@ -1138,16 +1134,16 @@ def test_link_with_context(copy_fortran_file):
project.markdown(md)

expected_links = {
"a": "module/a.html",
"b": "type/b.html",
"c": "type/b.html#variable-c",
"d": "proc/d.html",
"e": "type/b.html#boundprocedure-e",
"f": "proc/d.html#proc-f",
"g": "module/a.html#variable-g",
"h": "program/h.html",
"i": "proc/d.html#variable-i",
"x": "proc/d.html#variable-x",
"a": "../module/a.html",
"b": "../type/b.html",
"c": "../type/b.html#variable-c",
"d": "../proc/d.html",
"e": "../type/b.html#boundprocedure-e",
"f": "../proc/d.html#proc-f",
"g": "../module/a.html#variable-g",
"h": "../program/h.html",
"i": "../proc/d.html#variable-i",
"x": "../proc/d.html#variable-x",
}

for item in chain(
Expand All @@ -1160,8 +1156,7 @@ def test_link_with_context(copy_fortran_file):

for link in docstring("a"):
assert link.string in expected_links, (item, item.name, link)
base_dir = pathlib.Path(item.get_url()).parent
expected_link = relpath(expected_links[link.string], base_dir)
expected_link = expected_links[link.string]

assert link.get("href", None) == expected_link, (
item,
Expand Down
17 changes: 17 additions & 0 deletions test/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import ford.reader as reader
from ford.reader import _contains_unterminated_string
from ford.settings import ProjectSettings

RE_WHITE = re.compile(r"\s+")

Expand Down Expand Up @@ -204,3 +205,19 @@ def test_preprocessor_warning(copy_fortran_file):
)
)
assert "foo" in lines


def test_preprocessor_leaves_urls(copy_fortran_file):
"""Check preprocessor leaves urls, issue #611"""

data = """\
!! http://www.example.com
module foo
end
"""
filename = copy_fortran_file(data, ".F90")
preprocessor = ProjectSettings().preprocessor.split()
lines = "\n".join(
list(reader.FortranReader(str(filename), preprocessor=preprocessor))
)
assert "http://www.example.com" in lines

0 comments on commit 809aa10

Please sign in to comment.