2005.07.11 04:14 PM

FlexWiki Notes - Indented Paragraphs

I sometimes need to indent a paragraph in my FlexWiki content, maybe because it hangs off an item in a list, or just because I want to set it apart, like a quote. Also, depending on where the paragraph appears in the content, I sometimes want to indent it a little more or less. I couldn't see where FlexWiki provided this feature, so I added it.

The changes required for this are similar to the changes made for specifying the starting numbers of ordered lists. One difference is that this feature needs some new mark-up so the FlexWiki engine can indentify indented paragraphs and know how far to indent them. After exploring the existing FlexWiki mark-up and looking at where in the code the mark-up identification would occur (which is important to understanding what kind of mark-up will fall through to where you want to be), I settled on using two dashes ("--") to identify indented paragraphs and the number of tab characters after the dashes to determine the level of indentation.

Here's how the mark-up will look:

This is a normal paragraph.

--This is an indented paragraph.

--        This is a doubly-indented paragraph.

And here's how it will render:

This is a normal paragraph.

    This is an indented paragraph.

        This is a doubly-indented paragraph.

The first change is to the Format method of the FlexWiki.Formatting.Formatter class. In that routine, paragraph identification occurs as a result of falling through all the other possible mark-up conditions. Here's what it looks like:

// As vanilla as can be -- just send it along
each = ProcessLineElements(each);

An indented paragraph is really just a regular paragraph with indentation, so the original logic will simply be prefixed with some additional logic to identify the optional indenting mark-up and establish the level of indentation:

// As vanilla as can be -- just send it along
int numIndents = 0;
if (each.StartsWith("--")) {
  each = each.Substring(2);
  while (each.Length > 0 && (each.StartsWith("\t")))
    each = each.Substring(1);
each = ProcessLineElements(each);

Note that the level of indentation (numIndents) is now passed to the Formatter's private EnsureParaOpen method. This leads us to the next set of changes, which is the addition of an overloaded EnsureParaOpen method that takes an int, and a modification to the existing EnsureParaOpen method to call the new overloaded method:

void EnsureParaOpen()
void EnsureParaOpen(int numIndents)
  if (!paraOpen) { _Output.WriteOpenPara(numIndents); paraOpen = true; }

I overloaded the EnsureParaOpen method instead of replacing it because there are a couple of other places in Formatter's Format method that call it to start paragraphs. However, EnsureParaOpen is the only code that calls the WriteOpenPara method of FlexWiki.Formatting.WikiOutput-derived objects, so I decided to simply replace its abstract WriteOpenPara method signature:

abstract public void WriteOpenPara();

With this one:

abstract public void WriteOpenPara(int numIndents);

Finally, I modified the WriteOpenPara method of the FlexWiki.Formatting.HtmlWikiOutput class, which is the only delivered FlexWiki class inheriting from WikiOutput that overrides WriteOpenPara, to handle the new int parameter:

override public void WriteOpenPara(int numIndents)
  int totalIndent = ((numIndents > 0) ? 25 : 0) + ((numIndents > 1) ? ((numIndents - 1) * 15) : 0);

  Write("<p " + css((totalIndent > 0) ? ("margin-left: " + totalIndent.ToString() + "px") : "") + ">");

Cheesy, you say? Okay, the level of indention should probably be parameterized. And, no, you don't have to use a first-level of indentation that differs from the subsequent levels, which would make the code a little simpler. But, in my defense, this satisfies my requirements and is consistent with the indention levels used by OL and UL LIs in my custom CSS. Anyhow, you've got the source, parameterize your butt off. ;)

Next up, separating the JavaScript from HtmlWikiOutput's WriteScripts method and fixing some FireFox compatibility problems.

FlexWiki Notes - Starting Ordered Lists
FlexWiki Notes - Multiple Namespaces
FlexWiki Notes - Getting Started
FlexWiki Notes


Post a Comment

  (no html)


TrackBack URL:  https://www.typepad.com/services/trackback/6a00d8341c7bd453ef00d83551a6e769e2

Listed below are links to weblogs that reference FlexWiki Notes - Indented Paragraphs: