From 8f661c130b2c79761475f3f620e48a52fa53b2e0 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sat, 9 Feb 2019 16:50:41 -0500 Subject: Add css and inline HTML options to hi --- bin/hi.c | 68 ++++++++++++++++++++++++++++++++++++----------------------- bin/man1/hi.1 | 26 +++++++++++++++++++---- 2 files changed, 64 insertions(+), 30 deletions(-) (limited to 'bin') diff --git a/bin/hi.c b/bin/hi.c index dcf725a3..d0609e55 100644 --- a/bin/hi.c +++ b/bin/hi.c @@ -285,8 +285,10 @@ static void check(void) { #define ENUM_OPTION \ X(Monospace, "monospace") \ - X(Document, "document") \ - X(Title, "title") + X(Document, "document") \ + X(Title, "title") \ + X(CSS, "css") \ + X(Inline, "inline") enum Option { #define X(option, _) option, @@ -446,6 +448,7 @@ static void htmlEscape(const char *str, size_t len) { size_t run = strcspn(str, "&<>"); if (run > len) run = len; switch (str[0]) { + break; case '"': run = 1; printf("""); break; case '&': run = 1; printf("&"); break; case '<': run = 1; printf("<"); break; case '>': run = 1; printf(">"); @@ -456,24 +459,40 @@ static void htmlEscape(const char *str, size_t len) { } } +static const char *ClassName[ClassLen] = { +#define X(class) [class] = #class, + ENUM_CLASS +#undef X +}; + +static const char *HTMLStyle[ClassLen] = { + [Keyword] = "color: dimgray;", + [Macro] = "color: green;", + [String] = "color: teal;", + [Format] = "color: teal; font-weight: bold;", + [Interp] = "color: green;", + [Comment] = "color: navy;", + [Todo] = "color: navy; font-weight: bold;", +}; + static void htmlHeader(const char *opts[]) { - if (opts[Document]) { - printf("\n"); - if (opts[Title]) htmlEscape(opts[Title], strlen(opts[Title])); - printf("\n"); - printf( - "\n" - ); + if (!opts[Document]) goto pre; + printf("\n"); + if (opts[Title]) htmlEscape(opts[Title], strlen(opts[Title])); + printf("\n"); + if (opts[CSS]) { + printf("\n"); + } else if (!opts[Inline]) { + printf("\n"); } +pre: printf("
");
 }
 
@@ -482,16 +501,13 @@ static void htmlFooter(const char *opts[]) {
 	printf("
\n"); } -static const char *ClassName[ClassLen] = { -#define X(class) [class] = #class, - ENUM_CLASS -#undef X -}; - static void htmlOutput(const char *opts[], enum Class class, const char *str, size_t len) { - (void)opts; - printf("", ClassName[class]); + if (opts[Inline]) { + printf("", HTMLStyle[class] ? HTMLStyle[class] : ""); + } else { + printf("", ClassName[class]); + } htmlEscape(str, len); printf(""); } diff --git a/bin/man1/hi.1 b/bin/man1/hi.1 index e1ab5b5b..befb1a69 100644 --- a/bin/man1/hi.1 +++ b/bin/man1/hi.1 @@ -1,4 +1,4 @@ -.Dd February 7, 2019 +.Dd February 9, 2019 .Dt HI 1 .Os . @@ -72,13 +72,31 @@ classes. The options are as follows: .Bl -tag -width "title=..." .It Cm document -Output an HTML document -with inline CSS. +Output an HTML document. .It Cm title Ns = Ns Ar ... -Set the title of the HTML document. +With +.Cm document , +set the +.Sy +element text. The default title is the .Ar file name. +.It Cm css Ns = Ns Ar url +With +.Cm document , +output a +.Sy <link> +element for the external stylesheet +.Ar url . +If unset, +output default styles in a +.Sy <style> +element. +.It Cm inline +Output inline +.Sy style +attributes rather than classes. .El .El . -- cgit 1.4.1