tsunami

log in
history
points to
tsunami syntax: italics & bolding ~ tsunami syntax tsunami syntax
add add

tsunami syntax: italics & bolding

Luke Breuer
2008-03-22 20:37 UTC

Introduction
Time allows _italics_ and *bolding*: italics and bolding. The trick is this: which underscores and which asterisks trigger markup? This is not the easiest of questions.
Was this
html = Regex.Replace(html, @"
    (?<=              # the character before the _ must be:
        ^             # beginning of the string
        |             # or
        [ \t>(\r\n]   # space, tab, open paren, end of line
    )
    _                 # the underscore that gets removed
    (
        (?:           # one or more of:
            [^_<>]    # no underscores or other HTML markup (from previous replaces)
            |         # or
            \\_       # escaped underscores are allowed
        )+?
    )
    (?<!\\)           # don't match an underscore if it is escaped
    _                 # the ending underscore that gets removed
    ", 
    "<em>$1</em>", RegexOptions.IgnorePatternWhitespace);
Currently
var map = new Dictionary<string, string>();

map["_"] = "em";
map["*"] = "strong";
map["--"] = "strike";

// emphasize, bold, strikethrough
html = Regex.Replace(html, @"
    (?<!\\)
    (?<=[ \t>(\r\n\-]|^)    #yuck... had to remove [a-z], as a_b_c should NOT italicize b
    (_|\*|--)
    (?![ \t\r\n`])
    (
        (?:
            (?!\1)[^<>\r\n]
            |
            \\\1
        )+?
    )
    (?<!\\)
    \1", 
    m => string.Format("<{0}>{1}</{0}>", map[m.Groups[1].Value], m.Groups[2]),
    RegexOptions.IgnorePatternWhitespace);
Problem
      \_italics_ and \*bolding*: _italics_ and *bolding*
bold:                          ^^^^^^^^^^^^^^^^
italic:                           

(I'm not sure I want to have to escape all * and _ and --.)