Parse the Markdown-like Markup!
PHP, 770 Bytes
Online Version
There are some rules to make html with this markup
the replacement of blockquote must before all others
h6,h5,h4,h3,h2,h1 is the next order
strong, em
img, a
and (li ,ul, li,ol) or (li ,ol, li,ul)
$s=[
"%>(.*)\n%Us"
,'%(\*|_)\1(.+)\1\1%Us'
,'%(\*|_)(.*)\1%Us'
,'%######(.*)\n%Us'
,'%#####(.*)\n%Us'
,'%####(.*)\n%Us'
,'%###(.*)\n%Us'
,'%##(.*)\n%Us'
,'%#(.*)\n%Us'
,'%- (.*)\n%Us'
,'%(?<!</li>)<li>%Us'
,'%</li>(?!<li>)%Us'
,'%\d+\. (.*)\n%Us'
,'%(?<!li>|ul>)<li>%Us'
,'%</li>(?!<(li|/ul)>)%Us'
,'%(\t| )(.*)\n%Us'
,'%!\[(.*)\]\((.*)\)%Us'
,'%\[(.*)\]\((.*)\)%Us'
];
$r=[
'<blockquote>\1</blockquote>'
,'<strong>\2</strong>'
,'<em>\2</em>'
,'<h6>\1</h6>'
,'<h5>\1</h5>'
,'<h4>\1</h5>'
,'<h3>\1</h5>'
,'<h2>\1</h5>'
,'<h1>\1</h5>'
,'<li>\1</li>'
,'<ul><li>'
,'</li></ul>'
,'<li>\1</li>'
,'<ol><li>'
,'</li></ol>'
,'<pre><code>\2</code></pre>'
,'<img src="\2" alt="\1"/>'
,'<a href="\2">\1</a>'
];
echo preg_replace($s,$r,$i);
Lua, 641 bytes
t={"<","<","^.+(>)",">","^(#+)(.*)",function(a,b)return("<h%d>%s</h%d>"):format(#a,b,#a)end,"^>(.*)","<blockquote>%1</blockquote>","^[\t ]+(.*)","<pre><code>%1</code>","!%[(.-)%]%((.-)%)",'<img src="%2" alt="%1"/>',"%[(.-)%]%((.-)%)",'<a href="%2">%1</a>',"%*%*","","__","","%b","<strong>%1</strong>","","","%b**","<em>%1</em>","%*","","%b__","<em>%1</em>","_",""}for l in io.lines()do u,U=l:match("^- ")or u and print("</ul>"),u o,O=l:match("^%d%. ")or o and print("</ol>"),o for i=1,#t,2 do l=l:gsub(t[i],t[i+1])end print((l:gsub("^- (.*)",(U and""or"<ul>").."<li>%1</li>"):gsub("^%d%. (.*)",(O and""or"<ol>").."<li>%1</li>")))end
Try it online!
Readable Version and Explanation
t = { -- Table used for simple replacements
"<", "<",
"^.+(>)", ">", -- Cannot be at the beginning of a line because of blockquotes
"^(#+)(.*)", function(a,b)return("<h%d>%s</h%d>"):format(#a,b,#a)end, -- Combined h# replacement
"^>(.*)", "<blockquote>%1</blockquote>",
"^[\t ]+(.*)", "<pre><code>%1</code>",
"!%[(.-)%]%((.-)%)", '<img src="%2" alt="%1"/>',
"%[(.-)%]%((.-)%)", '<a href="%2">%1</a>',
"%*%*", "", -- Using the actual control character is shorter than using an escape code
"__", "",
"%b", "<strong>%1</strong>",
"", "",
"%b**", "<em>%1</em>",
"%*", "",
"%b__", "<em>%1</em>",
"_", ""
}
for l in io.lines() do -- For every line in STDIN
u,U=l:match("^- ") or u and print("</ul>"),u -- Some compact logic to print the end of the lists and mark things as such
o,O=l:match("^%d%. ") or o and print("</ol>"),o
for i=1,#t,2 do
l=l:gsub(t[i],t[i+1]) -- Run through the table of replacements
end
print(( -- Use two parentheses so that we only print the first return value
l:gsub("^- (.*)", (U and "" or "<ul>") .. "<li>%1</li>") -- Rest of the list logic
:gsub("^%d%. (.*)", (O and "" or "<ol>") .. "<li>%1</li>")
))
end
I can expand upon specific things if wanted, but it's very late as-of posting this so it'll have to wait until later.