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.

39 lines
1.3 KiB

  1. import os
  2. from django.conf import settings
  3. from django.core.management.base import BaseCommand
  4. class Command(BaseCommand):
  5. help = "Renders Markdown to HTML"
  6. def handle(self, *args, **options):
  7. try:
  8. import markdown
  9. # We use pygments for highlighting code samples
  10. import pygments
  11. except ImportError as e:
  12. self.stdout.write(f"This command requires the {e.name} package.")
  13. self.stdout.write("Please install it with:\n\n")
  14. self.stdout.write(f" pip install {e.name}\n\n")
  15. return
  16. extensions = ["fenced_code", "codehilite", "tables", "def_list", "attr_list"]
  17. ec = {"codehilite": {"css_class": "highlight", "startinline": True}}
  18. docs_path = os.path.join(settings.BASE_DIR, "templates/docs")
  19. for doc in os.listdir(docs_path):
  20. if not doc.endswith(".md"):
  21. continue
  22. print("Rendering %s" % doc)
  23. src_path = os.path.join(docs_path, doc)
  24. dst_path = os.path.join(docs_path, doc[:-3] + ".html")
  25. text = open(src_path, "r", encoding="utf-8").read()
  26. html = markdown.markdown(text, extensions=extensions, extension_configs=ec)
  27. with open(dst_path, "w", encoding="utf-8") as f:
  28. f.write(html)