kopia lustrzana https://github.com/miklobit/TiddlyWiki5
Add parser rule for hard linebreaks
rodzic
b1e7ba29f1
commit
6a8feb216a
|
@ -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;
|
||||
};
|
||||
|
||||
})();
|
|
@ -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' } ] } ]
|
||||
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
})();
|
||||
|
|
|
@ -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
|
||||
"""'>>
|
|
@ -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]].
|
||||
|
|
Ładowanie…
Reference in New Issue