Skip to content

Commit fa33005

Browse files
committed
chore: Code clenaup
1 parent 2c4701a commit fa33005

3 files changed

Lines changed: 25 additions & 29 deletions

File tree

docs/usage.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Parameters:
8686
``paths``
8787
Equivalent to :rst:dir:`recentupdate:paths`.
8888

89-
``self``
89+
``self_only``
9090
Equivalent to :rst:dir:`recentupdate:self`.
9191

9292
``group_by``

src/sphinxnotes/recentupdate/__init__.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
logger = logging.getLogger(__name__)
4444

4545

46-
CURRENT_REPO: Repo
47-
4846
@dataclass
4947
class Revision:
5048
#: Git commit message, split by lines
@@ -206,11 +204,12 @@ def path2doc(repo: Repo, env: BuildEnvironment, blob_path: str) -> str | None:
206204
def collect_revisions(
207205
repo: Repo,
208206
env: BuildEnvironment,
209-
count: int,
207+
count: int | None,
210208
paths: list[str],
211-
group_by: str = '',
209+
group_by: str | None,
212210
) -> list[Revision]:
213211
"""Collect recent revisions from git, optionally grouped by time period."""
212+
repo = CURRENT_REPO
214213
count = count or env.config.recentupdate_count
215214
group_by = group_by or env.config.recentupdate_group_by
216215

@@ -231,44 +230,42 @@ def collect_revisions(
231230

232231
return revs
233232

234-
235233
class RecentUpdateDirective(BaseContextDirective):
236234
"""Directive for displaying recent document updates."""
237235

236+
@staticmethod
237+
def _group_by_choice(arg: str):
238+
return directives.choice(arg, GROUP_BY_CHOICES)
239+
238240
has_content = True
239241
required_arguments = 0
240242
optional_arguments = 1
241243
option_spec = {
242244
'self': directives.flag,
243245
'paths': directives.unchanged,
244-
'group-by': directives.unchanged,
246+
'group-by': _group_by_choice,
245247
}
246248

247249
def current_context(self) -> dict:
248250
repo = CURRENT_REPO
249251

250-
count = (
251-
int(self.arguments[0])
252-
if self.arguments
253-
else self.env.config.recentupdate_count
254-
)
252+
count = int(self.arguments[0]) if self.arguments else None
255253

256-
current_doc = 'self' in self.options
257-
if current_doc:
254+
if 'self' in self.options:
258255
docpath = self.env.doc2path(self.env.docname)
259256
paths = [path.relpath(docpath, repo.working_dir)]
260257
elif 'paths' in self.options:
261258
paths = [p.strip() for p in self.options['paths'].splitlines() if p.strip()]
262259
else:
263-
paths = ['.']
260+
paths = []
264261

265-
group_by = self.options.get('group-by', '')
262+
group_by = self.options.get('group-by')
266263

267264
revs = collect_revisions(repo, self.env, count, paths, group_by)
268265
return {'revisions': revs}
269266

270267
def current_template(self) -> Template:
271-
text = '\n'.join(self.content) if self.has_content and self.content else ''
268+
text = '\n'.join(self.content)
272269
if not text.strip():
273270
text = self.env.config.recentupdate_template
274271
return Template(text, phase=Phase.Parsing)
@@ -282,22 +279,22 @@ class RecentUpdateExtraContext(ExtraContext):
282279
def generate(
283280
self,
284281
req: ExtraContextRequest,
285-
count: int = 0,
286-
paths: list[str] = [
287-
'.',
288-
],
289-
self_doc: bool = False,
290-
group_by: str = '',
282+
count: int | None = None,
283+
paths: list[str] = [],
284+
group_by: str | None = None,
285+
self_only: bool = False,
291286
) -> Any:
292287
repo = CURRENT_REPO
293-
if self_doc:
288+
289+
if self_only:
294290
docpath = req.env.doc2path(req.env.docname)
295-
repo_path = path.relpath(docpath, repo.working_dir)
296-
paths = [repo_path]
291+
paths = [path.relpath(docpath, repo.working_dir)]
297292

298293
return collect_revisions(repo, req.env, count, paths, group_by)
299294

300295

296+
CURRENT_REPO: Repo
297+
GROUP_BY_CHOICES = ['day', 'month', 'year']
301298
DEFAULT_TEMPLATE = dedent("""\
302299
{% for r in revisions %}
303300
``👤 {{ r.author }}`` @ ``📅 {{ r.date.strftime('%Y-%m-%d') }}``
@@ -317,7 +314,6 @@ def generate(
317314
{% endfor %}
318315
""")
319316

320-
321317
def setup(app: Sphinx):
322318
meta.pre_setup(app)
323319

@@ -334,7 +330,7 @@ def setup(app: Sphinx):
334330
app.add_config_value('recentupdate_count', 5, 'env', types=int)
335331
app.add_config_value('recentupdate_template', DEFAULT_TEMPLATE, 'env', types=str)
336332
app.add_config_value(
337-
'recentupdate_group_by', None, 'env', types=ENUM(None, 'day', 'month', 'year')
333+
'recentupdate_group_by', None, 'env', types=ENUM(None, *GROUP_BY_CHOICES)
338334
)
339335

340336
return meta.post_setup(app)

tests/roots/test-recentupdate-current-doc/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Recentupdate Current Doc Test
44
.. data.render::
55
:debug:
66
7-
{% for r in load_extra('recentupdate', count=10, self_doc=True) %}
7+
{% for r in load_extra('recentupdate', count=10, self_only=True) %}
88
Commit: {{ r.message[0] }}
99
{% endfor %}
1010

0 commit comments

Comments
 (0)