kisscommunity

kisscommunity.bvnf.space site sources
git clone git://bvnf.space/home/kiss/kisscommunity.git
Log | Files | Refs | Submodules | README | LICENSE

label.awk (635B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
#!/usr/bin/awk -f
# For articles with only h2 and h3 as major and minor headings.
# If there is a ## Index already present, run with `awk -v major=-1`.
# You must manually move the index to the top.

BEGIN { li = 0 }

/^## / {
    printf("## %%[%s]\n", substr($0, 4))
    labels[li,0] = substr($0, 4)
    labels[li++,1] = 1
    next
}
/^### / {
    printf("### %%[%s]\n", substr($0, 5))
    labels[li++,0] = substr($0, 5)
    next
}
{
    print
}

END {
    printf("\n## Index\n\n")
    for (i=0; i < li; i++) {
        if (labels[i,1] != 1)
            printf("  ");
        printf("- [%s](#%s)\n", labels[i,0], labels[i,0]);
    }
}