From 3bfab9f9da5fa3076c62abaf94aa1503af78c25c Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Sat, 3 Mar 2012 12:29:13 +0000 Subject: [PATCH] Added support for typed blocks in wikitext This allows embedded blocks of another mime type to appear in a tiddler, useful for syntax highlighting fragments of javascript --- js/App.js | 6 ++-- js/WikiTextRules.js | 28 +++++++++++++++++++ readme.md | 14 +++++----- .../tiddlers/TiddlyWikiArchitecture.tid | 8 +++--- tiddlywiki5/tiddlers/TypedBlockTests.tid | 13 +++++++++ 5 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 tiddlywiki5/tiddlers/TypedBlockTests.tid diff --git a/js/App.js b/js/App.js index 081a0d97d..59af86f32 100644 --- a/js/App.js +++ b/js/App.js @@ -27,9 +27,9 @@ var App = function() { this.store = new WikiStore(); // Register the parsers this.store.registerParser("text/x-tiddlywiki",new WikiTextParser({store: this.store})); - this.store.registerParser(["image/svg+xml","image/jpg","image/jpeg","image/png","image/gif"],new ImageParser({store: this.store})); - this.store.registerParser("application/javascript",new JavaScriptParser({store: this.store})); - this.store.registerParser("application/json",new JSONParser({store: this.store})); + this.store.registerParser(["image/svg+xml",".svg","image/jpg",".jpg","image/jpeg",".jpeg","image/png",".png","image/gif",".gif"],new ImageParser({store: this.store})); + this.store.registerParser(["application/javascript",".js"],new JavaScriptParser({store: this.store})); + this.store.registerParser(["application/json",".json"],new JSONParser({store: this.store})); // Register the standard tiddler serializers and deserializers tiddlerInput.register(this.store); tiddlerOutput.register(this.store); diff --git a/js/WikiTextRules.js b/js/WikiTextRules.js index 7f511a90e..636724516 100755 --- a/js/WikiTextRules.js +++ b/js/WikiTextRules.js @@ -397,6 +397,34 @@ var rules = [ } }, +{ + name: "typedBlock", + match: "^\\$\\$\\$(?:.*)\\n", + lookaheadRegExp: /^\$\$\$(.*)\n((?:.|\n)*?)\$\$\$/mg, + handler: function(w) + { + var lookaheadMatch = this.lookaheadRegExp.exec(w.source); + if(lookaheadMatch && lookaheadMatch.index == w.matchStart) { + // The wikitext parsing infrastructure is horribly unre-entrant + var mimeType = lookaheadMatch[1], + content = lookaheadMatch[2], + oldOutput = w.output, + oldSource = w.source, + oldNextMatch = w.nextMatch, + oldChildren = w.children, + oldDependencies = w.dependencies, + parseTree = w.store.parseText(mimeType,content); + w.output = oldOutput; + w.source = oldSource; + w.nextMatch = oldNextMatch; + w.children = oldChildren; + w.dependencies = oldDependencies; + w.output.push.apply(w.output,parseTree.tree); + w.nextMatch = this.lookaheadRegExp.lastIndex; + } + } +}, + { name: "wikifyComment", match: "^(?:/\\*\\*\\*|