From 3a754133dbc138390503341fd2e9beba3e43aa4b Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Fri, 27 Jan 2012 12:05:17 -0800 Subject: Merged old FileIO --- imports/codemirror/mode/gfm/gfm.js | 108 +++++++++++++++++++++++++++++++++ imports/codemirror/mode/gfm/index.html | 47 ++++++++++++++ 2 files changed, 155 insertions(+) create mode 100755 imports/codemirror/mode/gfm/gfm.js create mode 100755 imports/codemirror/mode/gfm/index.html (limited to 'imports/codemirror/mode/gfm') diff --git a/imports/codemirror/mode/gfm/gfm.js b/imports/codemirror/mode/gfm/gfm.js new file mode 100755 index 00000000..1e9d7d87 --- /dev/null +++ b/imports/codemirror/mode/gfm/gfm.js @@ -0,0 +1,108 @@ +CodeMirror.defineMode("gfm", function(config, parserConfig) { + var mdMode = CodeMirror.getMode(config, "markdown"); + var aliases = { + html: "htmlmixed", + js: "javascript", + json: "application/json", + c: "text/x-csrc", + "c++": "text/x-c++src", + java: "text/x-java", + csharp: "text/x-csharp", + "c#": "text/x-csharp", + }; + + // make this lazy so that we don't need to load GFM last + var getMode = (function () { + var i, modes = {}, mimes = {}, mime; + + var list = CodeMirror.listModes(); + for (i = 0; i < list.length; i++) { + modes[list[i]] = list[i]; + } + var mimesList = CodeMirror.listMIMEs(); + for (i = 0; i < mimesList.length; i++) { + mime = mimesList[i].mime; + mimes[mime] = mimesList[i].mime; + } + + for (var a in aliases) { + if (aliases[a] in modes || aliases[a] in mimes) + modes[a] = aliases[a]; + } + + return function (lang) { + return modes[lang] ? CodeMirror.getMode(config, modes[lang]) : null; + } + }()); + + function markdown(stream, state) { + // intercept fenced code blocks + if (stream.sol() && stream.match(/^```([\w+#]*)/)) { + // try switching mode + state.localMode = getMode(RegExp.$1) + if (state.localMode) + state.localState = state.localMode.startState(); + + state.token = local; + return 'code'; + } + + return mdMode.token(stream, state.mdState); + } + + function local(stream, state) { + if (stream.sol() && stream.match(/^```/)) { + state.localMode = state.localState = null; + state.token = markdown; + return 'code'; + } + else if (state.localMode) { + return state.localMode.token(stream, state.localState); + } else { + stream.skipToEnd(); + return 'code'; + } + } + + // custom handleText to prevent emphasis in the middle of a word + // and add autolinking + function handleText(stream, mdState) { + var match; + if (stream.match(/^\w+:\/\/\S+/)) { + return 'linkhref'; + } + if (stream.match(/^[^\[*\\<>` _][^\[*\\<>` ]*[^\[*\\<>` _]/)) { + return mdMode.getType(mdState); + } + if (match = stream.match(/^[^\[*\\<>` ]+/)) { + var word = match[0]; + if (word[0] === '_' && word[word.length-1] === '_') { + stream.backUp(word.length); + return undefined; + } + return mdMode.getType(mdState); + } + if (stream.eatSpace()) { + return null; + } + } + + return { + startState: function() { + var mdState = mdMode.startState(); + mdState.text = handleText; + return {token: markdown, mode: "markdown", mdState: mdState, + localMode: null, localState: null}; + }, + + copyState: function(state) { + return {token: state.token, mode: state.mode, mdState: CodeMirror.copyState(mdMode, state.mdState), + localMode: state.localMode, + localState: state.localMode ? CodeMirror.copyState(state.localMode, state.localState) : null}; + }, + + token: function(stream, state) { + return state.token(stream, state); + } + } +}); diff --git a/imports/codemirror/mode/gfm/index.html b/imports/codemirror/mode/gfm/index.html new file mode 100755 index 00000000..b27d49ab --- /dev/null +++ b/imports/codemirror/mode/gfm/index.html @@ -0,0 +1,47 @@ + + +
+