From fe85845c3ce78ff102e411d01873912b3de8705e Mon Sep 17 00:00:00 2001 From: Jermolene Date: Fri, 28 Sep 2018 16:01:32 +0100 Subject: [PATCH] Add new "\import" pragma --- .../parsers/wikiparser/rules/import.js | 53 +++++++++++++++++++ editions/tw5.com/tiddlers/concepts/Pragma.tid | 4 +- .../widgets/ImportVariablesWidget.tid | 12 ++++- 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 core/modules/parsers/wikiparser/rules/import.js diff --git a/core/modules/parsers/wikiparser/rules/import.js b/core/modules/parsers/wikiparser/rules/import.js new file mode 100644 index 000000000..bce5e1ef3 --- /dev/null +++ b/core/modules/parsers/wikiparser/rules/import.js @@ -0,0 +1,53 @@ +/*\ +title: $:/core/modules/parsers/wikiparser/rules/import.js +type: application/javascript +module-type: wikirule + +Wiki pragma rule for importing variable definitions + +``` +\import [[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of]] +``` + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.name = "import"; +exports.types = {pragma: true}; + +/* +Instantiate parse rule +*/ +exports.init = function(parser) { + this.parser = parser; + // Regexp to match + this.matchRegExp = /^\\import[^\S\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 the filter terminated by a line break + var reMatch = /(.*)(\r?\n)|$/mg; + reMatch.lastIndex = this.parser.pos; + var match = reMatch.exec(this.parser.source); + this.parser.pos = reMatch.lastIndex; + // Parse tree nodes to return + return [{ + type: "importvariables", + attributes: { + filter: {type: "string", value: match[1]} + }, + children: [] + }]; +}; + +})(); diff --git a/editions/tw5.com/tiddlers/concepts/Pragma.tid b/editions/tw5.com/tiddlers/concepts/Pragma.tid index cb932dcf8..3adc97954 100644 --- a/editions/tw5.com/tiddlers/concepts/Pragma.tid +++ b/editions/tw5.com/tiddlers/concepts/Pragma.tid @@ -1,5 +1,5 @@ created: 20150219175930000 -modified: 20170712154519026 +modified: 20180928145730028 tags: Concepts title: Pragma type: text/vnd.tiddlywiki @@ -16,3 +16,5 @@ The following pragmas are available: : for adjusting the set of rules used to parse the text ;`\whitespace trim` or `\whitespace notrim` : <<.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 diff --git a/editions/tw5.com/tiddlers/widgets/ImportVariablesWidget.tid b/editions/tw5.com/tiddlers/widgets/ImportVariablesWidget.tid index c95045571..674526e37 100644 --- a/editions/tw5.com/tiddlers/widgets/ImportVariablesWidget.tid +++ b/editions/tw5.com/tiddlers/widgets/ImportVariablesWidget.tid @@ -1,9 +1,9 @@ +caption: importvariables created: 20140612142500000 -modified: 20140612175900970 +modified: 20180928150043777 tags: Widgets title: ImportVariablesWidget type: text/vnd.tiddlywiki -caption: importvariables ! Introduction @@ -31,3 +31,11 @@ So-called global macros are implemented within the main page template ([[$:/core ... ``` + +! `\import` Pragma + +<<.from-version "5.1.18">> The `\import` [[pragma|Pragma]] is an alternative syntax for using the ImportVariablesWidget. For example, the previous example could be expressed as: + +``` +\import [[$:/core/ui/PageMacros]] [all[shadows+tiddlers]tag[$:/tags/Macro]!has[draft.of] +``` \ No newline at end of file