1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-23 13:53:15 +00:00

Add parser rule for hard linebreaks

This commit is contained in:
Jermolene 2013-12-14 17:05:57 +00:00
parent b1e7ba29f1
commit 6a8feb216a
4 changed files with 89 additions and 1 deletions

View File

@ -0,0 +1,60 @@
/*\
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
this.matchRegExp = /"""(?:\r?\n)+/mg;
};
exports.parse = function() {
var reEnd = /(""")|(\r?\n)/mg,
tree = [];
// 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;
var match = reEnd.exec(this.parser.source);
console.log("Match",match,reEnd.lastIndex)
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]);
console.log("Generating",tree)
// Return the nodes
return tree;
};
})();

View File

@ -113,6 +113,15 @@ describe("WikiText parser tests", function() {
});
it("should parse hard linebreak areas", function() {
expect(parse("\"\"\"Something\nin the\nway she moves\n\"\"\"\n\n")).toEqual(
[ { type : 'element', tag : 'p', children : [ { type : 'text', text : 'Something' }, { type : 'element', tag : 'br' }, { type : 'text', text : 'in the' }, { type : 'element', tag : 'br' }, { type : 'text', text : 'way she moves' }, { type : 'element', tag : 'br' } ] } ]
);
});
});
})();

View File

@ -0,0 +1,17 @@
created: 20131214165710101
modified: 20131214170106553
tags: wikitext
title: Hard Linebreaks in WikiText
type: text/vnd.tiddlywiki
The usual handling of [[paragraphs in wikitext|Paragraphs in WikiText]] causes single line breaks to be ignored, and double linebreaks to be interpreted as the end of a paragraph.
This behaviour isn't convenient when dealing with material that incorporates hard linebreaks - for instance, poetry. You can mark a block of content as containing hard line breaks like this:
<<wikitext-example src:'"""
This is a line
and this is a new line
while this is yet another line
and this is the final one
apart from this one
"""'>>

View File

@ -1,5 +1,5 @@
created: 20131205155836435
modified: 20131205155853526
modified: 20131214170445345
tags: wikitext
title: Paragraphs in WikiText
type: text/vnd.tiddlywiki
@ -18,3 +18,5 @@ Single line breaks are ignored within paragraphs. For example:
paragraph made
up of
short lines">>
For situations where this behaviour isn't convenient, you can also use [[Hard Linebreaks in WikiText]].