From 641fe408b1b9b45d1dc20905d8ec3c00cd4fc804 Mon Sep 17 00:00:00 2001
From: Thomas Voss <thomasvoss@live.com>
Date: Wed, 12 Jan 2022 00:42:15 +0100
Subject: Add support for customizable tab widths

---
 mpaste.1  | 14 +++++++++++---
 mpaste.go | 11 ++++++-----
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/mpaste.1 b/mpaste.1
index f163734..a739936 100644
--- a/mpaste.1
+++ b/mpaste.1
@@ -1,5 +1,5 @@
 .\" vi: tw=100
-.Dd $Mdocdate: October 12 2021 $
+.Dd $Mdocdate: January 12 2022 $
 .Dt MPASTE 1 URM
 .Os UNIX
 .Sh NAME
@@ -16,8 +16,9 @@
 .Sh DESCRIPTION
 .Nm
 is a minimal paste server for hosting plaintext data.
-The paste server has support for file uploads, syntax highlighting, a customizable homepage, and a
-password protected mode where only users with valid API keys can upload pastes.
+The paste server has support for file uploads, syntax highlighting, a customizable homepage,
+customizable tab width, and a password protected mode where only users with valid API keys can
+upload pastes.
 For the simplest example of a working paste server, simply run
 .Nm
 and provide it with a
@@ -45,6 +46,13 @@ If you would like syntax highlighting simply append the appropiate file extensio
 For example, to syntax highlight C code with paste ID 5, go to
 .Dq domain.com/5.c .
 .Pp
+By default when syntax highlighting the server displays tab characters with a width of 8 columns. If
+you would like to customize the width of a tab you can set the
+.Dq tabs
+query parameter in the URI.
+For example, to view a Python file with a tab width of 4 columns, go to
+.Dq domain.com/1.py?tabs=4 .
+.Pp
 If you would like to protect the server by requiring all users to have an API key, simply set the
 .Ev MPASTE_SECRET
 environment variable.
diff --git a/mpaste.go b/mpaste.go
index f105c2b..b5de948 100644
--- a/mpaste.go
+++ b/mpaste.go
@@ -37,11 +37,7 @@ var (
 	user_file    string
 )
 
-var (
-	style     = styles.Get("pygments")
-	formatter = html.New(html.Standalone(true), html.WithClasses(true),
-		html.WithLineNumbers(true), html.LineNumbersInTable(true))
-)
+var style = styles.Get("pygments")
 
 func usage() {
 	fmt.Fprintf(os.Stderr,
@@ -147,6 +143,11 @@ func syntax_highlighting(w http.ResponseWriter, r *http.Request) {
 		WRITE_HEADER(http.StatusInternalServerError, "Failed to tokenize output")
 	}
 
+	tw, err := strconv.Atoi(r.URL.Query().Get("tabs"))
+	if err != nil {
+		tw = 8
+	}
+	formatter := html.New(html.Standalone(true), html.WithClasses(true), html.WithLineNumbers(true), html.LineNumbersInTable(true), html.TabWidth(tw))
 	if err := formatter.Format(w, style, iterator); err != nil {
 		WRITE_HEADER(http.StatusInternalServerError, "Failed to format output")
 	}
-- 
cgit v1.2.3