37
37
38
38
from nikola import utils
39
39
from nikola .plugin_categories import RestExtension
40
+ from nikola .packages .datecond import date_in_range
40
41
41
42
# WARNING: the directive name is post-list
42
43
# (with a DASH instead of an UNDERSCORE)
@@ -86,10 +87,19 @@ class PostList(Directive):
86
87
Reverse the order of the post-list.
87
88
Defaults is to not reverse the order of posts.
88
89
89
- ``sort``: string
90
+ ``sort`` : string
90
91
Sort post list by one of each post's attributes, usually ``title`` or a
91
92
custom ``priority``. Defaults to None (chronological sorting).
92
93
94
+ ``date`` : string
95
+ Show posts that match date range specified by this option. Format:
96
+
97
+ * comma-separated clauses (AND)
98
+ * clause: attribute comparison_operator value (spaces optional)
99
+ * attribute: year, month, day, hour, month, second, weekday, isoweekday; or empty for full datetime
100
+ * comparison_operator: == != <= >= < >
101
+ * value: integer or dateutil-compatible date input
102
+
93
103
``tags`` : string [, string...]
94
104
Filter posts to show only posts having at least one of the ``tags``.
95
105
Defaults to None.
@@ -136,6 +146,7 @@ class PostList(Directive):
136
146
'lang' : directives .unchanged ,
137
147
'template' : directives .path ,
138
148
'id' : directives .unchanged ,
149
+ 'date' : directives .unchanged ,
139
150
}
140
151
141
152
def run (self ):
@@ -154,17 +165,21 @@ def run(self):
154
165
lang = self .options .get ('lang' , utils .LocaleBorg ().current_lang )
155
166
template = self .options .get ('template' , 'post_list_directive.tmpl' )
156
167
sort = self .options .get ('sort' )
168
+ date = self .options .get ('date' )
157
169
158
170
output = _do_post_list (start , stop , reverse , tags , categories , slugs , post_type ,
159
- show_all , lang , template , sort , state = self .state , site = self .site )
171
+ show_all , lang , template , sort , state = self .state , site = self .site , date = date )
160
172
self .state .document .settings .record_dependencies .add ("####MAGIC####TIMELINE" )
161
- return [nodes .raw ('' , output , format = 'html' )]
173
+ if output :
174
+ return [nodes .raw ('' , output , format = 'html' )]
175
+ else :
176
+ return []
162
177
163
178
164
179
def _do_post_list (start = None , stop = None , reverse = False , tags = None , categories = None ,
165
180
slugs = None , post_type = 'post' , show_all = False , lang = None ,
166
181
template = 'post_list_directive.tmpl' , sort = None , id = None ,
167
- data = None , state = None , site = None ):
182
+ data = None , state = None , site = None , date = None ):
168
183
if lang is None :
169
184
lang = utils .LocaleBorg ().current_lang
170
185
if site .invariant : # for testing purposes
@@ -213,6 +228,9 @@ def _do_post_list(start=None, stop=None, reverse=False, tags=None, categories=No
213
228
if sort :
214
229
filtered_timeline = natsort .natsorted (filtered_timeline , key = lambda post : post .meta [lang ][sort ], alg = natsort .ns .F | natsort .ns .IC )
215
230
231
+ if date :
232
+ filtered_timeline = [p for p in filtered_timeline if date_in_range (date , p .date )]
233
+
216
234
for post in filtered_timeline [start :stop :step ]:
217
235
if slugs :
218
236
cont = True
0 commit comments