summaryrefslogtreecommitdiff
path: root/lib/filters/syntax-highlighting.sh
blob: 44cd310769c4807dd385fb03ac4641bf78de5c8d (plain)
  1. #!/bin/sh
  2. # This script can be used to implement syntax highlighting in the cgit
  3. # tree-view by referring to this file with the source-filter or repo.source-
  4. # filter options in cgitrc.
  5. # Depends: cmark highlight
  6. # The following environment variables can be used to retrieve the configuration
  7. # of the repository for which this script is called:
  8. # CGIT_REPO_URL ( = repo.url setting )
  9. # CGIT_REPO_NAME ( = repo.name setting )
  10. # CGIT_REPO_PATH ( = repo.path setting )
  11. # CGIT_REPO_OWNER ( = repo.owner setting )
  12. # CGIT_REPO_DEFBRANCH ( = repo.defbranch setting )
  13. # CGIT_REPO_SECTION ( = section setting )
  14. # CGIT_REPO_CLONE_URL ( = repo.clone-url setting )
  15. # store filename and extension in local vars
  16. BASENAME="$1"
  17. EXTENSION="${BASENAME##*.}"
  18. [ "${BASENAME}" = "${EXTENSION}" ] && EXTENSION=txt
  19. [ -z "${EXTENSION}" ] && EXTENSION=txt
  20. # map Makefile and Makefile.* to .mk
  21. [ "${BASENAME%%.*}" = "Makefile" ] && EXTENSION=mk
  22. # map less common markdown extensions to .md
  23. case "$(printf '%s' "$EXTENSION" | tr '[:upper:]' '[:lower:]')" in
  24. markdown|mdown|mkd|mdwn) EXTENSION=md;;
  25. esac
  26. case "$EXTENSION" in
  27. md) exec cmark --safe;;
  28. *) exec highlight --force -f -I -O xhtml -S "$EXTENSION" --ordered-list --line-number-ref=n 2>/dev/null;;
  29. esac