1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-02 18:23:28 +00:00
TiddlyWiki5/core/modules/parsers/wikiparser/rules/hardlinebreaks.js

60 lines
1.2 KiB
JavaScript
Raw Normal View History

2013-12-14 17:05:57 +00:00
/*\
title: $:/core/modules/parsers/wikiparser/rules/hardlinebreaks.js
type: application/javascript
module-type: wikirule
Wiki text inline rule for marking areas with hard line breaks. For example:
```
"""
This is some text
That is set like
It is a Poem
When it is
Clearly
Not
"""
```
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "hardlinebreaks";
exports.types = {inline: true};
exports.init = function(parser) {
this.parser = parser;
// Regexp to match
2013-12-14 17:42:50 +00:00
this.matchRegExp = /"""(?:\r?\n)?/mg;
2013-12-14 17:05:57 +00:00
};
exports.parse = function() {
var reEnd = /(""")|(\r?\n)/mg,
tree = [],
match;
2013-12-14 17:05:57 +00:00
// Move past the match
this.parser.pos = this.matchRegExp.lastIndex;
do {
// Parse the run up to the terminator
tree.push.apply(tree,this.parser.parseInlineRun(reEnd,{eatTerminator: false}));
// Redo the terminator match
reEnd.lastIndex = this.parser.pos;
match = reEnd.exec(this.parser.source);
2013-12-14 17:05:57 +00:00
if(match) {
this.parser.pos = reEnd.lastIndex;
// Add a line break if the terminator was a line break
if(match[2]) {
tree.push({type: "element", tag: "br"});
}
}
} while(match && !match[1]);
// Return the nodes
return tree;
};
})();