filters.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python
  2. # -*- coding=UTF-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: filter.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-06-15 00:39:29 (CST)
  9. # Last Update:星期四 2016-6-16 1:31:14 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from datetime import datetime
  14. from maple import redis_data
  15. from maple.settings import setting
  16. from maple.topic.models import Reply, Topic
  17. from maple.user.models import User
  18. from flask import Markup
  19. from misaka import Markdown, HtmlRenderer
  20. from pygments import highlight
  21. from pygments.formatters import HtmlFormatter
  22. from pygments.lexers import get_lexer_by_name
  23. from bleach import clean
  24. def safe_clean(text):
  25. tags = ['b', 'i', 'font', 'br', 'blockquote', 'a', 'div', 'ul', 'li', 'h2']
  26. attrs = {'*': ['style', 'id', 'class'], 'font': ['color'], 'a': ['href']}
  27. styles = ['color']
  28. return Markup(clean(text, tags=tags, attributes=attrs, styles=styles))
  29. class Filters(object):
  30. def safe_markdown(text):
  31. class HighlighterRenderer(HtmlRenderer):
  32. def blockcode(self, text, lang):
  33. lang = 'python'
  34. if not lang:
  35. return '\n<pre><code>{}</code></pre>\n'.format(text.strip(
  36. ))
  37. lexer = get_lexer_by_name(lang, stripall=True)
  38. formatter = HtmlFormatter()
  39. return highlight(text, lexer, formatter)
  40. renderer = HighlighterRenderer()
  41. md = Markdown(renderer, extensions=('fenced-code', ))
  42. return Markup(md(safe_clean(text)))
  43. # return Markup(md(text))
  44. def timesince(dt, default="just now"):
  45. now = datetime.now()
  46. diff = now - dt
  47. if diff.days > 10:
  48. return dt.strftime('%Y-%m-%d %H:%M')
  49. if diff.days <= 10 and diff.days > 0:
  50. periods = ((diff.days, "day", "days"), )
  51. if diff.days <= 0 and diff.seconds > 3600:
  52. periods = ((diff.seconds / 3600, "hour", "hours"), )
  53. if diff.seconds <= 3600 and diff.seconds > 90:
  54. periods = ((diff.seconds / 60, "minute", "minutes"), )
  55. if diff.seconds <= 90:
  56. return default
  57. # periods = ((diff.days / 365, "year", "years"),
  58. # (diff.days / 30, "month", "months"),
  59. # (diff.days / 7, "week", "weeks"),
  60. # (diff.days, "day", "days"),
  61. # (diff.seconds / 3600, "hour", "hours"),
  62. # (diff.seconds / 60, "minute", "minutes"),
  63. # (diff.seconds, "second", "seconds"), )
  64. for period, singular, plural in periods:
  65. if period:
  66. return "%d %s ago" % (period, singular if period == 1 else
  67. plural)
  68. return default
  69. def get_last_reply(uid):
  70. reply = Reply.query.join(Reply.topic).filter(Topic.id == uid).first()
  71. return reply
  72. def get_user_infor(name):
  73. user = User.query.filter(User.username == name).first()
  74. return user
  75. def get_read_count(id):
  76. read = redis_data.hget('topic:%s' % str(id), 'read')
  77. replies = redis_data.hget('topic:%s' % str(id), 'replies')
  78. if not read:
  79. read = 0
  80. else:
  81. read = int(read)
  82. if not replies:
  83. replies = 0
  84. else:
  85. replies = int(replies)
  86. return replies, read
  87. class Title(object):
  88. title = setting['title']
  89. picture = setting['picture']
  90. description = setting['description']