开发者

DokuWiki nested lists regexp

开发者 https://www.devze.com 2023-04-09 15:55 出处:网络
How can i replace DokuWiki nested list string with using one or two regexps in Ruby? For example, if we have this string:

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:

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 !

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号