|
5 | 5 |
|
6 | 6 | **Author:** Niklas Frykholm, <[email protected]> |
7 | 7 | **Date:** 31 May 2008 |
8 | | -**Edited by:** Marcus Thunström (2018-06-14) |
| 8 | +**Edited by:** Marcus Thunström (2018-06-14, 2021-05-06) |
9 | 9 |
|
10 | 10 | This is an implementation of the popular text markup language Markdown in pure Lua. |
11 | 11 | Markdown can convert documents written in a simple and easy to read text format |
@@ -921,65 +921,101 @@ end |
921 | 921 | function images(text) |
922 | 922 | local function reference_link(alt, id) |
923 | 923 | alt = encode_alt(alt:match("%b[]"):sub(2,-2)) |
924 | | - id = id:match("%[(.*)%]"):lower() |
925 | | - if id == "" then id = text:lower() end |
| 924 | + id = id:match("%b[]"):sub(2,-2):lower() -- @Edit |
| 925 | + |
| 926 | + if id == "" then id = text:lower() end |
| 927 | + |
926 | 928 | link_database[id] = link_database[id] or {} |
927 | | - if not link_database[id].url then return nil end |
928 | | - local url = link_database[id].url or id |
929 | | - url = encode_alt(url) |
| 929 | + if not link_database[id].url then return nil end |
| 930 | + |
| 931 | + local url = link_database[id].url or id |
| 932 | + url = encode_alt(url) |
930 | 933 | local title = encode_alt(link_database[id].title) |
931 | | - if title then title = " title=\"" .. title .. "\"" else title = "" end |
| 934 | + -- print(("images reference_link url=%-60s title=%s"):format(url, tostring(title)) -- DEBUG |
| 935 | + |
| 936 | + if title then |
| 937 | + title = ' title="' .. title .. '"' |
| 938 | + else |
| 939 | + title = "" |
| 940 | + end |
| 941 | + |
932 | 942 | return add_escape ('<img src="' .. url .. '" alt="' .. alt .. '"' .. title .. ">") -- @Edit |
933 | 943 | end |
934 | 944 |
|
935 | 945 | local function inline_link(alt, link) |
936 | | - alt = encode_alt(alt:match("%b[]"):sub(2,-2)) |
937 | | - local url, title = link:match("%(<?(.-)>?[ \t]*['\"](.+)['\"]") |
938 | | - url = url or link:match("%(<?(.-)>?%)") |
939 | | - url = encode_alt(url) |
940 | | - title = encode_alt(title) |
| 946 | + alt = encode_alt(alt:match"%b[]":sub(2,-2)) |
| 947 | + local url, title = link:match"%(<?(.-)>?[ \t]*['\"](.+)['\"]" |
| 948 | + url = url or link:match"%(<(.-)>%)" or link:match"%((.-)%)" or "" -- @Edit |
| 949 | + url = encode_alt(url) |
| 950 | + title = encode_alt(title) |
| 951 | + -- print(("images inline_link url=%-60s title=%s"):format(url, tostring(title)) -- DEBUG |
| 952 | + |
941 | 953 | if title then |
942 | 954 | return add_escape('<img src="' .. url .. '" alt="' .. alt .. '" title="' .. title .. '">') -- @Edit |
943 | 955 | else |
944 | 956 | return add_escape('<img src="' .. url .. '" alt="' .. alt .. '">') -- @Edit |
945 | 957 | end |
946 | 958 | end |
947 | 959 |
|
| 960 | + -- ![alt] [id] |
948 | 961 | text = text:gsub("!(%b[])[ \t]*\n?[ \t]*(%b[])", reference_link) |
| 962 | + --  |
| 963 | + --  |
| 964 | + --  |
| 965 | + --  |
949 | 966 | text = text:gsub("!(%b[])(%b())", inline_link) |
| 967 | + |
950 | 968 | return text |
951 | 969 | end |
952 | 970 |
|
953 | 971 | -- Handle anchor references |
954 | 972 | function anchors(text) |
955 | 973 | local function reference_link(text, id) |
956 | 974 | text = text:match("%b[]"):sub(2,-2) |
957 | | - id = id:match("%b[]"):sub(2,-2):lower() |
958 | | - if id == "" then id = text:lower() end |
| 975 | + id = id:match("%b[]"):sub(2,-2):lower() |
| 976 | + |
| 977 | + if id == "" then id = text:lower() end |
| 978 | + |
959 | 979 | link_database[id] = link_database[id] or {} |
960 | | - if not link_database[id].url then return nil end |
961 | | - local url = link_database[id].url or id |
962 | | - url = encode_alt(url) |
| 980 | + if not link_database[id].url then return nil end |
| 981 | + |
| 982 | + local url = link_database[id].url or id |
| 983 | + url = encode_alt(url) |
963 | 984 | local title = encode_alt(link_database[id].title) |
964 | | - if title then title = " title=\"" .. title .. "\"" else title = "" end |
965 | | - return add_escape("<a href=\"" .. url .. "\"" .. title .. ">") .. text .. add_escape("</a>") |
| 985 | + -- print(("anchors reference_link url=%-60s title=%s"):format(url, tostring(title)) -- DEBUG |
| 986 | + |
| 987 | + if title then |
| 988 | + title = ' title="' .. title .. '"' |
| 989 | + else |
| 990 | + title = "" |
| 991 | + end |
| 992 | + |
| 993 | + return add_escape('<a href="' .. url .. '"' .. title .. ">") .. text .. add_escape("</a>") |
966 | 994 | end |
967 | 995 |
|
968 | 996 | local function inline_link(text, link) |
969 | | - text = text:match("%b[]"):sub(2,-2) |
970 | | - local url, title = link:match("%(<?(.-)>?[ \t]*['\"](.+)['\"]") |
971 | | - title = encode_alt(title) |
972 | | - url = url or link:match("%(<?(.-)>?%)") or "" |
973 | | - url = encode_alt(url) |
| 997 | + text = text:match"%b[]":sub(2,-2) |
| 998 | + local url, title = link:match"%(<?(.-)>?[ \t]*['\"](.+)['\"]" |
| 999 | + url = url or link:match"%(<(.-)>%)" or link:match"%((.-)%)" or "" -- @Edit |
| 1000 | + url = encode_alt(url) |
| 1001 | + title = encode_alt(title) |
| 1002 | + -- print(("anchors inline_link url=%-60s title=%s"):format(url, tostring(title)) -- DEBUG |
| 1003 | + |
974 | 1004 | if title then |
975 | | - return add_escape("<a href=\"" .. url .. "\" title=\"" .. title .. "\">") .. text .. "</a>" |
| 1005 | + return add_escape('<a href="' .. url .. '" title="' .. title .. '">') .. text .. "</a>" |
976 | 1006 | else |
977 | | - return add_escape("<a href=\"" .. url .. "\">") .. text .. add_escape("</a>") |
| 1007 | + return add_escape('<a href="' .. url .. '">') .. text .. add_escape("</a>") |
978 | 1008 | end |
979 | 1009 | end |
980 | 1010 |
|
| 1011 | + -- [text] [id] |
981 | 1012 | text = text:gsub("(%b[])[ \t]*\n?[ \t]*(%b[])", reference_link) |
| 1013 | + -- [text](link) |
| 1014 | + -- [text](<link>) |
| 1015 | + -- [text](link "title") |
| 1016 | + -- [text](<link> "title") |
982 | 1017 | text = text:gsub("(%b[])(%b())", inline_link) |
| 1018 | + |
983 | 1019 | return text |
984 | 1020 | end |
985 | 1021 |
|
@@ -1127,9 +1163,9 @@ function strip_link_definitions(text) |
1127 | 1163 | end |
1128 | 1164 |
|
1129 | 1165 | local def_no_title = "\n ? ? ?(%b[]):[ \t]*\n?[ \t]*<?([^%s>]+)>?[ \t]*" |
1130 | | - local def_title1 = def_no_title .. "[ \t]+\n?[ \t]*[\"'(]([^\n]+)[\"')][ \t]*" |
1131 | | - local def_title2 = def_no_title .. "[ \t]*\n[ \t]*[\"'(]([^\n]+)[\"')][ \t]*" |
1132 | | - local def_title3 = def_no_title .. "[ \t]*\n?[ \t]+[\"'(]([^\n]+)[\"')][ \t]*" |
| 1166 | + local def_title1 = def_no_title .. "[ \t]+\n?[ \t]*[\"'(]([^\n]+)[\"')][ \t]*" |
| 1167 | + local def_title2 = def_no_title .. "[ \t]*\n[ \t]*[\"'(]([^\n]+)[\"')][ \t]*" |
| 1168 | + local def_title3 = def_no_title .. "[ \t]*\n?[ \t]+[\"'(]([^\n]+)[\"')][ \t]*" |
1133 | 1169 |
|
1134 | 1170 | text = text:gsub(def_title1, link_def) |
1135 | 1171 | text = text:gsub(def_title2, link_def) |
|
0 commit comments