How can i replace DokuWiki nested list string with using one or two regexps in Ruby?
For example, if we have this string:
* one
* two
* three
* four
we should get this HTML:
- one
- two
- three
- four开发者_运维百科
I've made a regexp replacing the whole list. E.g.:
s.sub!(/(^\s+\*\s.+$)+/m, '<ul>\1</ul>')
And it works as it should. But how to replace the single list items?
The regex :
Here are some example lists :
* first item
* second item
No longer a list
* third item? no, it's the first item of the second list
* first item
* second item with linebreak\\ second line
* third item with code: <code>
some code
comes here
</code>
* fourth item
The regex for matching all lists
(?<=^|\n)(?: {2,}\*([^\n]*?<code>.*?</code>[^\n]*|[^\n]*)\n?)+
View it in action : http://rubular.com/r/VMjwbyhJTm
The code :
Surround all lists with a <ul>...</ul>
s.sub!(/(?<=^|\n)(?: {2,}\*(?:[^\n]*?<code>.*?<\/code>[^\n]*|[^\n]*)\n?)+/m, '<ul>\0</ul>')
Add missing <li>
s (s2 in the following code is the string with <ul>...</ul>
added)
s2.sub!(/ {2,}\*([^\n]*?<code>.*?<\/code>[^\n]*|[^\n]*)\n?/m, '<li>\1</li>')
Note : Nested lists can not be handled with this regex. If this is a requirement, a parser will be more adapted !
精彩评论