diff --git a/conf.py b/conf.py index 62f58d75..5db4ae6a 100644 --- a/conf.py +++ b/conf.py @@ -3,6 +3,7 @@ from docutils import nodes from docutils.parsers.rst import roles from sphinx.deprecation import RemovedInSphinx90Warning +from sphinx.transforms import SphinxTransform warnings.filterwarnings("ignore", category=RemovedInSphinx90Warning) @@ -68,7 +69,48 @@ def _meta_type_role(name, rawtext, text, lineno, inliner, options=None, content= return [chip], [*messages, *ref_messages] +# The id docutils derives from "``url`` *(required)*" is url-required. +# Anchor a required key at its name alone, so the anchor stays when +# the key becomes optional. +class _AnchorWithoutRequiredMarker(SphinxTransform): + # before SphinxDomains (850), which records the label targets + default_priority = 800 + + def apply(self, **kwargs): + for section in self.document.findall(nodes.section): + title = section[0] + text = "".join( + child.astext() + for child in title.children + if not (isinstance(child, nodes.emphasis) and child.astext() == "(required)") + ) + anchor = nodes.make_id(text) + if not anchor or self.document.ids.get(anchor, section) is not section: + continue + if anchor in section["ids"]: + section["ids"].remove(anchor) + section["ids"].insert(0, anchor) + self.document.ids[anchor] = section + + +# A label before a heading becomes an empty span above the section, +# and :ref: links to that span (#date-type) instead of the section id +# the heading's own permalink uses (#date). Point the labels of +# sections at the section id. +def _link_labels_to_sections(app, doctree): + std = app.env.domains.standard_domain + docname = app.env.docname + for name, (labeldoc, labelid, sectname) in list(std.labels.items()): + node = doctree.ids.get(labelid) + if labeldoc != docname or not isinstance(node, nodes.section): + continue + std.labels[name] = labeldoc, node["ids"][0], sectname + std.anonlabels[name] = labeldoc, node["ids"][0] + + def setup(app): # priority > 500: after the theme handler that builds toctree_data app.connect("html-page-context", _flag_expandable_toc_entries, priority=501) + app.add_transform(_AnchorWithoutRequiredMarker) + app.connect("doctree-read", _link_labels_to_sections) app.add_role("meta-type", _meta_type_role) diff --git a/docs/standard/forks.rst b/docs/standard/forks.rst index 0f8973e8..0afba26e 100644 --- a/docs/standard/forks.rst +++ b/docs/standard/forks.rst @@ -1,98 +1,67 @@ -.. _forks-and-variants: - -Forks and variants -================== - -A fork serves one of two purposes, and ``publiccode.yml`` handles -each differently: technical forks and software variants. - -Technical forks (i.e. to publish patches) ------------------------------------------ - -A technical fork is made to work on the original code base or to send -improvements to its authors, with no goal of publishing an alternative -version of the software. On code hosting platforms like GitHub, -forking is almost always the first step of a contribution sent as a -pull request. - -These platforms publish a fork as an exact copy of the original -repository, ``publiccode.yml`` included, so parsers need a way to tell -a technical fork from the original codebase. - -Parsers -~~~~~~~ - -Parsers SHOULD identify a technical fork by noticing that the -top-level :ref:`url ` key does not point to the repository in which the -``publiccode.yml`` is found. - -Parsers MAY also identify a technical fork through metadata -exposed by the code hosting platform (e.g. GitHub marks forks -explicitly as forks). - -Authors -~~~~~~~ - -Authors of technical forks SHOULD NOT modify ``publiccode.yml`` in -any way. Specifically, they MUST NOT modify the top-level :ref:`url ` -key that MUST continue pointing to the original repository. - -No key marks a fork as technical, by design: authors of technical -forks do not need to know ``publiccode.yml`` or how to edit it, and -nothing is asked of them. - -Software variants ------------------ - -A software variant is a fork that is offered as an alternative to the -original upstream software. - -It contains modifications not yet part of the upstream version, like -more features, different dependencies or optimizations. - -By marking a fork as a variant, the author states that it holds a -complete and working set of modifications that other people might -find useful. - -Marking a fork as a variant says nothing about contributing upstream: -the author might still plan to, or be doing it already. Even a fork -that will eventually be merged upstream can be marked as a variant -meanwhile, so that others can find it and use it. - -.. _parsers-1: - -Parsers -~~~~~~~ - -Parsers SHOULD identify a variant by noticing that the top-level -:ref:`url ` key matches the repository in which the ``publiccode.yml`` is -found, **AND** a top-level :ref:`isBasedOn ` exists and points to a -different repository. - -Parsers SHOULD expect and analyze other differences in -``publiccode.yml`` between variants of the software. In particular, -``description/[lang]/features`` is designed to be compared across -variants to show their user-visible differences. - -.. _authors-1: - -Authors -~~~~~~~ - -Authors who publish a fork as a variant MUST at least: - -- Add a key :ref:`isBasedOn ` pointing to one or more - upstream repositories from which this variant is derived. -- Change the value for :ref:`url ` to point to the repository - holding the variant. -- Change the value for :ref:`organisation/name ` - to refer to themselves (the authors of the variant). -- Revisit the :ref:`maintenance ` section to refer to - the maintenance status of the variant. - -Moreover, authors SHOULD evaluate the following changes: - -- Add the features that differentiate the variant to the - ``description/[lang]/features`` key. Existing features SHOULD NOT be - edited or removed from this list unless they have been removed from - the variant, to allow parsers to easily compare feature lists. +.. _forks: + +Forks +===== + +When you fork a repository, you also copy its ``publiccode.yml``. +Update the file according to how you plan to use the fork. + +Forking to contribute +--------------------- + +If you fork to contribute changes to the original project, you +SHOULD leave ``publiccode.yml`` unchanged. You MUST leave +:ref:`url ` pointing to the original repository. Catalogs use +this URL to recognize the copy and avoid a duplicate listing. + +Forking to publish your own version +----------------------------------- + +If you publish a working version of your fork for others to use, +you MUST update ``publiccode.yml`` to describe it: + +- Point :ref:`url ` to the repository of the fork. +- Add :ref:`isBasedOn ` with the ``url`` of the + software it was forked from. +- Replace :ref:`organisation `, both ``uri`` and + ``name``, with the organisation publishing the fork, or remove the + section. +- Use :ref:`maintenance ` to describe who maintains + the fork and how. + +You SHOULD add the fork's features to +:ref:`description/[lang]/features `. Existing features SHOULD +NOT be edited or removed unless they have been removed from the fork. +Parsers SHOULD expect and analyze metadata differences between forks, +including comparing feature lists to show their user visible differences. + +.. tip:: + + You can publish a usable fork while contributing its changes + back to the original project. + +How parsers tell forks apart +---------------------------- + +Parsers SHOULD compare :ref:`url ` with the repository where +they found the file and check for :ref:`isBasedOn `: + +.. list-table:: + :header-rows: 1 + + * - ``url`` points to + - ``isBasedOn`` + - The repository holds + * - this repository + - absent + - the original software + * - this repository + - present + - a fork published as software of its own + * - another repository + - present or absent + - a copy of that repository, such as a fork made to contribute: + parsers SHOULD NOT list it as separate software + +Parsers MAY also use the fork information exposed by the code hosting +platform (e.g. GitHub marks forks explicitly as forks). diff --git a/docs/standard/index.rst b/docs/standard/index.rst index 658ba822..84f97aa8 100644 --- a/docs/standard/index.rst +++ b/docs/standard/index.rst @@ -90,10 +90,10 @@ prefer HTTPS. .. tip:: - Forking just to contribute upstream? Do not change ``url``: it - keeps pointing at the original repository, and parsers use the - mismatch to skip technical forks. Only a fork meant to become its - own project SHOULD change it. See :ref:`forks-and-variants`. + Forking just to contribute? Do not change ``url``: it keeps + pointing at the original repository, and catalogs use the mismatch + to skip the fork. Only a fork published as software of its own + SHOULD change it. See :ref:`forks`. ``softwareType`` *(required)* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -273,6 +273,8 @@ A URL to the hosted API documentation of the software. too. If there is a reference or test deployment, also consider an interactive interface (e.g. Swagger). +.. _features: + ``description/[lang]/features`` ''''''''''''''''''''''''''''''' @@ -396,6 +398,8 @@ You can name several owners in one English sentence or name a community, such as "Linus Torvalds and all Linux contributors". Omit the key if you cannot identify a main copyright owner. +.. _organisation: + ``organisation`` ~~~~~~~~~~~~~~~~ @@ -763,13 +767,13 @@ array of strings :caption: Example isBasedOn: - - https://github.com/example-org/firefax.git + - https://example.com/openfax.git -When the software is a variant or a fork, the :ref:`url ` of -the original projects, whether or not they have a ``publiccode.yml``. +When the software is a fork, the :ref:`url ` of the software it +was forked from, whether or not that has a ``publiccode.yml``. -The key marks the software as a variant descending from those -repositories: see :ref:`forks-and-variants`. +The key marks the software as a fork published as software of its +own: see :ref:`forks`. ``dependsOn`` ~~~~~~~~~~~~~ diff --git a/tests/test_anchors.py b/tests/test_anchors.py new file mode 100644 index 00000000..b3c41b6a --- /dev/null +++ b/tests/test_anchors.py @@ -0,0 +1,66 @@ +"""Cross references must use stable heading permalinks.""" + +import subprocess +import sys +import tempfile +import unittest +from html.parser import HTMLParser +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[1] + + +class Links(HTMLParser): + def __init__(self, html): + super().__init__() + self.sections = [] + self.links = [] + self.references = [] + self.current_link = None + self.feed(html) + + def handle_starttag(self, tag, attrs): + attrs = dict(attrs) + if tag == "section": + self.sections.append(attrs.get("id")) + if tag == "a": + self.current_link = attrs.get("href") + self.links.append((attrs.get("class", ""), self.current_link)) + if tag == "span" and "std-ref" in attrs.get("class", "").split(): + self.references.append(self.current_link) + + +class AnchorTests(unittest.TestCase): + def test_required_and_optional_keys_share_section_links(self): + for marker in (" *(required)*", ""): + with self.subTest(marker=marker), tempfile.TemporaryDirectory() as tmp: + source = Path(tmp) / "source" + output = Path(tmp) / "html" + source.mkdir() + heading = "``url``" + marker + (source / "index.rst").write_text( + "Anchor checks\n=============\n\n" + ":ref:`url-key` and :ref:`URL `.\n\n" + ":ref:`date-type` and :ref:`Date `.\n\n" + ".. _url-key:\n\n" + f"{heading}\n{'-' * len(heading)}\n\n" + "Repository URL.\n\n" + ".. _date-type:\n\n" + "Date\n----\n\nCalendar date.\n" + ) + result = subprocess.run( + [sys.executable, "-m", "sphinx", "-W", "--keep-going", + "-b", "html", "-c", str(ROOT), str(source), str(output)], + capture_output=True, text=True, + ) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + page = Links((output / "index.html").read_text()) + for anchor in ("url", "date"): + self.assertIn(anchor, page.sections) + self.assertIn(("headerlink", f"#{anchor}"), page.links) + self.assertEqual(page.references.count(f"#{anchor}"), 2) + + +if __name__ == "__main__": + unittest.main()