• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""A few small helpers for working with Markdown."""
15
16def markdown_link(link_text, href):
17    """Creates a markdown link.
18
19    Args:
20      link_text: The text to display for the link.
21      href: The href for the link.
22
23    Returns:
24      A markdown link.
25    """
26    return "[" + link_text + "](" + href + ")"
27
28def xref_substitutions(match_text_patterns):
29    """Creates a dictionary of substitutions for use for linkification of text.
30
31    Example:
32    ```
33    # Produces a dictionary containing:
34    #   {
35    #     "foo": "[foo](http://foo.com)"
36    #     "bar": "[bar](http://bar.com)"
37    #   }
38    substitutions = xref_substitutions({
39        "foo": "http://foo.com",
40        "bar": "http://bar.com",
41    })
42    ```
43
44    Args:
45      match_text_patterns: A dictionary mapping string literals to the links they should point to.
46
47    Returns:
48      A dictionary of string literals mapped to their linkified substitutions.
49    """
50    return {
51        match_text: markdown_link(match_text, href)
52        for match_text, href in match_text_patterns.items()
53    }
54