[Home]WikiPatches/UnderlineTag

UseModWiki | WikiPatches | RecentChanges | Preferences

General description

In the usenet news, and similar environments where unformatted text is used, the underscore is often used to simulate underlining. I thought it would be nice to have it in UseMod too. However there is not an HTML tag for underlining as there is for bold and italics. That's why this patch uses the cascading style sheets for rendering.

Syntax

Examples

These work:
*The last word in the sentence is _underlined_.
*_Two_ words are underlined on a _single_ row.
*_Many words are underlined._
*Let's try a _Word_With_Underscores_. Sometimes words __begin_ with an underscore.
*This complex _example is more_ __or__ _less ridiculous_.

And are rendered as:

These don't work:

*_No _enclosed_ underlinings._
*A word character must_ follow and precede _the tag pair.

They are rendered as:

Changes to the code

New global variable

First change is to add a global variable $WordChar. It will be later defined to contain all the characters that a word can contain. In other words all the characters that are not punctuation, white space nor special characters.

Here's the diff for 0.92:

48c48
<   $UserGotoBar);
---
>   $UserGotoBar $WordChar);

Diff for 1.0:

56c56
<   $EditNameLink $UseMetaWiki @ImageSites $BracketImg );
---
>   $EditNameLink $UseMetaWiki @ImageSites $BracketImg $WordChar);

And the same with some context for 0.92:

--------------------------
  # Configuration/constant variables:
  use vars qw(@RcDays @HtmlPairs @HtmlSingle
    $TempDir $LockDir $DataDir $HtmlDir $UserDir $KeepDir $PageDir
    ... (lines deleted)
    $FooterNote $EditNote $MaxPost $NewText $NotifyDefault $HttpCharset
-   $UserGotoBar);
+   $UserGotoBar $WordChar);
  # Note: $NotifyDefault is kept because it was a config variable in 0.90
  # Other global variables:
  use vars qw(%Page %Section %Text %InterSite %SaveUrl %SaveNumUrl
--------------------------

Or for 1.0:

--------------------------
   @IsbnNames @IsbnPre @IsbnPost $EmailFile $FavIcon $RssDays $UserHeader
   $UserBody $StartUID $ParseParas $AuthorFooter $UseUpload $AllUpload
   $UploadDir $UploadUrl $LimitFileUrl $MaintTrimRc $SearchButton 
-  $EditNameLink $UseMetaWiki @ImageSites $BracketImg );
+  $EditNameLink $UseMetaWiki @ImageSites $BracketImg $WordChar);
 # Note: $NotifyDefault is kept because it was a config variable in 0.90
 # Other global variables:
 use vars qw(%Page %Section %Text %InterSite %SaveUrl %SaveNumUrl
--------------------------

Then the new variable is initialized to an empty string so that it wont be used unitialized by accident.

86a87
> $WordChar    = "";              # List of word characters for underline matching

It's together with other configuration.

--------------------------
  # Default configuration (used if UseConfig is 0)
  $CookieName  = "Wiki";          # Name for this wiki (for multi-wiki sites)
    ... (lines deleted)
  $UserGotoBar = "";              # HTML added to end of goto bar
+ $WordChar    = "";              # List of word characters for underline matching

  # Major options:
--------------------------

In 1.0:

--------------------------
 $UploadDir   = '';              # Full path (like /foo/www/uploads) for files
 $UploadUrl   = '';              # Full URL (like http://foo.com/uploads)
 @ImageSites  = qw();            # Url prefixes of good image sites: ()=all
+$WordChar    = "";              # List of word characters for underline matching
 
 # Major options:
 $UseSubpage  = 1;           # 1 = use subpages,       0 = do not use subpages
--------------------------

Set the new variable $WordChar

The $WordChar variable is set in the InitLinkPatterns function. A localized letter pattern is there at hand so I grabbed to the opportunity. (Maybe it's a bad idea.)

179a181
>   $WordChar = $AnyLetter . '_0-9]'; # For underline

The new line goes just after the decision to use the English letters or the non-English letters and just before the check for simple links. Since the value of $WordChar should not depend on the $SimpleLinks variable, the numbers and the underscore must be concatenated separately to the string.

--------------------------
    if ($NonEnglish) {
      $UpperLetter .= "\xc0-\xde";
      $LowerLetter .= "\xdf-\xff";
      $AnyLetter   .= "\xc0-\xff";
    }
+   $WordChar = $AnyLetter . '_0-9]'; # For underline
    if (!$SimpleLinks) {
      $AnyLetter .= "_0-9";
    }
    $UpperLetter .= "]"; $LowerLetter .= "]"; $AnyLetter .= "]";
--------------------------

Context in 1.0:

--------------------------
    if ($NonEnglish) {
      $UpperLetter .= "\xc0-\xde";
      $LowerLetter .= "\xdf-\xff";
      if ($NewFS) {
        $AnyLetter   .= "\x80-\xff";
      } else {
        $AnyLetter   .= "\xc0-\xff";
      }
    }
+   $WordChar = $AnyLetter . '_0-9]'; # For underline
    if (!$SimpleLinks) {
      $AnyLetter .= "_0-9";
    }
    $UpperLetter .= "]"; $LowerLetter .= "]"; $AnyLetter .= "]";
    # Main link pattern: lowercase between uppercase, then anything
    $LpA = $UpperLetter . "+" . $LowerLetter . "+" . $UpperLetter
           . $AnyLetter . "*";
    # Optional subpage link pattern: uppercase, lowercase, then anything
    $LpB = $UpperLetter . "+" . $LowerLetter . "+" . $AnyLetter . "*";
--------------------------

The result is a character group for the regular expression as the string is closed with ] (a closing bracket?).

Parse the underline tags

The parsing goes to the CommonMarkup function.
1235a1238
>     s/(?<!$WordChar)_(.*?)_(?!$WordChar)/<span class=wikiunderline>$1<\/span>/g;  # Underline

The new row is in the block where the single quote tags (for bold and italics) and equal sign heading tags are parsed. The parsing is accomplished in the same manner: the shortest string is matched so that many tag pairs may appear on a single row. It also means that both the opening and closing tags must always be on the same row.

--------------------------
    if ($doLines) { # 0 = no line-oriented, 1 or 2 = do line-oriented
      # The quote markup patterns avoid overlapping tags (with 5 quotes)
      # by matching the inner quotes for the strong pattern.
      s/('*)'''(.*?)'''/$1<strong>$2<\/strong>/g;
      s/''(.*?)''/<em>$1<\/em>/g;
+     s/(?<!$WordChar)_(.*?)_(?!$WordChar)/<span class=wikiunderline>$1<\/span>/g;  # Underline
      if ($UseHeadings) {
        s/(^|\n)\s*(\=+)\s+([^\n]+)\s+\=+/&WikiHeading($1, $2, $3)/geo;
      }
    }
--------------------------

The regular expression pattern checks that a non-word character is immediately before and after the underline tags (the underscores). This way the underscores inside a word wont mangle the parser.

Cascading style sheet

As there is not an HTML tag for underlining the Wiki style underline tags have to be rendered with the CSS. First of all the CSS support for UseMod must be set on: the location of the style sheet file must be written to the $StyleSheet configuration variable. Then put or add the following to the file:

.wikiunderline {
	text-decoration: underline;
}


Comments

There is an HTML tag: <u>. It's probably deprecated though.

Have you tested what happens with underlined links? For example: _WikiPatches/UnderlineTag . I've done similar code but found there are sensitivities as to what formatting is done first.


UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited May 22, 2009 4:04 pm by GunnarH (diff)
Search: