javascript - CodeMirror - language auto-detection - Stack Overflow

I'm using stand-alone highlighting (not the editor), something like this:.htmlHow can I auto-dete

I'm using stand-alone highlighting (not the editor), something like this: .html

How can I auto-detect the language to use when running the highlighter?

There's a mode-autochanging demo here: .html

But I don't know how could I adapt this to work with Codemirror.runMode(). I wish to highlight the entire code block using the auto-detected scheme.

I'm using stand-alone highlighting (not the editor), something like this: http://codemirror/demo/runmode.html

How can I auto-detect the language to use when running the highlighter?

There's a mode-autochanging demo here: http://codemirror/demo/changemode.html

But I don't know how could I adapt this to work with Codemirror.runMode(). I wish to highlight the entire code block using the auto-detected scheme.

Share Improve this question asked Jan 20, 2012 at 15:32 AlexAlex 66.2k185 gold badges460 silver badges651 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

The "changemode" demo can only distinguish between the "Scheme" programming language and "everything else", see the implementation of

function looksLikeScheme(code) {
    return !/^\s*\(\s*function\b/.test(code) && /^\s*[;\(]/.test(code);
}

So this won't work to auto-detect other programming languages, and as the demo pages says, even for detecting Scheme it's very crude.

Unfortunately, it's not easy to auto-detect a large range of programming languages from a source code snippet (for small snippets, different programming languages might even use the exact same syntax). However, a reasonable approach is suggested in Detecting programming language from a snippet.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745257566a4619046.html

相关推荐

  • javascript - CodeMirror - language auto-detection - Stack Overflow

    I'm using stand-alone highlighting (not the editor), something like this:.htmlHow can I auto-dete

    4小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信