Welcome to little lamb

Code » qmdoc » commit 90b2594

Add proper line numbers to code blocks

author Olivier Brunel
2022-12-24 20:38:17 UTC
committer Olivier Brunel
2022-12-24 20:38:17 UTC
parent 568a26baad09e42b3a7e6ee1aee67a5236858f55

Add proper line numbers to code blocks

base.md +1 -1
main.c +12 -2

diff --git a/base.md b/base.md
index 25000d7..541d5fc 100644
--- a/base.md
+++ b/base.md
@@ -88,7 +88,7 @@ in ``pre``/``code`` blocks too (unlike the others) :
 Oh yeah, we might wanna have "code blocks". Those would be like ``pre`` ones,
 only a bit more styled :
 
-```
+```c
 #include <stdlib.h>
 int main(int argc, char **argv)
 {
diff --git a/main.c b/main.c
index 04eda12..20e4e1e 100644
--- a/main.c
+++ b/main.c
@@ -191,8 +191,18 @@ static int leave_block(MD_BLOCKTYPE type, void *details, void *ctx_)
                     ctx->buf.f = NULL;
 
                     if (ctx->code.flags & CODE_LINES) {
-                        printf("<pre class=\"lineno\">%d...</pre>", ctx->code.from);
-                        printf("<code>");
+                        const char *s = ctx->buf.str;
+                        size_t l = ctx->buf.len;
+
+                        printf("<pre class=\"lineno\">");
+                        for (int n = ctx->code.from; s; ++n) {
+                            const char *e = memchr(s, '\n', l);
+                            if (!e) break;
+                            l -= ++e - s;
+                            s = e;
+                            printf("%d\n", n);
+                        }
+                        printf("</pre><code>");
                     } else {
                         printf("<pre>");
                     }