You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.9 KiB

  1. from django.core.management.base import BaseCommand
  2. def _process(name, lexer):
  3. from pygments import highlight
  4. from pygments.formatters import HtmlFormatter
  5. source = open("templates/front/snippets/%s.txt" % name).read()
  6. processed = highlight(source, lexer, HtmlFormatter())
  7. processed = processed.replace("PING_URL", "{{ ping_url }}")
  8. processed = processed.replace("SITE_ROOT", "{{ SITE_ROOT }}")
  9. processed = processed.replace("PING_ENDPOINT", "{{ PING_ENDPOINT }}")
  10. with open("templates/front/snippets/%s.html" % name, "w") as out:
  11. out.write(processed)
  12. class Command(BaseCommand):
  13. help = 'Compiles snippets with Pygments'
  14. def handle(self, *args, **options):
  15. try:
  16. from pygments import lexers
  17. except ImportError:
  18. self.stdout.write("This command requires Pygments package.")
  19. self.stdout.write("Please install it with:\n\n")
  20. self.stdout.write(" pip install Pygments\n\n")
  21. return
  22. # Invocation examples
  23. _process("bash_curl", lexers.BashLexer())
  24. _process("bash_wget", lexers.BashLexer())
  25. _process("browser", lexers.JavascriptLexer())
  26. _process("crontab", lexers.BashLexer())
  27. _process("python_urllib2", lexers.PythonLexer())
  28. _process("python_requests", lexers.PythonLexer())
  29. _process("php", lexers.PhpLexer())
  30. _process("powershell", lexers.shell.PowerShellLexer())
  31. _process("node", lexers.JavascriptLexer())
  32. # API examples
  33. _process("list_checks_request", lexers.BashLexer())
  34. _process("list_checks_response", lexers.JsonLexer())
  35. _process("create_check_request_a", lexers.BashLexer())
  36. _process("create_check_request_b", lexers.BashLexer())
  37. _process("create_check_response", lexers.JsonLexer())
  38. _process("pause_check_request", lexers.BashLexer())
  39. _process("pause_check_response", lexers.JsonLexer())