Skip to content

Commit 641749e

Browse files
authored
Docs: refactor Python code for sphinx configuration and extensions (OSGeo#3761)
1 parent 3f43e13 commit 641749e

File tree

6 files changed

+251
-394
lines changed

6 files changed

+251
-394
lines changed

HOWTO-RELEASE

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ of the release.*
242242
3.2 Documentation
243243
-------------------------------------------------------------------------------
244244

245+
Edit the Sphinx configuration docs/source/conf.py with these changes:
246+
247+
- Set "version" to the major project version, e.g. "9.2"
248+
- Set "release" to the full project version, e.g. "9.2.1"
249+
- Set "data_version" to the PROJ-data version
250+
- Set "today_date = date(Y, M, D)" using the release date
251+
- set "github_version" to the maintenance branch label, e.g. "9.2"
252+
245253
HTML and PDF documentation is built using ReadTheDocs, which supports multiple
246254
versions. Versions are based on branch labels, e.g. "9.2". To modify settings,
247255
registered users must first log in. To add a version, go to
@@ -250,6 +258,3 @@ next to the maintenance branch label.
250258

251259
The latest maintenance branch should be set as the "Default version" found at
252260
https://readthedocs.org/dashboard/osgeo-proj/advanced/
253-
254-
Also edit docs/source/conf.py to change the active branch for documentation
255-
in the `github_version` variable.
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
from sphinx.domains.std import StandardDomain
22

3-
class MyStandardDomain(StandardDomain):
43

4+
class MyStandardDomain(StandardDomain):
55
def resolve_xref(self, env, fromdocname, builder, typ, target, node, contnode):
6-
if typ == 'program':
7-
typ = 'ref'
8-
return StandardDomain.resolve_xref(self, env, fromdocname, builder, typ, target, node, contnode)
6+
if typ == "program":
7+
typ = "ref"
8+
return StandardDomain.resolve_xref(
9+
self, env, fromdocname, builder, typ, target, node, contnode
10+
)
11+
912

1013
def setup(app):
1114
# Override the "program" role to be an alias of "ref"
1215
MyStandardDomain.roles["program"] = StandardDomain.roles["ref"]
1316
app.add_domain(MyStandardDomain, override=True)
14-
return { 'parallel_read_safe': True, 'parallel_write_safe': True }
17+
return {"parallel_read_safe": True, "parallel_write_safe": True}

docs/source/_extensions/redirects.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
1-
import os
1+
"""Sphinx doc extension for HTML redirects.
22
3-
# https://tech.signavio.com/2017/managing-sphinx-redirects
3+
Within conf.py, add a 'redirect_files' dict with alias/target pairs.
44
5+
Originally from https://tech.signavio.com/2017/managing-sphinx-redirects (dead)
6+
"""
7+
from pathlib import Path
58

6-
template="""<html>
9+
template = """\
10+
<html>
711
<head>
8-
<meta http-equiv="refresh" content="1; url=%s" />
12+
<meta http-equiv="refresh" content="1; url={target}" />
913
<script>
10-
window.location.href = "%s"
14+
window.location.href = "{target}"
1115
</script>
1216
</head>
13-
</html>"""
17+
</html>
18+
"""
1419

1520

16-
def gather_redirects():
17-
output = {}
18-
19-
output.update({ 'projjson.html' : 'usage/projjson.html' })
20-
return output
21-
22-
from shutil import copyfile
23-
# copy legacy redirects
24-
def copy_legacy_redirects(app, docname): # Sphinx expects two arguments
25-
if app.builder.name == 'html':
26-
for key in app.config.redirect_files:
27-
src = key
28-
tgt = app.config.redirect_files[key]
29-
html = template % (tgt, tgt)
30-
with open(os.path.join(app.outdir, src), 'wb') as f:
31-
f.write(html.encode('utf-8'))
32-
f.close()
33-
21+
def copy_legacy_redirects(app, docname): # Sphinx expects two arguments
22+
"""Copy legacy redirects."""
23+
if app.builder.name == "html":
24+
for alias, target in app.config.redirect_files.items():
25+
pth = Path(app.outdir) / alias
26+
pth.write_text(template.format(target=target))
3427

3528

3629
def setup(app):
37-
app.add_config_value('redirect_files', {}, 'html')
38-
app.connect('build-finished', copy_legacy_redirects)
39-
return { 'parallel_read_safe': False, 'parallel_write_safe': True }
30+
app.add_config_value("redirect_files", {}, "html")
31+
app.connect("build-finished", copy_legacy_redirects)
32+
return {"parallel_read_safe": False, "parallel_write_safe": True}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Sphinx doc extension for simple word replacements.
2+
3+
Within conf.py, add a 'replacement_pairs' dict with key/replacement pairs.
4+
5+
With inspiration from
6+
https://github.com/sphinx-doc/sphinx/issues/4054#issuecomment-329097229
7+
"""
8+
9+
10+
def replace_words(app, docname, source):
11+
result = source[0]
12+
for key, value in app.config.replacement_pairs.items():
13+
result = result.replace(key, value)
14+
source[0] = result
15+
16+
17+
def setup(app):
18+
app.add_config_value("replacement_pairs", {}, True)
19+
app.connect("source-read", replace_words)

docs/source/bibstyle.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@
2727
###############################################################################
2828

2929
import re
30+
31+
from pybtex.plugin import register_plugin
3032
from pybtex.style.formatting.unsrt import Style as UnsrtStyle
3133
from pybtex.style.labels.alpha import LabelStyle
32-
from pybtex.plugin import register_plugin
3334

3435

3536
class LinkLabelStyle(LabelStyle):
3637
"""Citation label used in text, and before each item in the
3738
References section"""
3839

39-
re_char_nums = re.compile(r'^[\d\w]+$')
40+
re_char_nums = re.compile(r"^[\d\w]+$")
4041

4142
def format_label(self, entry):
4243
"""Returns BibTeX key for label
@@ -47,23 +48,25 @@ def format_label(self, entry):
4748
label = entry.key
4849
if not self.re_char_nums.match(label):
4950
raise KeyError(
50-
'BibTeX key must contain only letters and numbers '
51-
'(found {0!r})'.format(label))
51+
"BibTeX key must contain only letters and numbers "
52+
"(found {!r})".format(label)
53+
)
5254
return label
5355

5456

5557
class CustomStyle(UnsrtStyle):
5658
"""Citation style in the References section"""
59+
5760
# TODO: Make more Harvard-like, i.e. year after name(s)
5861

59-
default_label_style = 'linklabel'
60-
default_name_style = 'lastfirst'
61-
default_sorting_style = 'author_year_title'
62+
default_label_style = "linklabel"
63+
default_name_style = "lastfirst"
64+
default_sorting_style = "author_year_title"
6265

6366
def __init__(self, **kwargs):
64-
kwargs['abbreviate_names'] = True
67+
kwargs["abbreviate_names"] = True
6568
UnsrtStyle.__init__(self, **kwargs)
6669

6770

68-
register_plugin('pybtex.style.labels', 'linklabel', LinkLabelStyle)
69-
register_plugin('pybtex.style.formatting', 'customstyle', CustomStyle)
71+
register_plugin("pybtex.style.labels", "linklabel", LinkLabelStyle)
72+
register_plugin("pybtex.style.formatting", "customstyle", CustomStyle)

0 commit comments

Comments
 (0)