2018-06-11 Highlighting Code and Rendering Markdown for cgit

If you publish your git repositories via cgit, you might want to link to Markdown files like you did on GitHub. By default this doesn’t work, though: the Markdown filter only applies to the About section. All the other Markdown files are simply highlighted but not rendered.

This is what we want:

=> Markdown is rendered

But this is the default:

=> Markdown is highlighted

This page hacks the two solutions together and will make your code eyes cry. I’m sorry.

Your /etc/cgitrc gets a line like the following:

source-filter=/usr/lib/cgit/filters/syntax-highlighting-or-markdown.py

And the content of this file:

#!/usr/bin/env python3

# This script uses Pygments and Python3. You must have both installed
# for this to work.
#
# http://pygments.org/
# http://python.org/
#
# It may be used with the source-filter or repo.source-filter settings
# in cgitrc.
#
# The following environment variables can be used to retrieve the
# configuration of the repository for which this script is called:
# CGIT_REPO_URL        ( = repo.url       setting )
# CGIT_REPO_NAME       ( = repo.name      setting )
# CGIT_REPO_PATH       ( = repo.path      setting )
# CGIT_REPO_OWNER      ( = repo.owner     setting )
# CGIT_REPO_DEFBRANCH  ( = repo.defbranch setting )
# CGIT_REPO_SECTION    ( = section        setting )
# CGIT_REPO_CLONE_URL  ( = repo.clone-url setting )

import sys
import io
import types
import markdown
from pygments import highlight
from pygments.util import ClassNotFound
from pygments.lexers import TextLexer
from pygments.lexers import MarkdownLexer
from pygments.lexers import guess_lexer
from pygments.lexers import guess_lexer_for_filename
from pygments.formatters import HtmlFormatter

sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
data = sys.stdin.read()
filename = sys.argv[1]
formatter = HtmlFormatter(style='pastie')

try:
	lexer = guess_lexer_for_filename(filename, data)
except ClassNotFound:
	# check if there is any shebang
	if data[0:2] == '#!':
		try:
			lexer = guess_lexer(data)
		except ClassNotFound:
			lexer = TextLexer()
	else:
		lexer = TextLexer()
except TypeError:
	lexer = TextLexer()

if isinstance(lexer, MarkdownLexer):
        # based on html-converters/md2html
        sys.stdout.write('''

''') sys.stdout.write("
") sys.stdout.flush() # Note: you may want to run this through bleach for sanitization html = markdown.markdown( data, output_format="html5", extensions=[ "markdown.extensions.fenced_code", "markdown.extensions.codehilite", "markdown.extensions.tables", "markdown.extensions.toc"], extension_configs={ "markdown.extensions.codehilite":{"css_class":"highlight"}}) sys.stdout.write(html) sys.stdout.write("
") # add back the useless tags (and this table will be hidden just like the first one) sys.stdout.write("
")
else:
        # printout pygments' css definitions as well
        sys.stdout.write('')
        sys.stdout.write(highlight(data, lexer, formatter, outfile=None))

​#cgit ​#Git ​#Markdown ​#Programming

Comments

(Please contact me if you want to remove your comment.)

I might yet get to replace all the links in Gridmapper!

=> Gridmapper

– Alex Schroeder 2018-06-11 21:02 UTC


I also added the TOC extension to /usr/lib/cgit/filters/html-converters/md2html:

markdown.markdownFromFile(
    output_format="html5",
    extensions=[
        "markdown.extensions.fenced_code",
        "markdown.extensions.codehilite",
        "markdown.extensions.tables",
        "markdown.extensions.toc"],
    extension_configs={"markdown.extensions.codehilite":{"css_class":"highlight"}})

– Alex Schroeder 2018-06-13 07:05 UTC


Thank you very much!

– satmd 2019-01-28 10:57 UTC


  • 2023-09-28*. I switched to legit. – Alex

=> legit

Proxy Information
Original URL
gemini://alexschroeder.ch/2018-06-11_Highlighting_Code_and_Rendering_Markdown_for_cgit
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
194.966464 milliseconds
Gemini-to-HTML Time
0.953867 milliseconds

This content has been proxied by September (3851b).