Add support for images
This commit is contained in:
parent
0ebdbe72c4
commit
29281f3d61
3 changed files with 38 additions and 0 deletions
|
@ -27,6 +27,7 @@ Here's a list of supported markdown primitives
|
|||
- Lists, both bulleted and numbered
|
||||
- Inline html (partial)
|
||||
- Links
|
||||
- Images
|
||||
|
||||
## License
|
||||
|
||||
|
|
28
markdown.awk
28
markdown.awk
|
@ -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(/&/, "\\&", str);
|
||||
gsub(/</, "\\<", 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)
|
||||
|
|
9
test.sh
9
test.sh
|
@ -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:
|
||||
|
||||
|
|
Loading…
Reference in a new issue