diff --git a/README.md b/README.md index d7f437a..1bfc53a 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Here's a list of supported markdown primitives - Lists, both bulleted and numbered - Inline html (partial) - Links +- Images ## License diff --git a/markdown.awk b/markdown.awk index 8aeca14..d4dc694 100644 --- a/markdown.awk +++ b/markdown.awk @@ -206,10 +206,33 @@ function parse_link(str, arr) { return "" arr[1] "" } +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 "" + } + return "" +} + 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) diff --git a/test.sh b/test.sh index be8f946..82a3010 100755 --- a/test.sh +++ b/test.sh @@ -423,6 +423,15 @@ Link with title: [foo](/bar "baz")
Link with title: foo
EOF +check <<-"EOF" +Spimple image: ![foo](/bar) + +Image with title: ![foo](/bar "baz") +--- +Spimple image:
+Image with title:
+EOF + check <<-"EOF" Horizontal rules: