Skip to content

Commit b302247

Browse files
SilverRainZMiMoCode
andauthored
feat: Add recentupdate directive using BaseContextDirective (#9)
Co-authored-by: MiMoCode <mimo@xiaomi.com>
1 parent ee1c5b3 commit b302247

6 files changed

Lines changed: 239 additions & 105 deletions

File tree

docs/changelog.rst

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,22 @@ Version 2.x
2727
- The template feature is now provided by
2828
:external+render:doc:`sphinxnotes-render <index>`. It offers richer features;
2929
Please see :external+render:doc:`tmpl`
30-
- The extension now provides a ``recentupdate`` extra context for replacing
31-
the ``.. recentupdate::`` directive. See :doc:`usage` for more details
30+
- The :rst:dir:`recentupdate`` directive supports more options:
31+
32+
- New ``:self:`` option: show only revisions that modified the
33+
current document
34+
- New ``:paths:`` option: Specifiy pathspecs to filter file changes
35+
- New ``:group-by:`` option: group revisions by time period (day/month/year)
36+
37+
- Provide a ``recentupdate`` extra context for use within
38+
``sphinxnotes-render`` compatible templates
39+
- New confval: ``recentupdate_group_by``
3240

3341
BREAKING CHANGES:
3442

35-
- Drop the ``.. recentupdate::`` directive
36-
- Drop the ``strftime`` and ``roles`` filters
37-
- Drop the ``recentupdate_date_format``, ``recentupdate_template``, and
38-
``recentupdate_exclude_path`` confvals
43+
- Drop the ``strftime`` filters, use ``datetime.strfime`` instead
44+
- The ``roles`` filter is now provided by ``sphinxnotes-render``
45+
- Drop the ``recentupdate_exclude_path``, ``recentupdate_date_format`` confval
3946
- The members of :py:class:`~sphinxnotes.recentupdate.Revision` are renamed
4047

4148
Version 1.x

docs/conf.rst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ Configuration
44

55
The extension provides the following configuration:
66

7-
.. autoconfval:: recentupdate_exclude_commit
8-
9-
A list of commit message pattern that should be excluded when looking for file changes.
10-
117
.. autoconfval:: recentupdate_count
128

139
Number of recent revisions to return by default when calling
1410
``load_extra('recentupdate')`` without an explicit ``count`` parameter.
1511

12+
.. autoconfval:: recentupdate_template
13+
14+
Default Jinja2 template for the :rst:dir:`recentupdate` directive.
15+
Used when the directive has no body content.
16+
The template context contains ``revisions``, a list of
17+
:py:class:`~sphinxnotes.recentupdate.Revision` objects.
18+
19+
.. autoconfval:: recentupdate_exclude_commit
20+
21+
A list of commit message pattern that should be excluded when looking for file changes.
22+
1623
.. autoconfval:: recentupdate_group_by
1724

1825
Group revisions by time period. When set, revisions are grouped by

docs/index.rst

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ Introduction
2929
3030
Get the Sphinx document update information from Git repository.
3131

32-
This extension integrates with :external+render:doc:`sphinxnotes-render <index>`
33-
by providing an extra context ``recentupdate``. The recent document update
34-
information is read from a Git_ repository. You can customize the presentation
35-
via ``data.render`` template.
32+
This extension provides a :rst:dir:`recentupdate` directive that displays
33+
recent document update information read from a Git_ repository. It also
34+
integrates with :external+render:doc:`sphinxnotes-render <index>` by
35+
providing an extra context for use in render templates.
3636

3737
.. _Git: https://git-scm.com/
3838

@@ -71,20 +71,12 @@ Then, add the extension name to ``extensions`` configuration item in your
7171

7272
.. ADDITIONAL CONTENT START
7373
74-
Now you can use the :rst:dir:`data.render` directive (provided by
75-
``sphinxnotes.render.ext``) with ``recentupdate`` extra context to render
76-
a revision list:
74+
Now you can use the :rst:dir:`recentupdate` directive to render a revision list
75+
of the our documentation.
7776

7877
.. example::
7978

80-
.. data.render::
81-
82-
The most recent 3 commits:
83-
84-
{% for r in load_extra('recentupdate', 3) %}
85-
``{{ r.date.strftime('%Y-%m-%d') }}``
86-
{{ r.message[0] }}
87-
{% endfor %}
79+
.. recentupdate:: 3
8880

8981
Please refer to :doc:`usage` for more details.
9082

docs/usage.rst

Lines changed: 92 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,54 @@
22
Usage
33
=====
44

5-
The extension provides an extra context ``recentupdate`` for `sphinxnotes-render`_.
6-
Extra context can be load by the :external+render:term:`load_extra` function in
7-
a Jinja template. User can easily render a Jinja template via :rst:dir:`data.render`
8-
directive.
5+
The :rst:dir:`recentupdate` directive is the recommended way to display recent
6+
document updates. For cases where you need recent update information inside a
7+
`sphinxnotes-render`_ compatible template (e.g. alongside other extra contexts),
8+
use the ``recentupdate`` extra context instead.
99

10-
``recentupdate``
11-
================
10+
The ``recentupdate`` Directive
11+
==============================
1212

13-
When calling ``load_extra('recentupdate', **kwargs)`` in the template, the
14-
following parameters are available:
13+
.. rst:directive:: .. recentupdate:: [count]
1514
16-
``count``
17-
Number of recent revisions to return (default from :confval:`recentupdate_count`).
15+
Display recent document updates from Git.
1816

19-
``paths``
20-
A list of git pathspecs (:manpage:`gitglossary(7)`) to filter file changes
21-
(default ``['.']``).
22-
See also :example:`Recent Updates of Custom Path`.
17+
The optional ``count`` is the number of recent revisions to display.
18+
Defaults to :confval:`recentupdate_count`.
2319

24-
``current_doc``
25-
If ``True``, only return revisions that modified the current document
26-
(default ``False``).
20+
.. rst:directive:option:: self
21+
:type: flag
2722
28-
.. note::
23+
Only show revisions that modified the current document.
24+
Mutually exclusive with :rst:dir:`recentupdate:paths`.
2925
30-
When enabled, ``paths`` is overridden with a pathspec matching the current
31-
document. So ``paths`` and ``current_doc`` are mutually exclusive.
26+
See also :example:`Recent Updates to Current Document`.
3227
33-
See also :example:`Recent Updates to Current Document`.
28+
.. rst:directive:option:: paths
29+
:type: lines of str
3430
35-
.. role:: py(code)
36-
:language: Python
31+
Git pathspecs (:manpage:`gitglossary(7)`) to filter file changes,
32+
one per line. Defaults to ``.``.
3733
38-
``group_by``
39-
Group revisions by time period. Revisions are grouped by UTC time
40-
period and author.
34+
See also :example:`Recent Updates of Custom Path`.
35+
36+
.. rst:directive:option:: group-by
37+
:type: str
4138
42-
Default from :confval:`recentupdate_group_by`, Available values:
43-
:data.render:`{{ load_extra('env').config.values['recentupdate_group_by'].valid_types | autoconfval_types | join(', ') }}`.
39+
.. role:: py(code)
40+
:language: Python
4441
45-
See also :example:`Grouped Recent Updates`.
42+
Group revisions by time period.
43+
Available values:
44+
:data.render:`{{ load_extra('env').config.values['recentupdate_group_by'].valid_types | autoconfval_types | join(', ') }}`.
45+
Defaults to :confval:`recentupdate_group_by`.
4646
47-
Each item returned is a :py:class:`~sphinxnotes.recentupdate.Revision` object:
47+
See also :example:`Grouped Recent Updates`.
48+
49+
The directive body is a Jinja2 template. When empty,
50+
:confval:`recentupdate_template` is used. The template context contains
51+
a ``{{ revisions }}`` variable, it is a list of
52+
:py:class:`~sphinxnotes.recentupdate.Revision` objects.
4853

4954
.. autoclass:: sphinxnotes.recentupdate.Revision
5055

@@ -55,6 +60,39 @@ Each item returned is a :py:class:`~sphinxnotes.recentupdate.Revision` object:
5560
.. autoattribute:: changed_docs
5661
.. autoattribute:: removed_docs
5762

63+
This is a basic example using the default template:
64+
65+
.. example::
66+
67+
.. recentupdate::
68+
69+
The ``recentupdate`` Extra Context
70+
==================================
71+
72+
.. tip::
73+
74+
The extra context is for use within `sphinxnotes-render`_ compatible templates.
75+
In most cases, prefer the :rst:dir:`recentupdate` directive instead.
76+
77+
The ``recentupdate`` extra context can be loaded via
78+
:external+render:term:`load_extra` in a Jinja template
79+
(e.g. via :rst:dir:`data.render`):
80+
81+
Parameters:
82+
83+
``count``
84+
Equivalent to the argument of :rst:dir:`recentupdate`.
85+
86+
``paths``
87+
Equivalent to :rst:dir:`recentupdate:paths`.
88+
89+
``self_only``
90+
Equivalent to :rst:dir:`recentupdate:self`.
91+
92+
``group_by``
93+
Equivalent to :rst:dir:`recentupdate:group-by`.
94+
95+
5896
This is a basic example:
5997

6098
.. example::
@@ -67,15 +105,18 @@ This is a basic example:
67105
{{ r.message[0] }}
68106
{% endfor %}
69107
108+
.. note:: To Use the ``data.render`` directive, you need to add
109+
``sphinxnotes.render.ext`` to your Sphinx extension list.
110+
70111
Examples
71112
========
72113

73-
.. example:: Show Which Files Updated
114+
.. example:: Show Which Documents Updated
74115

75-
.. data.render::
116+
.. recentupdate::
76117

77-
{% for r in load_extra('recentupdate', count=5) %}
78-
``📅 {{ r.date.strftime('%Y-%m-%d') }}``
118+
{% for r in revisions %}
119+
``{{ r.date.strftime('%Y-%m-%d') }}``
79120
{% if r.changed_docs -%}
80121
:Modified: {{ r.changed_docs | roles("doc") | join(", ") }}
81122
{% endif %}
@@ -87,41 +128,46 @@ Examples
87128
{% endif %}
88129
{% endfor %}
89130

90-
The aboved :external+render:term:`roles` filter is also provided by
91-
`sphinxnotes-render`_.
131+
The aboved :external+render:term:`roles` filter is also provided by
132+
`sphinxnotes-render`_.
133+
92134

93135
.. example:: Recent Updates of Custom Path
94136

95-
.. data.render::
137+
Recent changes of the :doc:`changelog`:
96138

97-
Recent changes of the :doc:`changelog`:
139+
.. recentupdate::
140+
:paths: docs/changelog.rst
98141

99-
{% for r in load_extra('recentupdate', count=5, paths=['docs/changelog.rst']) %}
142+
{% for r in revisions %}
100143
``{{ r.date.strftime('%Y-%m-%d') }}`` — {{ r.message[0] }}
101144
{% endfor %}
102145

103146
.. example:: Recent Updates to Current Document
104147

105-
.. data.render::
148+
Recent changes to this document:
106149

107-
Recent changes to this document:
150+
.. recentupdate::
151+
:self:
108152

109-
{% for r in load_extra('recentupdate', count=5, current_doc=True) %}
153+
{% for r in revisions %}
110154
``{{ r.date.strftime('%Y-%m-%d') }}`` — {{ r.message[0] }}
111155
{% endfor %}
112156

113157
.. example:: Grouped Recent Updates
114158

115-
.. data.render::
159+
Recent updates grouped by month:
116160

117-
Recent updates grouped by month:
161+
.. recentupdate:: 3
162+
:group-by: month
118163

119-
{% for r in load_extra('recentupdate', count=5, group_by='month') %}
164+
{% for r in revisions %}
120165
``📅 {{ r.date.strftime('%Y-%m') }}``
121166
::
122-
{% for msg in r.message %}
167+
{% for msg in r.message[:20] %}
123168
{{ msg }}
124169
{%- endfor %}
170+
...
125171
{% endfor %}
126172

127173
``sphinxnotes-render``

0 commit comments

Comments
 (0)