fork download
  1. s = """
  2. * first item
  3. * second item
  4.  
  5. No longer a list
  6.  
  7. * third item? no, it's the first item of the second list
  8.  
  9. * first item
  10. * second item with linebreak\\ second line
  11. * third item with code: <code>
  12. some code
  13. comes here
  14. </code>
  15. * fourth item
  16.  
  17. * This is a list
  18. * The second item
  19. * You may have different levels
  20. * Another item
  21. """
  22.  
  23. s.gsub!(/(?<=^|\n)(?: {2,}\*(?:[^\n]*?<code>.*?<\/code>[^\n]*|[^\n]*)\n?)+/m, '<ul>\0</ul>')
  24. s.gsub!(/ {2,}\*([^\n]*?<code>.*?<\/code>[^\n]*|[^\n]*)\n?/m, '<li>\1</li>')
  25.  
  26. puts s
  27.  
  28. =begin
  29. Returns
  30. <ul><li> first item</li><li> second item</li></ul>
  31. No longer a list
  32.  
  33. <ul><li> third item? no, it's the first item of the second list</li></ul>
  34. <ul><li> first item </li><li> second item with linebreak\ second line</li><li> third item with code: <code>
  35. some code
  36. comes here
  37. </code></li><li> fourth item</li></ul>
  38. <ul><li> This is a list</li><li> The second item</li><li> You may have different levels</li><li> Another item</li></ul>
  39. =end
  40.  
  41. =begin
  42. While should return
  43.  
  44. <ul><li> first item</li><li> second item</li></ul>
  45. No longer a list
  46.  
  47. <ul><li> third item? no, it's the first item of the second list</li></ul>
  48. <ul><li> first item </li><li> second item with linebreak\ second line</li><li> third item with code: <code>
  49. some code
  50. comes here
  51. </code></li><li> fourth item</li></ul>
  52. <ul><li> This is a list</li><li> The second item<ul><li> You may have different levels</li></ul></li><li> Another item</li></ul> </div>
  53. =end
Success #stdin #stdout 0s 4716KB
stdin
Standard input is empty
stdout
<ul><li> first item</li><li> second item</li></ul>
No longer a list

<ul><li> third item? no, it's the first item of the second list</li></ul>
<ul><li> first item </li><li> second item with linebreak\ second line</li><li> third item with code: <code>
some code
comes here
</code></li><li> fourth item</li></ul>
<ul><li> This is a list</li><li> The second item</li><li> You may have different levels</li><li> Another item</li></ul>