diff options
| Commit | fba9f8e29140547ebefaed7bec15173cb8c7d800 (patch) | |
|---|---|---|
| Author | 2024-12-31 17:58:59 +0200 | |
| Committer | 2024-12-31 17:58:59 +0200 | |
| Files | e4cf879c8bb46a4fc374b8115d127e352c99eaa7 /single.awk | |
| csg-fba9f8e29140547ebefaed7bec15173cb8c7d800.tar csg-fba9f8e29140547ebefaed7bec15173cb8c7d800.tar.gz csg-fba9f8e29140547ebefaed7bec15173cb8c7d800.zip | ||
| Parent | 0e1268eaad117ec6174074787b5ad08b6f233987 (diff) | |
feat(single): Implement links and images
1 files changed, 67 insertions, 7 deletions
| -rw-r--r-- | single.awk | 74 |
@@ -9,15 +9,15 @@ # [ ] Lists # [x] Code blocks # [x] Horizontal rules -# [.] Span elements -# [ ] Links -# [ ] Inline -# [ ] Reference +# [x] Span elements +# [x] Links +# [x] Inline +# [x] Reference # [x] Emphasis # [x] Code -# [ ] Images -# [ ] Inline -# [ ] Reference +# [x] Images +# [x] Inline +# [x] Reference # [ ] Other # [ ] Autolinks # [ ] Escapes @@ -56,6 +56,22 @@ function _next() { next } +function char(string, ind) { + if (1 > ind || ind > length(string)) { + return ""; + } + return substr(string, ind, 1) +} + +function backup_match() { + backup_rstart = RSTART + backup_rlength = RLENGTH +} +function restore_match() { + RSTART = backup_rstart + RLENGTH = backup_rlength +} + { not_paragraph_p1 = not_paragraph not_paragraph = 0 @@ -74,6 +90,11 @@ function _next() { _next() } +# Link definition +!inside_codeblock && /^\[[^\]]+\]:[ \t]+.*/ { + next +} + # Blockquotes blockquote_level > 0 && empty0 { for (P1 = P1 ORS; blockquote_level > 0; --blockquote_level) { @@ -190,6 +211,45 @@ function gsurround(brace, left, right, blen) { gsurround("`" , "<code>" , "</code>") } +# Links +/!?\[[^\]]*\] ?[(\[][^)\]]*[\])]/ { + while (match($0, /!?\[[^\]]*\] ?[(\[][^)\]]*[\])]/)) { + link = substr($0, RSTART, RLENGTH) + backup_match() + + ## ](xxxxx) + match(link, /\] ?[(\[][^)\]]*[\])]$/) + has_space = char(link, RSTART + 1) == " " + is_img = char(link, 1) == "!" + + ## ...[xxxxx](...)... + text = substr(link, 2 + is_img, RSTART - 2 - is_img) + ## ...[...](xxxxx)... + href = substr(link, RSTART + has_space + 2, RLENGTH - has_space - 3) + + title = "\"\"" + if (char(link, RSTART + RLENGTH - 1) == "]") { ## is reference link + if (href == "") href = text # [...][] + ## [...]: ... "xxxxx" + title = reference[href, "title"] + ## [...]: xxxxx "..." + href = reference[href, "href"] + } + else if (match(href, /[ \t]"[^"]*"[ \t]*$/)) { ## has title + ## ...[...](... "xxxxx")... + title = substr(href, RSTART + 1, RLENGTH - 1) + ## ...[...](xxxxx "...")... + href = substr(href, 1, RSTART - 1) + } + + if (is_img) elem = "<img src=\"" href "\" alt=\"" text "\" title=" title ">" + else elem = "<a href=\"" href "\" title=" title ">" text "</a>" + + restore_match() + sub_match(elem) + } +} + # Paragraph emptyP1 && !empty0 && !not_paragraph { $0 = "<p>" $0 } !emptyP1 && empty0 && !not_paragraph_p1 { P1 = P1 "</p>" } |
