From 265e7a2da29e6e53e19d5c53404935e33bb47c52 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Tue, 7 Apr 2020 16:16:29 +0100 Subject: [PATCH] Add support for \dir pragma --- core/modules/parsers/wikiparser/rules/dir.js | 51 +++++++++++++++++++ core/modules/parsers/wikiparser/wikiparser.js | 3 +- .../tiddlers/Right-To-Left Languages.tid | 11 ++++ editions/tw5.com/tiddlers/concepts/Pragma.tid | 6 ++- 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 core/modules/parsers/wikiparser/rules/dir.js create mode 100644 editions/tw5.com/tiddlers/Right-To-Left Languages.tid diff --git a/core/modules/parsers/wikiparser/rules/dir.js b/core/modules/parsers/wikiparser/rules/dir.js new file mode 100644 index 000000000..e30fe7a47 --- /dev/null +++ b/core/modules/parsers/wikiparser/rules/dir.js @@ -0,0 +1,51 @@ +/*\ +title: $:/core/modules/parsers/wikiparser/rules/dir.js +type: application/javascript +module-type: wikirule + +Wiki pragma rule for specifying text direction + +``` +\dir rtl +\dir ltr +\dir auto +``` + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.name = "dir"; +exports.types = {pragma: true}; + +/* +Instantiate parse rule +*/ +exports.init = function(parser) { + this.parser = parser; + // Regexp to match + this.matchRegExp = /^\\dir[^\S\n]*(\S+)\r?\n/mg; +}; + +/* +Parse the most recent match +*/ +exports.parse = function() { + var self = this; + // Move past the pragma invocation + this.parser.pos = this.matchRegExp.lastIndex; + // Parse tree nodes to return + return [{ + type: "element", + tag: this.parser.parseAsInline ? "span" : "div", + attributes: { + dir: {type: "string", value: this.match[1]} + }, + children: [] + }]; +}; + +})(); diff --git a/core/modules/parsers/wikiparser/wikiparser.js b/core/modules/parsers/wikiparser/wikiparser.js index 673176038..5ef49b814 100644 --- a/core/modules/parsers/wikiparser/wikiparser.js +++ b/core/modules/parsers/wikiparser/wikiparser.js @@ -27,6 +27,7 @@ Attributes are stored as hashmaps of the following objects: var WikiParser = function(type,text,options) { this.wiki = options.wiki; + this.parseAsInline = options.parseAsInline; var self = this; // Check for an externally linked tiddler if($tw.browser && (text || "") === "" && options._canonical_uri) { @@ -63,7 +64,7 @@ var WikiParser = function(type,text,options) { this.tree = []; var topBranch = this.parsePragmas(); // Parse the text into inline runs or blocks - if(options.parseAsInline) { + if(this.parseAsInline) { topBranch.push.apply(topBranch,this.parseInlineRun()); } else { topBranch.push.apply(topBranch,this.parseBlocks()); diff --git a/editions/tw5.com/tiddlers/Right-To-Left Languages.tid b/editions/tw5.com/tiddlers/Right-To-Left Languages.tid new file mode 100644 index 000000000..67c02cfb8 --- /dev/null +++ b/editions/tw5.com/tiddlers/Right-To-Left Languages.tid @@ -0,0 +1,11 @@ +created: 20200406151830903 +modified: 20200406152521698 +title: Right-To-Left Languages +type: text/vnd.tiddlywiki + +<<.from-version "5.1.22">> The [[language plugins|Languages]] in TiddlyWiki's plugin library apply the appropriate [["right-to-left" setting|https://www.w3.org/International/questions/qa-html-dir]] to the entire document. To set the right to left setting independently for an individual tiddler, use the `\dir` [[pragma|Pragma]] at the top of the tiddler: + +``` +\dir rtl +This text will be displayed with right-to-left formatting +``` diff --git a/editions/tw5.com/tiddlers/concepts/Pragma.tid b/editions/tw5.com/tiddlers/concepts/Pragma.tid index 3adc97954..5e4ff3302 100644 --- a/editions/tw5.com/tiddlers/concepts/Pragma.tid +++ b/editions/tw5.com/tiddlers/concepts/Pragma.tid @@ -1,5 +1,5 @@ created: 20150219175930000 -modified: 20180928145730028 +modified: 20200406151829696 tags: Concepts title: Pragma type: text/vnd.tiddlywiki @@ -18,3 +18,7 @@ The following pragmas are available: : <<.from-version "5.1.15">> Control whether whitespace is trimmed from the start and end of text runs (the default is ''notrim''). This setting can be useful when the whitespace generated by linebreaks disturbs formatting ;`\import ` : <<.from-version "5.1.18">> for importing macro definitions from tiddlers identified by a filter expression +;`\dir ltr` +;`\dir rtl` +;`\dir auto` +: <<.from-version "5.1.22">> for setting the text direction of a tiddler -- see [[Right-To-Left Languages]].