2012-12-15 11:39:58 +00:00
|
|
|
/*\
|
2012-12-20 16:41:06 +00:00
|
|
|
title: $:/core/modules/parsers/wikiparser/rules/codeblock.js
|
2012-12-15 11:39:58 +00:00
|
|
|
type: application/javascript
|
2012-12-20 16:02:03 +00:00
|
|
|
module-type: wikirule
|
2012-12-15 11:39:58 +00:00
|
|
|
|
|
|
|
Wiki text rule for code blocks. For example:
|
|
|
|
|
2012-12-22 23:09:10 +00:00
|
|
|
```
|
|
|
|
```
|
2012-12-15 11:39:58 +00:00
|
|
|
This text will not be //wikified//
|
2012-12-22 23:09:10 +00:00
|
|
|
```
|
|
|
|
```
|
2012-12-15 11:39:58 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
exports.name = "codeblock";
|
2012-12-20 16:02:03 +00:00
|
|
|
exports.types = {block: true};
|
2012-12-15 11:39:58 +00:00
|
|
|
|
|
|
|
exports.init = function(parser) {
|
|
|
|
this.parser = parser;
|
2014-01-07 22:57:46 +00:00
|
|
|
// Regexp to match and get language if defined
|
|
|
|
this.matchRegExp = /```([\w-]*)\r?\n/mg;
|
2012-12-15 11:39:58 +00:00
|
|
|
};
|
|
|
|
|
2012-12-20 15:43:12 +00:00
|
|
|
exports.parse = function() {
|
2012-12-22 23:09:10 +00:00
|
|
|
var reEnd = /(\r?\n```$)/mg;
|
2012-12-15 11:39:58 +00:00
|
|
|
// Move past the match
|
|
|
|
this.parser.pos = this.matchRegExp.lastIndex;
|
2014-01-07 22:57:46 +00:00
|
|
|
|
2012-12-15 11:39:58 +00:00
|
|
|
// Look for the end of the block
|
|
|
|
reEnd.lastIndex = this.parser.pos;
|
|
|
|
var match = reEnd.exec(this.parser.source),
|
|
|
|
text;
|
|
|
|
// Process the block
|
|
|
|
if(match) {
|
|
|
|
text = this.parser.source.substring(this.parser.pos,match.index);
|
|
|
|
this.parser.pos = match.index + match[0].length;
|
|
|
|
} else {
|
|
|
|
text = this.parser.source.substr(this.parser.pos);
|
|
|
|
this.parser.pos = this.parser.sourceLength;
|
|
|
|
}
|
2014-01-07 22:57:46 +00:00
|
|
|
// Return the $codeblock widget
|
2012-12-15 11:39:58 +00:00
|
|
|
return [{
|
2014-05-14 07:51:08 +00:00
|
|
|
type: "codeblock",
|
2014-01-07 22:57:46 +00:00
|
|
|
attributes: {
|
2014-01-12 17:09:24 +00:00
|
|
|
code: {type: "string", value: text},
|
|
|
|
language: {type: "string", value: this.match[1]}
|
2014-01-07 22:57:46 +00:00
|
|
|
}
|
2012-12-15 11:39:58 +00:00
|
|
|
}];
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|