jinja.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: jinja.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-11-07 21:00:32 (CST)
  9. # Last Update: Wednesday 2019-05-08 16:26:18 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from datetime import datetime
  14. from forums import default
  15. from bleach import clean
  16. from flask import Markup, g
  17. from flask_babel import format_datetime
  18. from misaka import HtmlRenderer, Markdown
  19. def safe_clean(text):
  20. tags = ['b', 'i', 'font', 'br', 'blockquote', 'div', 'h2', 'a', 'p']
  21. attrs = {'*': ['style', 'id', 'class'], 'font': ['color'], 'a': ['href']}
  22. styles = ['color']
  23. return Markup(clean(text, tags=tags, attributes=attrs, styles=styles))
  24. def markdown(text, clean=True):
  25. renderer = HtmlRenderer()
  26. md = Markdown(renderer, extensions=('fenced-code', ))
  27. if clean:
  28. return Markup(safe_clean(md(text)))
  29. return Markup(md(text))
  30. def timesince(dt, default="just now"):
  31. now = datetime.utcnow()
  32. diff = now - dt
  33. if diff.days > 10:
  34. return format_datetime(dt, 'Y-M-d H:m')
  35. elif diff.days <= 10 and diff.days > 0:
  36. periods = ((diff.days, "day", "days"), )
  37. elif diff.days <= 0 and diff.seconds > 3600:
  38. periods = ((diff.seconds / 3600, "hour", "hours"), )
  39. elif diff.seconds <= 3600 and diff.seconds > 90:
  40. periods = ((diff.seconds / 60, "minute", "minutes"), )
  41. else:
  42. return default
  43. for period, singular, plural in periods:
  44. if period:
  45. return "%d %s ago" % (period, singular if period == 1 else plural)
  46. return default
  47. def show_time():
  48. if g.user.is_authenticated:
  49. return 'LOCALE:' + format_datetime(datetime.utcnow())
  50. return 'UTC:' + format_datetime(datetime.utcnow())
  51. def hot_tags():
  52. from forums.api.tag.db import Tags
  53. tags = Tags.query.limit(9).all()
  54. return tags
  55. def recent_tags():
  56. from forums.api.tag.db import Tags
  57. tags = Tags.query.limit(12).all()
  58. return tags
  59. def forums_count():
  60. from forums.extension import redis_data
  61. key = 'count:forums'
  62. return redis_data.hgetall(key)
  63. def init_app(app):
  64. app.jinja_env.globals['DEFAULT'] = default
  65. app.jinja_env.globals['hot_tags'] = hot_tags
  66. app.jinja_env.globals['recent_tags'] = recent_tags
  67. app.jinja_env.globals['show_time'] = show_time
  68. app.jinja_env.globals['forums_count'] = forums_count
  69. app.jinja_env.filters['timesince'] = timesince
  70. app.jinja_env.filters['safe_clean'] = safe_clean