Welcome to little lamb

Code » qmdoc » commit e1f7165

Highlighting in code blocks: Add italic & bold

author Olivier Brunel
2023-01-13 10:53:47 UTC
committer Olivier Brunel
2023-01-13 10:54:17 UTC
parent f9f17161bfb57848d677a27c3b9113d8f5145b3c

Highlighting in code blocks: Add italic & bold

In addition to <hl> for highlighting effect, it is now also possible to
use <em> and <b> for italic and bold respectively.

Notably useful for man pages, where in function signatures one might
want to have variable names in e.g. italics.

doc/qmdoc.1.md +2 -1
src/main.c +29 -13

diff --git a/doc/qmdoc.1.md b/doc/qmdoc.1.md
index 746aa80..42a6713 100644
--- a/doc/qmdoc.1.md
+++ b/doc/qmdoc.1.md
@@ -303,7 +303,8 @@ corner of the block, as indication to the reader.
 Additionaly, custom parameters can be specified, in the `key[=value]` form :
 
 * *hl* : Enable highlighting effects. Then, any text in between `<hl>` and
-  `</hl>` tags will be highlighted.
+  `</hl>` tags will be =highlighted=, in between `<em>` and `</em>` will be in
+  /italic/, and in between `<b>` and `</b>` will be in *bold*.
 * *from=`N`* : Enable line numbering, starting from `N`
 
 Line numbering is also automatically enabled for any fenced code blocks /unless/
diff --git a/src/main.c b/src/main.c
index c165f3a..ba5ac81 100644
--- a/src/main.c
+++ b/src/main.c
@@ -217,21 +217,37 @@ strip_tags(struct ctx *ctx, const char *text, size_t size, int no_escaping)
 static int
 highlight_escape_text(struct ctx *ctx, const char *text, size_t size)
 {
+    struct {
+        const char *open;
+        const char *close;
+        int olen;
+        int clen;
+        const char *orepl;
+        const char *crepl;
+        size_t pos;
+    } tags[] = {
+        { "<hl>", "</hl>", 4, 5, "<span class=\"highlighted\">", "</span>" },
+        { "<em>", "</em>", 4, 5, "<em>", "</em>" },
+        { "<b>", "</b>", 3, 4, "<strong>", "</strong>" },
+        { NULL, NULL, 0, 0, NULL, NULL }
+    }, *t = tags;
     const char *end = text + size;
 
-    for ( ; text < end; ) {
-        const char *open, *close;
-        open = memmem(text, size, "<hl>", 4);
-        if (!open) break;
-        close = memmem(open + 4, end - open - 4, "</hl>", 5);
-        if (!close) break;
-        if (!escape_text(ctx, text, open - text)
-                || !raw_str(ctx, "<span class=\"highlighted\">")
-                || !escape_text(ctx, open + 4, close - open - 4)
-                || !raw_str(ctx, "</span>"))
-            return 0;
-        text = close + 5;
-        size = end - text;
+    for ( ; t->open; ++t) {
+        for ( ; text < end; ) {
+            const char *open, *close;
+            open = memmem(text, size, t->open, t->olen);
+            if (!open) break;
+            close = memmem(open + t->olen, end - open - t->olen, t->close, t->clen);
+            if (!close) break;
+            if (!highlight_escape_text(ctx, text, open - text)
+                    || !raw_str(ctx, t->orepl)
+                    || !highlight_escape_text(ctx, open + t->olen, close - open - t->olen)
+                    || !raw_str(ctx, t->crepl))
+                return 0;
+            text = close + t->clen;
+            size = end - text;
+        }
     }
     if (!escape_text(ctx, text, size))
         return 0 ;