Gopher + Markdown = something kinda nice

mdparse.lua 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. local md = {}
  2. function md.parse(md)
  3. local it = {}
  4. it.l = {}
  5. it[#it+1] = {["content"]="",["bold"]=false,["italic"]=false}
  6. local lc,llc = "",""
  7. local function newpart()
  8. it[#it+1] = {["content"]="",["bold"]=it[#it].bold,["italic"]=it[#it].italic}
  9. end
  10. newpart()
  11. local lm = false
  12. for c in md:gmatch(".") do
  13. if c == "*" then
  14. if lc == "*" then
  15. it[#it].italic = false
  16. it[#it].italic = it[#it-1].italic
  17. it[#it].bold = not it[#it].bold
  18. else
  19. newpart()
  20. it[#it].italic = not it[#it].italic
  21. end
  22. elseif c == "[" then
  23. newpart()
  24. elseif c == "(" and lc == "]" then
  25. lm = true
  26. it[#it].content = it[#it].content:sub(1,-2)
  27. it[#it].address = ""
  28. elseif c == ")" and lm then
  29. lm = false
  30. it.l[#it.l+1] = it[#it].address
  31. it[#it].addrid = #it.l
  32. newpart()
  33. else
  34. if not lm then
  35. it[#it].content = it[#it].content .. c
  36. else
  37. it[#it].address = it[#it].address .. c
  38. end
  39. end
  40. llc = lc
  41. lc = c
  42. end
  43. for k,v in pairs(it) do
  44. if v.content then
  45. v.content = v.content:gsub("\n","\r\n")
  46. end
  47. end
  48. return it
  49. end
  50. return md