Add support for images

This commit is contained in:
Konstantin Nazarov 2021-07-29 22:13:25 +00:00
parent 0ebdbe72c4
commit 29281f3d61
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 38 additions and 0 deletions

View file

@ -27,6 +27,7 @@ Here's a list of supported markdown primitives
- Lists, both bulleted and numbered
- Inline html (partial)
- Links
- Images
## License

View file

@ -206,10 +206,33 @@ function parse_link(str, arr) {
return "<a href=\"" arr[2] "\" title=\"" arr[4] "\">" arr[1] "</a>"
}
function extract_image(str, i, sstr) {
sstr=substr(str, i, length(str) - i + 1);
if (!match(sstr, /^!\[([^\[\]]*)\]\( *([^() ]*)( +"([^"]*)")? *\)/, arr))
return "";
return substr(str, i, RLENGTH);
}
function parse_image(str, arr) {
if (!match(str, /^!\[([^\[\]]*)\]\( *([^() ]*)( +"([^"]*)")? *\)/, arr))
return "";
if (arr[4] == "") {
return "<img src=\"" arr[2] "\" alt=\"" arr[1] "\" />"
}
return "<img src=\"" arr[2] "\" alt=\"" arr[1] "\" title=\"" arr[4] "\" />"
}
function is_link(str, i) {
return extract_link(str, i) != "";
}
function is_image(str, i) {
return extract_image(str, i) != "";
}
function escape_text(str) {
gsub(/&/, "\\&amp;", str);
gsub(/</, "\\&lt;", str);
@ -270,6 +293,11 @@ function parse_line(str, result, end, i, c) {
result = result parse_link(link);
i = i + length(link) - 1;
}
else if (c == "!" && is_image(str, i)) {
image = extract_image(str, i);
result = result parse_image(image);
i = i + length(image) - 1;
}
else {
if (c == "\n") {
if (length(result) > 0)

View file

@ -423,6 +423,15 @@ Link with title: [foo](/bar "baz")
<p>Link with title: <a href="/bar" title="baz">foo</a></p>
EOF
check <<-"EOF"
Spimple image: ![foo](/bar)
Image with title: ![foo](/bar "baz")
---
<p>Spimple image: <img src="/bar" alt="foo" /></p>
<p>Image with title: <img src="/bar" alt="foo" title="baz" /></p>
EOF
check <<-"EOF"
Horizontal rules: