| 1 |
import os |
| 2 |
import re |
| 3 |
from pathlib import Path |
| 4 |
from typing import Any, Dict, Iterable, List, Sequence, Tuple, Union |
| 5 |
|
| 6 |
from docutils import nodes |
| 7 |
from docutils.nodes import Node, document |
| 8 |
from sphinx.application import Sphinx |
| 9 |
|
| 10 |
from docutils.parsers.rst import Directive |
| 11 |
|
| 12 |
# Do not require newer sphinx. EPEL packages build man pages with Sphinx 1.7.6. Refer: CDRIVER-4767 |
| 13 |
needs_sphinx = '1.7' |
| 14 |
author = 'MongoDB, Inc' |
| 15 |
|
| 16 |
# -- Options for HTML output ---------------------------------------------- |
| 17 |
|
| 18 |
smart_quotes = False |
| 19 |
html_show_sourcelink = False |
| 20 |
|
| 21 |
# Note: http://www.sphinx-doc.org/en/1.5.1/config.html#confval-html_copy_source |
| 22 |
# This will degrade the Javascript quicksearch if we ever use it. |
| 23 |
html_copy_source = False |
| 24 |
|
| 25 |
|
| 26 |
def _file_man_page_name(fpath: Path) -> Union[str, None]: |
| 27 |
"Given an rST file input, find the :man_page: frontmatter value, if present" |
| 28 |
lines = fpath.read_text().splitlines() |
| 29 |
for line in lines: |
| 30 |
mat = re.match(r':man_page:\s+(.+)', line) |
| 31 |
if not mat: |
| 32 |
continue |
| 33 |
return mat[1] |
| 34 |
|
| 35 |
|
| 36 |
def _collect_man(app: Sphinx): |
| 37 |
# Note: 'app' is partially-formed, as this is called from the Sphinx.__init__ |
| 38 |
docdir = Path(app.srcdir) |
| 39 |
# Find everything: |
| 40 |
children = docdir.rglob('*') |
| 41 |
# Find only regular files: |
| 42 |
files = filter(Path.is_file, children) |
| 43 |
# Find files that have a .rst extension: |
| 44 |
rst_files = (f for f in files if f.suffix == '.rst') |
| 45 |
# Pair each file with its :man_page: frontmatter, if present: |
| 46 |
with_man_name = ((f, _file_man_page_name(f)) for f in rst_files) |
| 47 |
# Filter out pages that do not have a :man_page: item:s |
| 48 |
pairs: Iterable[tuple[Path, str]] = ((f, m) for f, m in with_man_name if m) |
| 49 |
# Populate the man_pages: |
| 50 |
for filepath, man_name in pairs: |
| 51 |
# Generate the docname. |
| 52 |
relative_path = filepath.relative_to(app.srcdir) |
| 53 |
# The docname is relative to the source directory, and excludes the suffix. |
| 54 |
docname = str(relative_path.parent / filepath.stem) |
| 55 |
|
| 56 |
assert docname, filepath |
| 57 |
man_pages.append((docname, man_name, '', [author], 3)) |
| 58 |
|
| 59 |
|
| 60 |
# -- Options for manual page output --------------------------------------- |
| 61 |
|
| 62 |
# NOTE: This starts empty, but we populate it in `setup` in _collect_man() (see above) |
| 63 |
man_pages: List[Tuple[str, str, str, List[str], int]] = [] |
| 64 |
|
| 65 |
# If true, show URL addresses after external links. |
| 66 |
# |
| 67 |
# man_show_urls = False |
| 68 |
|
| 69 |
# -- Sphinx customization --------------------------------------- |
| 70 |
|
| 71 |
|
| 72 |
def add_ga_javascript(app: Sphinx, pagename: str, templatename: str, context: Dict[str, Any], doctree: document): |
| 73 |
if not app.env.config.analytics: |
| 74 |
return |
| 75 |
|
| 76 |
# Add google analytics. |
| 77 |
context['metatags'] = ( |
| 78 |
context.get('metatags', '') |
| 79 |
+ """ |
| 80 |
<!-- Google tag (gtag.js) --> |
| 81 |
<script async src="https://www.googletagmanager.com/gtag/js?id=G-56KD6L3MDX"></script> |
| 82 |
<script> |
| 83 |
window.dataLayer = window.dataLayer || []; |
| 84 |
function gtag(){dataLayer.push(arguments);} |
| 85 |
gtag('js', new Date()); |
| 86 |
|
| 87 |
gtag('config', 'G-56KD6L3MDX'); |
| 88 |
</script> |
| 89 |
""" |
| 90 |
) |
| 91 |
|
| 92 |
|
| 93 |
class VersionList(Directive): |
| 94 |
"""Custom directive to generate the version list from an environment variable""" |
| 95 |
|
| 96 |
option_spec = {} |
| 97 |
has_content = True |
| 98 |
|
| 99 |
def run(self) -> Sequence[Node]: |
| 100 |
if self.content[0] != 'libmongoc' and self.content[0] != 'libbson': |
| 101 |
print('versionlist must be libmongoc or libbson') |
| 102 |
return [] |
| 103 |
|
| 104 |
libname = self.content[0] |
| 105 |
env_name = libname.upper() + '_VERSION_LIST' |
| 106 |
if env_name not in os.environ: |
| 107 |
print(env_name + ' not set, not generating version list') |
| 108 |
return [] |
| 109 |
|
| 110 |
versions = os.environ[env_name].split(',') |
| 111 |
|
| 112 |
header = nodes.paragraph('', '') |
| 113 |
p = nodes.paragraph('', '') |
| 114 |
uri = 'https://www.mongoc.org/%s/%s/index.html' % (libname, versions[0]) |
| 115 |
p += nodes.reference('', 'Latest Release (%s)' % versions[0], internal=False, refuri=uri) |
| 116 |
header += p |
| 117 |
p = nodes.paragraph('', '') |
| 118 |
uri = 'https://s3.amazonaws.com/mciuploads/mongo-c-driver/docs/%s/latest/index.html' % (libname) |
| 119 |
p += nodes.reference('', 'Current Development (master)', internal=False, refuri=uri) |
| 120 |
header += p |
| 121 |
|
| 122 |
blist = nodes.bullet_list() |
| 123 |
for v in versions: |
| 124 |
item = nodes.list_item() |
| 125 |
p = nodes.paragraph('', '') |
| 126 |
uri = 'https://www.mongoc.org/%s/%s/index.html' % (libname, v) |
| 127 |
p += nodes.reference('', v, internal=False, refuri=uri) |
| 128 |
item += p |
| 129 |
blist += item |
| 130 |
return [header, blist] |
| 131 |
|
| 132 |
|
| 133 |
def mongoc_common_setup(app: Sphinx): |
| 134 |
_collect_man(app) |
| 135 |
app.connect('html-page-context', add_ga_javascript) |
| 136 |
# Run sphinx-build -D analytics=1 to enable Google Analytics. |
| 137 |
app.add_config_value('analytics', False, 'html') |
| 138 |
app.add_directive('versionlist', VersionList) |