Skip to content

Commit

Permalink
Merge pull request getnikola#633 from punchagan/reading-time
Browse files Browse the repository at this point in the history
Add reading_time property to posts.
  • Loading branch information
ralsina committed Sep 5, 2013
2 parents 576cae1 + 066c732 commit 0604c73
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 0604c73

Please sign in to comment.