Welcome to little lamb

Code » qmdoc » commit ddc195d

Add support of [page](1) for man pages

author Olivier Brunel
2023-01-06 11:18:42 UTC
committer Olivier Brunel
2023-01-06 11:18:42 UTC
parent d0a6ac3c89757464af6546a7d0c92c2c265cb276

Add support of [page](1) for man pages

Meaning we don't set that as a link to "1" but add a "(1)" after "page"
so it looks like usual man page references.

main.c +39 -9

diff --git a/main.c b/main.c
index c72586a..0af5a2c 100644
--- a/main.c
+++ b/main.c
@@ -301,6 +301,19 @@ attribute_box_type(MD_ATTRIBUTE *attr)
     return 0;
 }
 
+static int
+get_section(MD_ATTRIBUTE *attr)
+{
+    const char *s = attr->text;
+    MD_SIZE l = attr->substr_offsets[1] - attr->substr_offsets[0];
+
+    if (attr->substr_types[0] != MD_TEXT_NORMAL || l != 1
+            || !(*s >= '1' && *s <= '8'))
+        return 0;
+
+    return *s - '0';
+}
+
 
 static int
 enter_block(MD_BLOCKTYPE type, void *details, void *ctx_)
@@ -845,15 +858,17 @@ enter_span(MD_SPANTYPE type, void *details, void *ctx_)
             {
                 MD_SPAN_A_DETAIL *d = details;
 
-                if (!raw_str(ctx, "<a href=\"") || !attribute(ctx, &d->href))
-                    return ERR_PARSER_ENTER_SPAN;
+                if (!get_section(&d->href)) {
+                    if (!raw_str(ctx, "<a href=\"") || !attribute(ctx, &d->href))
+                        return ERR_PARSER_ENTER_SPAN;
 
-                if (d->title.text
-                        && (!raw_str(ctx, "\" title=\"") || !attribute(ctx, &d->title)))
-                    return ERR_PARSER_ENTER_SPAN;
+                    if (d->title.text
+                            && (!raw_str(ctx, "\" title=\"") || !attribute(ctx, &d->title)))
+                        return ERR_PARSER_ENTER_SPAN;
 
-                if (!raw_str(ctx, "\">"))
-                    return ERR_PARSER_ENTER_SPAN;
+                    if (!raw_str(ctx, "\">"))
+                        return ERR_PARSER_ENTER_SPAN;
+                }
             }
             break;
 
@@ -907,8 +922,23 @@ leave_span(MD_SPANTYPE type, void *details, void *ctx_)
             break;
 
         case MD_SPAN_A:
-            if (!raw_str(ctx, "</a>"))
-                return ERR_PARSER_LEAVE_SPAN;
+            {
+                MD_SPAN_A_DETAIL *d = details;
+                int section = get_section(&d->href);
+
+                if (section) {
+                    char buf[UINT32_FMT + 2];
+                    buf[0] = '(';
+                    section = uint32_fmt(buf + 1, (uint32) section);
+                    buf[1 + section] = ')';
+                    buf[2 + section] = '\0';
+                    if (!raw_str(ctx, buf))
+                        return ERR_PARSER_LEAVE_SPAN;
+                } else {
+                    if (!raw_str(ctx, "</a>"))
+                        return ERR_PARSER_LEAVE_SPAN;
+                }
+            }
             break;
 
         case MD_SPAN_IMG: