Skip to content

Commit

Permalink
Add reading_time property to posts.
Browse files Browse the repository at this point in the history
The property returns a rough estimate of the reading time for the blog
post in minutes.  This code is inspired by
https://gist.github.com/zachleat/5792681.

Note: This commit does not make any changes to the templates
  • Loading branch information
punchagan committed Aug 30, 2013
1 parent a72ce97 commit 066c732
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions nikola/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def __init__(
self._template_name = template_name
self.is_two_file = True
self.hyphenate = hyphenate
self._reading_time = None

default_metadata = get_meta(self, file_metadata_regexp)

Expand Down Expand Up @@ -371,6 +372,17 @@ def text(self, lang=None, teaser_only=False, strip_html=False, really_absolute=F
data = content.text_content().strip() # No whitespace wanted.
return data

@property
def reading_time(self):
"""Reading time based on length of text.
"""
if self._reading_time is None:
text = self.text(strip_html=True)
words_per_minute = 180
words = len(text.split())
self._reading_time = int(round(words/words_per_minute)) or 1
return self._reading_time

def source_link(self, lang=None):
"""Return absolute link to the post's source."""
return "/" + self.destination_path(
Expand Down

0 comments on commit 066c732

Please sign in to comment.