Welcome to little lamb

Code » qmdoc » commit a5c8f41

main: Parse <hl> inside code blocks

author Olivier Brunel
2022-12-24 15:53:29 UTC
committer Olivier Brunel
2022-12-24 15:53:29 UTC
parent e96ffdbb50079a01ed57c23ff36534147a1907aa

main: Parse <hl> inside code blocks

main.c +23 -2

diff --git a/main.c b/main.c
index a94d129..eed24c1 100644
--- a/main.c
+++ b/main.c
@@ -1,3 +1,4 @@
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -304,6 +305,27 @@ static int escape_text(const char *text, size_t size)
     return 0;
 }
 
+static int code_text(const char *text, size_t size, void *ctx_)
+{
+    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;
+        printf("%.*s%s%.*s%s",
+               (int) (open - text), text,
+               "<span class=\"highlighted\">",
+               (int) (close - open - 4), open + 4,
+               "</span>");
+        text = close + 5;
+        size = end - text;
+    }
+    printf("%.*s", (int) size, text);
+    return 0;
+}
+
 static int text(MD_TEXTTYPE type, const MD_CHAR *text, MD_SIZE size, void *ctx_)
 {
     switch (type) {
@@ -326,8 +348,7 @@ static int text(MD_TEXTTYPE type, const MD_CHAR *text, MD_SIZE size, void *ctx_)
             break;
 
         case MD_TEXT_CODE:
-            printf("%.*s", size, text);
-            break;
+            return code_text(text, size, ctx_);
 
         case MD_TEXT_HTML:
             if (size == 4 && !strncmp(text, "<hl>", 4))