summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-02-09 18:08:48 -0500
committerJune McEnroe <june@causal.agency>2019-02-09 18:08:48 -0500
commit1748f04aaada41f4d616df0750cdd55b18f9ff70 (patch)
tree6ee1185d7fb4528304f78a061b2f0e988fb0e19b
parentAdd css and inline HTML options to hi (diff)
downloadsrc-1748f04aaada41f4d616df0750cdd55b18f9ff70.tar.gz
src-1748f04aaada41f4d616df0750cdd55b18f9ff70.zip
Add tab option to HTML output of hi
-rw-r--r--bin/hi.c32
-rw-r--r--bin/man1/hi.131
2 files changed, 46 insertions, 17 deletions
diff --git a/bin/hi.c b/bin/hi.c
index d0609e55..23b3cb3a 100644
--- a/bin/hi.c
+++ b/bin/hi.c
@@ -284,11 +284,12 @@ static void check(void) {
 }
 
 #define ENUM_OPTION \
-	X(Monospace, "monospace") \
-	X(Document, "document") \
-	X(Title, "title") \
 	X(CSS, "css") \
-	X(Inline, "inline")
+	X(Document, "document") \
+	X(Inline, "inline") \
+	X(Monospace, "monospace") \
+	X(Tab, "tab") \
+	X(Title, "title")
 
 enum Option {
 #define X(option, _) option,
@@ -475,6 +476,14 @@ static const char *HTMLStyle[ClassLen] = {
 	[Todo]     = "color: navy; font-weight: bold;",
 };
 
+static void htmlTabSize(const char *tab) {
+	printf("-moz-tab-size: ");
+	htmlEscape(tab, strlen(tab));
+	printf("; tab-size: ");
+	htmlEscape(tab, strlen(tab));
+	printf(";");
+}
+
 static void htmlHeader(const char *opts[]) {
 	if (!opts[Document]) goto pre;
 	printf("<!DOCTYPE html>\n<title>");
@@ -486,14 +495,25 @@ static void htmlHeader(const char *opts[]) {
 		printf("\">\n");
 	} else if (!opts[Inline]) {
 		printf("<style>\n");
+		if (opts[Tab]) {
+			printf("pre.hi { ");
+			htmlTabSize(opts[Tab]);
+			printf(" }\n");
+		}
 		for (enum Class class = 0; class < ClassLen; ++class) {
 			if (!HTMLStyle[class]) continue;
-			printf(".hi.%s { %s }\n", ClassName[class], HTMLStyle[class]);
+			printf("span.hi.%s { %s }\n", ClassName[class], HTMLStyle[class]);
 		}
 		printf("</style>\n");
 	}
 pre:
-	printf("<pre class=\"hi\">");
+	if (opts[Inline] && opts[Tab]) {
+		printf("<pre class=\"hi\" style=\"");
+		htmlTabSize(opts[Tab]);
+		printf("\">");
+	} else {
+		printf("<pre class=\"hi\">");
+	}
 }
 
 static void htmlFooter(const char *opts[]) {
diff --git a/bin/man1/hi.1 b/bin/man1/hi.1
index befb1a69..046b6384 100644
--- a/bin/man1/hi.1
+++ b/bin/man1/hi.1
@@ -71,17 +71,6 @@ classes.
 .Pp
 The options are as follows:
 .Bl -tag -width "title=..."
-.It Cm document
-Output an HTML document.
-.It Cm title Ns = Ns Ar ...
-With
-.Cm document ,
-set the
-.Sy <title>
-element text.
-The default title is the
-.Ar file
-name.
 .It Cm css Ns = Ns Ar url
 With
 .Cm document ,
@@ -93,10 +82,30 @@ If unset,
 output default styles in a
 .Sy <style>
 element.
+.It Cm document
+Output an HTML document.
 .It Cm inline
 Output inline
 .Sy style
 attributes rather than classes.
+.It Cm tab Ns = Ns Ar n
+With
+.Cm document
+or
+.Cm inline ,
+set the
+.Sy tab-size
+property to
+.Ar n .
+.It Cm title Ns = Ns Ar ...
+With
+.Cm document ,
+set the
+.Sy <title>
+element text.
+The default title is the
+.Ar file
+name.
 .El
 .El
 .