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.

46 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 the 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("cs", lexers.CSharpLexer())
  28. _process("node", lexers.JavascriptLexer())
  29. _process("go", lexers.GoLexer())
  30. _process("python_urllib2", lexers.PythonLexer())
  31. _process("python_requests", lexers.PythonLexer())
  32. _process("python_requests_fail", lexers.PythonLexer())
  33. _process("python_requests_start", lexers.PythonLexer())
  34. _process("python_requests_payload", lexers.PythonLexer())
  35. _process("php", lexers.PhpLexer(startinline=True))
  36. _process("powershell", lexers.shell.PowerShellLexer())
  37. _process("powershell_inline", lexers.shell.BashLexer())
  38. _process("ruby", lexers.RubyLexer())