]> git.armaanb.net Git - stagit.git/commitdiff
Ditch linguist and switch to python.
authorDemonstrandum <moi@knutsen.co>
Thu, 6 Aug 2020 00:22:47 +0000 (01:22 +0100)
committerDemonstrandum <moi@knutsen.co>
Thu, 6 Aug 2020 00:22:47 +0000 (01:22 +0100)
Gemfile [deleted file]
Gemfile.lock [deleted file]
Makefile
highlight
repo-gen.sh
requirements.txt [new file with mode: 0644]

diff --git a/Gemfile b/Gemfile
deleted file mode 100644 (file)
index 9452483..0000000
--- a/Gemfile
+++ /dev/null
@@ -1,4 +0,0 @@
-source 'https://rubygems.org'
-
-gem 'github-linguist'
-gem 'pygments.rb'
diff --git a/Gemfile.lock b/Gemfile.lock
deleted file mode 100644 (file)
index 86591cb..0000000
+++ /dev/null
@@ -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
index 1b2750e4b1351957a3971d04862f2817e99a1a6d..f94e8e9f0ea0277d91761d5f6031d05d3a7e38b2 100644 (file)
--- 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}
index 2b46f20fdd885d4c0df382109c7c818d51520d2c..cfcc452fb2862375aa073c4c5f2052514957f49d 100755 (executable)
--- 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))
+
index c2a25a81c29ad9cde5b5523cb93847af608e2866..d3440483e8bf5775d3aa23c018d82c7a8e85aab5 100755 (executable)
@@ -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
new file mode 100644 (file)
index 0000000..3d4b9d6
--- /dev/null
@@ -0,0 +1,2 @@
+Pygments
+