1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-30 17:23:16 +00:00

Added support for style blocks

This commit is contained in:
Jeremy Ruston 2012-06-14 18:18:32 +01:00
parent fa279514c7
commit 11c9031873
5 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,58 @@
/*\
title: $:/core/modules/parsers/newwikitextparser/rules/styleblock.js
type: application/javascript
module-type: wikitextrule
Wiki text block rule for assigning classes to paragraphs and other blocks. For example:
{{{
{{myClass{
This paragraph will have the CSS class `myClass`.
* The `<ul>` around this list will also have the class `myClass`
* List item 2
}}}
}}}
Note that the opening and closing braces both must be immediately followed by a newline.
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
exports.name = "style";
exports.blockParser = true;
exports.regExpString = "@@[a-zA-Z0-9_\\-]+:.*$";
exports.parse = function(match,isBlock) {
var styles = {},
tree = [],
reStyleSpecififer = /@@([a-zA-Z0-9_\-]+):(.*)((?:\r?\n)?)/mg,
reEndString = "@@",
endMatch;
// Look for the first style specifier
reStyleSpecififer.lastIndex = this.pos;
match = reStyleSpecififer.exec(this.source);
while(match) {
// Save the style specified
styles[match[1].trim()] = match[2].trim();
// Look to see if there is a further style specifier
this.pos = match.index + match[0].length;
reStyleSpecififer.lastIndex = this.pos;
match = reStyleSpecififer.exec(this.source);
}
// Parse until we get to the end marker
tree = this.parseBlocks(reEndString);
for(var t=0; t<tree.length; t++) {
tree[t].addStyles(styles);
}
return tree;
};
})();

View File

@ -158,6 +158,13 @@ Element.prototype.addClass = function(className) {
this.attributes["class"].push(className);
};
Node.prototype.addStyles = function(styles) {
this.attributes.style = this.attributes.style || {};
for(var t in styles) {
this.attributes.style[t] = styles[t];
}
};
exports.Element = Element;
})();

View File

@ -75,6 +75,13 @@ Node.prototype.addClass = function(className) {
};
/*
Add styles to a node
*/
Node.prototype.addStyles = function(styles) {
};
exports.Node = Node;
})();

View File

@ -46,6 +46,7 @@ These are the individual rules that make up WikiText:
* MacroWikiText
* PrettyLinkWikiText
* RuleWikiText
* StyleBlockWikiText
* TypedBlockWikiText
* WikiLinkWikiText

View File

@ -0,0 +1,21 @@
title: StyleBlockWikiText
This syntax enables you to assign arbitrary styles to generated elements. For example:
{{{
@@color:#f00
This is in red!
@@
}}}
Generates the results:
@@color:#f00
This is in red!
@@
The HTML looks like this:
{{{
<p style="color:rgb(255, 0, 0);">This is in red!</p>
}}}