Personal wiki notebook (not under development)

Page_navigation.py 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from Tags import P, Span, A, Strong
  2. class Page_navigation( P ):
  3. def __init__( self, page_path, displayed_item_count, total_item_count, start, items_per_page, return_text = None ):
  4. if start is None or items_per_page is None:
  5. P.__init__( self )
  6. return
  7. if displayed_item_count == 1 and displayed_item_count < total_item_count:
  8. if not return_text:
  9. P.__init__( self )
  10. return
  11. P.__init__(
  12. self,
  13. Span(
  14. A(
  15. return_text,
  16. href = "%s" % page_path,
  17. ),
  18. ),
  19. )
  20. return
  21. if start == 0 and items_per_page >= total_item_count:
  22. P.__init__( self )
  23. return
  24. P.__init__(
  25. self,
  26. ( start > 0 ) and Span(
  27. A(
  28. u"previous",
  29. href = self.href( page_path, max( start - items_per_page, 0 ), items_per_page ),
  30. ),
  31. u" | ",
  32. ) or None,
  33. [ Span(
  34. ( start == page_start ) and Strong( unicode( page_number + 1 ) ) or A(
  35. Strong( unicode( page_number + 1 ) ),
  36. href = self.href( page_path, page_start, items_per_page ),
  37. ),
  38. ) for ( page_number, page_start ) in enumerate( range( 0, total_item_count, items_per_page ) ) ],
  39. ( start + items_per_page < total_item_count ) and Span(
  40. u" | ",
  41. A(
  42. u"next",
  43. href = self.href( page_path, min( start + items_per_page, total_item_count - 1 ), items_per_page ),
  44. ),
  45. ) or None,
  46. )
  47. @staticmethod
  48. def href( page_path, start, count ):
  49. # if start is zero, leave off start and count parameters and just use the defaults
  50. if start == 0:
  51. return page_path
  52. return u"%s?start=%d&count=%d" % ( page_path, start, count )