commit 8c3969e7a6d10326c611faa10224072ce67d3c7a
parent 503398bd5f325447f7d801306222e5fc142cacae
Author: Demonstrandum <moi@knutsen.co>
Date: Thu, 6 Aug 2020 01:22:47 +0100
Ditch linguist and switch to python.
Diffstat:
6 files changed, 63 insertions(+), 87 deletions(-)
diff --git a/Gemfile b/Gemfile
@@ -1,4 +0,0 @@
-source 'https://rubygems.org'
-
-gem 'github-linguist'
-gem 'pygments.rb'
diff --git a/Gemfile.lock b/Gemfile.lock
@@ -1,25 +0,0 @@
-GEM
- remote: https://rubygems.org/
- specs:
- charlock_holmes (0.7.7)
- escape_utils (1.2.1)
- github-linguist (7.9.0)
- charlock_holmes (~> 0.7.6)
- escape_utils (~> 1.2.0)
- mini_mime (~> 1.0)
- rugged (>= 0.25.1)
- mini_mime (1.0.2)
- multi_json (1.15.0)
- pygments.rb (1.2.1)
- multi_json (>= 1.0.0)
- rugged (1.0.1)
-
-PLATFORMS
- ruby
-
-DEPENDENCIES
- github-linguist
- pygments.rb
-
-BUNDLED WITH
- 2.1.2
diff --git a/Makefile b/Makefile
@@ -63,8 +63,8 @@ dist:
${OBJ}: ${HDR}
-stagit.out: stagit.o ${COMPATOBJ} Gemfile
- bundle install
+stagit.out: stagit.o ${COMPATOBJ} requirements.txt
+ pip3 install -r requirements.txt
${CC} -o $@ stagit.o ${COMPATOBJ} ${STAGIT_LDFLAGS}
stagit-index.out: stagit-index.o ${COMPATOBJ}
diff --git a/highlight b/highlight
@@ -1,49 +1,33 @@
-#!/usr/bin/env ruby
-
-require 'linguist'
-require 'pygments'
-
-stdin = ARGF.file
-
-filename = stdin.readline.strip # Read first line (filename).
-contents = stdin.read # Read rest (code).
-
-class FakeBlob < Linguist::FileBlob
- def initialize(path, content, base_bath=nil)
- super(path, base_bath)
- @content = content
- end
-
- def data
- @content
- end
-
- def size
- @content.bytesize
- end
-end
-
-blob = FakeBlob.new(filename, contents)
-detected = if blob.language
- blob.language.name
- else
- "Text only"
- end
-
-# Debugging
-#puts "File #{filename}"
-#puts "Code:
-#{contents}"
-#print "Language: "
-#pp detected
-
-html = Pygments.highlight(contents,
- :lexer => detected,
- :formatter => 'html',
- :options => {
- :encoding => 'utf-8',
- :linenos => 'table',
- :lineanchors => 'loc',
- :anchorlinenos => true})
-
-puts html
+#!/usr/bin/env python3
+
+import pygments
+from pygments import highlight
+from pygments.formatters import HtmlFormatter
+from pygments.lexers import guess_lexer, guess_lexer_for_filename
+
+from sys import stdin
+
+filename = stdin.readline().strip()
+contents = stdin.read()
+
+lexer=None
+
+try:
+ lexer = guess_lexer_for_filename(filename, contents)
+except pygments.util.ClassNotFound:
+ try:
+ lexer = guess_lexer(contents)
+ except pygments.util.ClassNotFound:
+ pass
+
+if lexer is None:
+ from pygments.lexers import TextLexer
+ lexer = TextLexer
+
+FORMAT = HtmlFormatter(
+ lineos='table',
+ lineanchors='loc',
+ anchorlinenos=True)
+
+print(highlight(contents, lexer, FORMAT))
+
diff --git a/repo-gen.sh b/repo-gen.sh
@@ -1,25 +1,35 @@
#!/bin/sh
fresh=false
+unique=false
for arg in "$@"; do
+ [ "$unique" = true ] && unique="$arg"
+ [ "$arg" = "--only" ] && unique=true
[ "$arg" = "--fresh" ] && fresh=true
done
+[ "$unique" = true ] && {
+ echo "Expected argument after \`--only\`.";
+ exit 1;
+}
+
STAGIT=/var/www/git/stagit.out
[ ! -f "$STAGIT" ] && STAGIT=stagit
STAGIT_INDEX=/var/www/git/stagit-index.out
[ ! -f "$STAGIT_INDEX" ] && STAGIT_INDEX=stagit-index
-for repo in /srv/git/*.git; do
- repo="$(basename "$repo" | rev | cut -c 5- | rev)"
+build_html () {
+ repo="$1"
+ repo="$(basename "$repo" | sed 's/\.git$//g')"
- [ "$fresh" = true ] \
- && echo "Deleting HTML for $repo." \
- && rm -fr "/var/www/git/$repo"
+ [ "$fresh" = true ] && {
+ echo "Deleting HTML for $repo.";
+ rm -fr "/var/www/git/$repo";
+ }
mkdir -p "/var/www/git/$repo"
- cd "/var/www/git/$repo"
+ cd "/var/www/git/$repo" || { echo "Couldn't cd."; exit 1; }
[ ! -f style.css ] && ln -s ../style.css ./
[ ! -f favicon.png ] && ln -s ../favicon.png ./
@@ -29,9 +39,18 @@ for repo in /srv/git/*.git; do
echo "git://git.knutsen.co/$repo" > "/srv/git/$repo.git/url"
COMMAND="$STAGIT /srv/git/$repo.git"
+ echo "Building web-page for $repo."
echo "$COMMAND"
$COMMAND
-done
+}
+
+if [ "$unique" = false ]; then
+ for repo in /srv/git/*.git; do
+ build_html "$repo"
+ done
+else
+ build_html "$unique"
+fi
echo "Generating index.html with \`$STAGIT_INDEX\`."
"$STAGIT_INDEX" /srv/git/*.git > /var/www/git/index.html
diff --git a/requirements.txt b/requirements.txt
@@ -0,0 +1,2 @@
+Pygments
+