代码之家  ›  专栏  ›  技术社区  ›  Thomas Hansen

什么是进行wiki格式化(.Net)的最佳正则表达式?

  •  2
  • Thomas Hansen  · 技术社区  · 16 年前

    伙计们,我有一个wiki格式化算法,我正在使用 Stacked

    // Body is wiki content...
    string tmp = Body.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");
    // Sanitizing carriage returns...
    tmp = tmp.Replace("\\r\\n", "\\n");
    
    // Replacing dummy links...
    tmp = Regex.Replace(
    " " + tmp,
    "(?<spaceChar>\\s+)(?<linkType>http://|https://)(?<link>\\S+)",
    "${spaceChar}<a href=\"${linkType}${link}\"" + nofollow + ">${link}</a>",
    RegexOptions.Compiled).Trim();
    
    // Replacing wiki links
    tmp = Regex.Replace(tmp,
    "(?<begin>\\[{1})(?<linkType>http://|https://)(?<link>\\S+)\\s+(?<content>[^\\]]+)(?<end>[\\]]{1})",
    "<a href=\"${linkType}${link}\"" + nofollow + ">${content}</a>",
    RegexOptions.Compiled);
    
    // Replacing bolds
    tmp = Regex.Replace(tmp,
    "(?<begin>\\*{1})(?<content>.+?)(?<end>\\*{1})",
    "<strong>${content}</strong>",
    RegexOptions.Compiled);
    
    // Replacing italics
    tmp = Regex.Replace(tmp,
    "(?<begin>_{1})(?<content>.+?)(?<end>_{1})",
    "<em>${content}</em>",
    RegexOptions.Compiled);
    
    // Replacing lists
    tmp = Regex.Replace(tmp,
    "(?<begin>\\*{1}[ ]{1})(?<content>.+)(?<end>[^*])",
    "<li>${content}</li>",
    RegexOptions.Compiled);
    tmp = Regex.Replace(tmp,
    "(?<content>\\<li\\>{1}.+\\<\\/li\\>)",
    "<ul>${content}</ul>",
    RegexOptions.Compiled);
    
    // Quoting
    tmp = Regex.Replace(tmp,
    "(?<content>^&gt;.+$)",
    "<blockquote>${content}</blockquote>",
    RegexOptions.Compiled | RegexOptions.Multiline).Replace("</blockquote>\n<blockquote>", "\n");
    
    // Paragraphs
    tmp = Regex.Replace(tmp,
    "(?<content>)\\n{2}",
    "${content}</p><p>",
    RegexOptions.Compiled);
    
    // Breaks
    tmp = Regex.Replace(tmp,
    "(?<content>)\\n{1}",
    "${content}<br />",
    RegexOptions.Compiled);
    
    // Code
    tmp = Regex.Replace(tmp,
    "(?<begin>\\[code\\])(?<content>[^$]+)(?<end>\\[/code\\])",
    "<pre class=\"code\">${content}</pre>",
    RegexOptions.Compiled);
    
    // Now hopefully tmp will contain perfect HTML
    

    对于那些认为在这里很难看到代码的人,您也可以查看它 here ...

    下面是完整的“wiki语法”;

    Link; [http://x.com text]
    
    *bold* (asterisk on both sides)
    
    _italic_ (underscores on both sides)
    
    * Listitem 1
    * Listitem 2
    * Listitem 3
    (the above is asterixes but so.com also creates lists from it)
    
    2 x Carriage Return is opening a new paragraph
    
    1 x Carriage Return is break (br)
    
    [code]
    if( YouDoThis )
      YouCanWriteCode();
    [/code]
    
    
    > quote (less then operator)
    

    1 回复  |  直到 16 年前
        1
  •  4
  •   Tomalak    16 年前

    不要使用正则表达式来完成这项任务,它是危险的,不会让你高兴。用户输入可能会以超出想象的方式被破坏(有意或无意),没有正则表达式能够覆盖所有可能的情况。

    有一些上下文和嵌套概念的解析器在这里更好。

    你能发布一个完整的允许语法的示例,这样人们就可以开始给你一个如何解析它的想法了吗?


    编辑:你可以考虑使用(可能修改的) Markdown Markdown.NET ,至少看一下源代码可能是值得的。也许修改它来满足你的需要并不难。