Hi all.
The txt2tags parser allows a numbered heading syntax. This means that
I need to store the 6-levels of html headings permitted so that I use
the appropriate section,subsection.subsubsection... I've implemented
something that works pretty well within a single formatter:
// HTML doesn't have an inherent number scheme for header tags. Just treat
// them as unnumbered for now.
{
name: 't2tNumberedHeading',
match: '^ *\\+{1,6}[^\\+\n]',
restoreRegExp: /^( *\+{1,6}[^\+\n])/mg,
lookaheadRegExp: /^ *(\+{1,6})([^\+\n][^\n]*[^\+\n])\1[ \t]*$/mg,
handler: function(w)
{
if (!tiddler.numheading) {
tiddler.numheading = [0,0,0,0,0,0];
}
this.lookaheadRegExp.lastIndex = w.matchStart;
var lookaheadMatch = this.lookaheadRegExp.exec(w.source);
if(lookaheadMatch && lookaheadMatch.index == w.matchStart) {
var text = lookaheadMatch[2];
//may not be needed, since \n terminates this markup
anyways...
if(config.browser.isIE)
text = text.replace(/\n/g,"\r");
var headingindex = lookaheadMatch[1].length - 1;
tiddler.numheading[headingindex]++;
//reset the next levels down to 0
for(var i = headingindex + 1; i<=5; i++){
tiddler.numheading[i] = 0;
}
text = " " + text;
for (headingindex; headingindex >= 0; headingindex--) {
text = tiddler.numheading[headingindex] + "." +
text;
}
createTiddlyElement(w.output,'h'+lookaheadMatch[1].length,null,null,text);
w.nextMatch = lookaheadMatch.index +
lookaheadMatch[0].length;
} else {
this.restoreRegExp.lastIndex = w.matchStart;
var restoreMatch = this.restoreRegExp.exec(w.source);
if (restoreMatch && restoreMatch.index == w.matchStart)
{
w.output.appendChild(document.createTextNode(restoreMatch[1]));
} else {
//shouldn't be possible!
//Only if you don't define restoreRegExp:
properly
w.output.appendChild(document.createTextNode("### NUMBER HEADING
SYNTAX ERROR ###"));
}
}
}
},
This formatter works the first time i view a tiddler, but the problem
is tiddler.numheading[]. It is stored with the tiddler between
renderings. This means that when I have a tiddler with 3 headings,
they first show as "1 2 3", but then if I edit the tiddler, the
headings are numbered "4 5 6" they just continue on. The formatter
can't re-set tiddler.numheading[] every time it runs, or else it will
lose the "count" between sections. Any suggestions on a proper
variable to store numheading[]? I think making it a member of tiddler
is giving it too wide a scope. Is there some sort of rendering
object?
--
David Young
--
You received this message because you are subscribed to the Google Groups
"TiddlyWikiDev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/tiddlywikidev?hl=en.