from Hacker News

Show HN: Mawkdown – (Toy) Markdown Parser in Awk

by rethab on 7/18/20, 9:22 AM with 4 comments

  • by asicsp on 7/18/20, 1:24 PM

    Nice. Haven't gone through it fully, but the header parsing stood out for improvement. Use match to capture number of '#' characters and use length, for example:

        $ echo '# ' | awk 'match($0, /^#+ /, m){print length(m[0])-1}'
        1
        $ echo '### ' | awk 'match($0, /^#+ /, m){print length(m[0])-1}'
        3
    
    You can also use capture groups so that you do not need -1 and remove that substr as well.

        awk 'match($0, /^(#+) (.+)/, m){l=length(m[1]); print "<h" l ">" m[2] "</h" l ">"}'
  • by khm on 7/18/20, 7:53 PM

    Would it be ok to use elements of this to improve the one we ship with Werc?

    http://code.9front.org/hg/werc/file/2ace198c631b/bin/contrib...