4343logger = logging .getLogger (__name__ )
4444
4545
46- CURRENT_REPO : Repo
47-
4846@dataclass
4947class Revision :
5048 #: Git commit message, split by lines
@@ -206,11 +204,12 @@ def path2doc(repo: Repo, env: BuildEnvironment, blob_path: str) -> str | None:
206204def 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-
235233class 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' ]
301298DEFAULT_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-
321317def 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 )
0 commit comments