diff --git a/core/modules/parsers/wikiparser/rules/hardlinebreaks.js b/core/modules/parsers/wikiparser/rules/hardlinebreaks.js new file mode 100644 index 000000000..5c1bcdced --- /dev/null +++ b/core/modules/parsers/wikiparser/rules/hardlinebreaks.js @@ -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; +}; + +})(); diff --git a/editions/test/tiddlers/tests/test-wikitext-parser.js b/editions/test/tiddlers/tests/test-wikitext-parser.js index 8565e196e..bc37d70ba 100644 --- a/editions/test/tiddlers/tests/test-wikitext-parser.js +++ b/editions/test/tiddlers/tests/test-wikitext-parser.js @@ -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' } ] } ] + + ); + + }); + }); })(); diff --git a/editions/tw5.com/tiddlers/wikitext/Hard Linebreaks in WikiText.tid b/editions/tw5.com/tiddlers/wikitext/Hard Linebreaks in WikiText.tid new file mode 100644 index 000000000..803939ff8 --- /dev/null +++ b/editions/tw5.com/tiddlers/wikitext/Hard Linebreaks in WikiText.tid @@ -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: + +<> diff --git a/editions/tw5.com/tiddlers/wikitext/Paragraphs in WikiText.tid b/editions/tw5.com/tiddlers/wikitext/Paragraphs in WikiText.tid index 06fbbc94b..7f416a266 100644 --- a/editions/tw5.com/tiddlers/wikitext/Paragraphs in WikiText.tid +++ b/editions/tw5.com/tiddlers/wikitext/Paragraphs in WikiText.tid @@ -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]].