1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-18 10:19:44 +00:00
TiddlyWiki5/core/modules/parsers/wikiparser/rules/run/entity.js
Jeremy Ruston d338a54370 Introduce refactored wiki parser and renderer
This is a half-way through a big refactoring of the parsing and
rendering infrastructure. The main change is to separate the parse and
render trees, which makes the code a lot cleaner. The new parser isn't
yet functional enough to replace the existing parser so for the moment
you have to manually invoke it with `$tw.testNewParser()` in your
browser console. I really ought to use branches for this kind of
thing...
2012-12-13 21:34:31 +00:00

53 lines
1.2 KiB
JavaScript

/*\
title: $:/core/modules/parsers/wikiparser/rules/run/entity.js
type: application/javascript
module-type: wikirunrule
Wiki text run rule for HTML entities. For example:
{{{
This is a copyright symbol: ©
}}}
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var EntityRule = function(parser,startPos) {
// Save state
this.parser = parser;
// Regexp to match
this.reMatch = /(&#?[a-zA-Z0-9]{2,8};)/mg;
// Get the first match
this.matchIndex = startPos-1;
this.findNextMatch(startPos);
};
EntityRule.prototype.findNextMatch = function(startPos) {
if(this.matchIndex !== undefined && startPos > this.matchIndex) {
this.reMatch.lastIndex = startPos;
this.match = this.reMatch.exec(this.parser.source);
this.matchIndex = this.match ? this.match.index : undefined;
}
return this.matchIndex;
};
/*
Parse the most recent match
*/
EntityRule.prototype.parse = function() {
// Get all the details of the match
var entityString = this.match[1];
// Move past the macro call
this.parser.pos = this.reMatch.lastIndex;
// Return the entity
return [{type: "entity", entity: this.match[0]}];
};
exports.EntityRule = EntityRule;
})();