Project

General

Profile

Revision 229 trunk/scripts/codemirror/mode/markdown

View differences:

index.html
6 6
    <script src="../../lib/codemirror.js"></script>
7 7
    <script src="../xml/xml.js"></script>
8 8
    <script src="markdown.js"></script>
9
    <link rel="stylesheet" href="markdown.css">
10 9
    <style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
11 10
    <link rel="stylesheet" href="../../doc/docs.css">
12 11
  </head>
markdown.js
13 13
  ,   strong   = 'strong'
14 14
  ,   emstrong = 'emstrong';
15 15

  
16
  var hrRE = /^[*-=_]/
17
  ,   ulRE = /^[*-+]\s+/
18
  ,   olRE = /^[0-9]\.\s+/
16
  var hrRE = /^([*\-=_])(?:\s*\1){2,}\s*$/
17
  ,   ulRE = /^[*\-+]\s+/
18
  ,   olRE = /^[0-9]+\.\s+/
19 19
  ,   headerRE = /^(?:\={3,}|-{3,})$/
20
  ,   codeRE = /^(k:\t|\s{4,})/
21 20
  ,   textRE = /^[^\[*_\\<>`]+/;
22 21

  
23 22
  function switchInline(stream, state, f) {
......
33 32

  
34 33
  // Blocks
35 34

  
35
  function blankLine(state) {
36
    // Reset EM state
37
    state.em = false;
38
    // Reset STRONG state
39
    state.strong = false;
40
    return null;
41
  }
42

  
36 43
  function blockNormal(stream, state) {
37
    if (stream.match(codeRE)) {
44
    var match;
45
    if (state.indentationDiff >= 4) {
46
      state.indentation -= state.indentationDiff;
38 47
      stream.skipToEnd();
39 48
      return code;
40
    }
41
    
42
    if (stream.eatSpace()) {
49
    } else if (stream.eatSpace()) {
43 50
      return null;
44
    }
45
    
46
    if (stream.peek() === '#' || stream.match(headerRE)) {
47
      stream.skipToEnd();
48
      return header;
49
    }
50
    if (stream.eat('>')) {
51
    } else if (stream.peek() === '#' || stream.match(headerRE)) {
52
      state.header = true;
53
    } else if (stream.eat('>')) {
51 54
      state.indentation++;
52
      return quote;
53
    }
54
    if (stream.peek() === '[') {
55
      state.quote = true;
56
    } else if (stream.peek() === '[') {
55 57
      return switchInline(stream, state, footnoteLink);
56
    }
57
    if (hrRE.test(stream.peek())) {
58
      var re = new RegExp('(?:\s*['+stream.peek()+']){3,}$');
59
      if (stream.match(re, true)) {
60
        return hr;
61
      }
62
    }
63
    
64
    var match;
65
    if (match = stream.match(ulRE, true) || stream.match(olRE, true)) {
58
    } else if (stream.match(hrRE, true)) {
59
      return hr;
60
    } else if (match = stream.match(ulRE, true) || stream.match(olRE, true)) {
66 61
      state.indentation += match[0].length;
67 62
      return list;
68 63
    }
......
82 77

  
83 78
  // Inline
84 79
  function getType(state) {
85
    return state.strong ? (state.em ? emstrong : strong)
86
                        : (state.em ? em       : null);
80
    var styles = [];
81
    
82
    if (state.strong) { styles.push(state.em ? emstrong : strong); }
83
    else if (state.em) { styles.push(em); }
84
    
85
    if (state.header) { styles.push(header); }
86
    if (state.quote) { styles.push(quote); }
87

  
88
    return styles.length ? styles.join(' ') : null;
87 89
  }
88 90

  
89 91
  function handleText(stream, state) {
......
192 194
        inline: inlineNormal,
193 195
        text: handleText,
194 196
        em: false,
195
        strong: false
197
        strong: false,
198
        header: false,
199
        quote: false
196 200
      };
197 201
    },
198 202

  
......
207 211
        inline: s.inline,
208 212
        text: s.text,
209 213
        em: s.em,
210
        strong: s.strong
214
        strong: s.strong,
215
        header: s.header,
216
        quote: s.quote
211 217
      };
212 218
    },
213 219

  
214 220
    token: function(stream, state) {
215 221
      if (stream.sol()) {
222
        if (stream.match(/^\s*$/, true)) { return blankLine(state); }
223

  
224
        // Reset state.header
225
        state.header = false;
226
        // Reset state.quote
227
        state.quote = false;
228

  
216 229
        state.f = state.block;
217
        var previousIndentation = state.indentation
218
        ,   currentIndentation = 0;
219
        while (previousIndentation > 0) {
220
          if (stream.eat(' ')) {
221
            previousIndentation--;
222
            currentIndentation++;
223
          } else if (previousIndentation >= 4 && stream.eat('\t')) {
224
            previousIndentation -= 4;
225
            currentIndentation += 4;
226
          } else {
227
            break;
228
          }
229
        }
230
        state.indentation = currentIndentation;
231
        
232
        if (currentIndentation > 0) return null;
230
        var indentation = stream.match(/^\s*/, true)[0].replace(/\t/g, '    ').length;
231
        state.indentationDiff = indentation - state.indentation;
232
        state.indentation = indentation;
233
        if (indentation > 0) { return null; }
233 234
      }
234 235
      return state.f(stream, state);
235 236
    },
236 237

  
238
    blankLine: blankLine,
239

  
237 240
    getType: getType
238 241
  };
239 242

  
240
});
243
}, "xml");
241 244

  
242 245
CodeMirror.defineMIME("text/x-markdown", "markdown");

Also available in: Unified diff