summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-02-17 21:13:36 -0500
committerJune McEnroe <june@causal.agency>2019-02-17 21:13:36 -0500
commitbc2eb7a5313217c2edf32914c5018ed910d08b15 (patch)
tree86c021bcc30772941410eeb0b6e30fef67724864 /bin
parentSimplify temp trap in up (diff)
downloadsrc-bc2eb7a5313217c2edf32914c5018ed910d08b15.tar.gz
src-bc2eb7a5313217c2edf32914c5018ed910d08b15.zip
Add hi -f html -o anchor for line number links
Diffstat (limited to '')
-rw-r--r--bin/hi.c24
-rw-r--r--bin/man1/hi.113
2 files changed, 37 insertions, 0 deletions
diff --git a/bin/hi.c b/bin/hi.c
index 7fac4bf2..ae72e43c 100644
--- a/bin/hi.c
+++ b/bin/hi.c
@@ -312,6 +312,7 @@ static void check(void) {
 }
 
 #define ENUM_OPTION \
+	X(Anchor, "anchor") \
 	X(CSS, "css") \
 	X(Document, "document") \
 	X(Inline, "inline") \
@@ -503,6 +504,19 @@ static void htmlHeader(const char *opts[]) {
 			htmlTabSize(opts[Tab]);
 			printf(" }\n");
 		}
+		if (opts[Anchor]) {
+			printf(
+				"a.hi.%s { text-decoration: none; %s }\n"
+				"a.hi.%s:focus {"
+				" outline-style: none;"
+				" font-weight: bold;"
+				" text-decoration: underline;"
+				" }\n"
+				"a.hi.%s::before { content: attr(data-line); }\n",
+				ClassName[Line], HTMLStyle[Line],
+				ClassName[Line], ClassName[Line]
+			);
+		}
 		for (enum Class class = 0; class < ClassLen; ++class) {
 			if (!HTMLStyle[class]) continue;
 			printf("span.hi.%s { %s }\n", ClassName[class], HTMLStyle[class]);
@@ -526,6 +540,16 @@ static void htmlFooter(const char *opts[]) {
 
 static void
 htmlOutput(const char *opts[], enum Class class, const char *str, size_t len) {
+	if (opts[Anchor] && class == Line) {
+		size_t num = 0;
+		sscanf(str, " %zu", &num);
+		printf(
+			"<a class=\"hi %s\" id=\"L%zu\" href=\"#L%zu\" data-line=\"%.*s\">"
+			"</a>",
+			ClassName[class], num, num, (int)len, str
+		);
+		return;
+	}
 	if (opts[Inline]) {
 		printf("<span style=\"%s\">", HTMLStyle[class] ? HTMLStyle[class] : "");
 	} else {
diff --git a/bin/man1/hi.1 b/bin/man1/hi.1
index afe0fe0c..ee3a55a8 100644
--- a/bin/man1/hi.1
+++ b/bin/man1/hi.1
@@ -69,9 +69,18 @@ Output HTML
 with
 .Sy <span>
 classes.
+.
 .Pp
 The options are as follows:
 .Bl -tag -width "title=..."
+.It Cm anchor
+With
+.Fl n ,
+output line numbers
+as un-selectable anchor links.
+Incompatible with
+.Cm inline .
+.
 .It Cm css Ns = Ns Ar url
 With
 .Cm document ,
@@ -83,12 +92,15 @@ 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
@@ -98,6 +110,7 @@ set the
 .Sy tab-size
 property to
 .Ar n .
+.
 .It Cm title Ns = Ns Ar ...
 With
 .Cm document ,