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.

34 lines
1.3 KiB

  1. from django.core.management.base import BaseCommand
  2. def _process(fin, fout, lexer):
  3. from pygments import highlight
  4. from pygments.formatters import HtmlFormatter
  5. source = open("templates/front/snippets/" + fin).read()
  6. processed = highlight(source, lexer, HtmlFormatter())
  7. processed = processed.replace("PING_URL", "{{ ping_url }}")
  8. with open("templates/front/snippets/" + fout, "w") as out:
  9. out.write(processed)
  10. class Command(BaseCommand):
  11. help = 'Compiles snippets with Pygments'
  12. def handle(self, *args, **options):
  13. try:
  14. from pygments import lexers
  15. except ImportError:
  16. self.stdout.write("This command requires Pygments package.")
  17. self.stdout.write("Please install it with:\n\n")
  18. self.stdout.write(" pip install Pygments\n\n")
  19. return
  20. _process("bash.txt", "bash.html", lexers.BashLexer())
  21. _process("browser.txt", "browser.html", lexers.JavascriptLexer())
  22. _process("crontab.txt", "crontab.html", lexers.BashLexer())
  23. _process("python.txt", "python.html", lexers.PythonLexer())
  24. _process("php.txt", "php.html", lexers.PhpLexer())
  25. _process("powershell.txt", "powershell.html",
  26. lexers.shell.PowerShellLexer())
  27. _process("node.txt", "node.html", lexers.JavascriptLexer())