TitleSpaced

Summary: How to have spaces in a page title
Version: n/a
Prerequisites:
Status:
Maintainer:

Question

How can I have spaces in a page title?

Answer

In the template file (*.tmpl) of the skin you are using, replace the variable $Title by $Titlespaced.

If you are using the PmWiki skin, it is a good idea to copy the whole skin into another directory and defined it as your new skin, to avoid deleting your modification at each PmWiki update.

Options

In English, it might be standard to capitalize the first letter of each word in a title, but this is not necessarily the case in others' languages.

In order to leave only the first letter of the title capitalized, you can create and set a new spacing function in the file local/config.php:

function AsSpacedAlt($text) {
  $text = preg_replace("/([[:lower:]\\d])([[:upper:]])/",
          '$1 $2', $text);
  $text = 
    preg_replace('/(?<![-\\d])(\\d+( |$))/',' $1',$text);
  $text =
    preg_replace("/([[:upper:]])([[:upper:]][[:lower:]\\d])/",
                 '$1 $2', $text);
  return $text{0}.strtolower(substr($text,1));
}
$AsSpacedFunction = 'AsSpacedAlt'; 

This doesn't work in UFT8. Try the following code instead:

function AsSpacedUTF8_FirstCapitalizeOnly($text) {
  $text = AsSpacedUTF8($text);
  $text = mb_convert_case($text, MB_CASE_LOWER, "UTF-8");
  $text = mb_ucfirst($text, "UTF-8");
  return $text;
}
function mb_ucfirst($str, $encoding = null) {
     $str = mb_strtoupper(mb_substr($str, 0,1,$encoding), $encoding) . mb_substr($str, 1, mb_strlen($str, $encoding)-1, $encoding);
     return $str;
}
$SpaceWikiWords = 1;
$AsSpacedFunction = 'AsSpacedUTF8_FirstCapitalizeOnly';

Beware that this function applies to all WikiWords if you have chosen to set $SpaceWikiWords = 1; in /config.php or when the directive (:spacewikiwords:) is active.

Note that the function strtolower() is dependant from php localization. Normally, the XLPage takes care to define the locale for you.

Though, if your server doesn't do that properly, you may wish to create your own lowercase function and replace the return line of the above function by:

return $text{0}.strtolower(strtr(substr($text,1),
'&#352;&#381;ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝ&#376;',
'&#353;&#382;àáâãäåçèéêëìíîïñòóôõöøùúûüýÿ'));

Or some more specific one (here for French):

return $text{0}.strtolower(strtr(substr($text,1),
'ÀÁÂÇÈÉÊËÔÖÙÚÛÎÏ','àáâçèéêëôöùúûîï'));

Discussion

Problems

In some cases, the title spacing is not pertinent, e.g. UsageOfPmWiki will became Usage Of Pm Wiki or Usage of pm wiki. Though, this is a question of statistic, if this modification set more good results than wrong (probably yes), you will have less (:title :) directives to use.

Does not seem to support words like "doesn't", and would write "doesnt" as a title. This is a serious problem in French, where there is many apostrophes.

Is it possible to allow the existing capitalization of a link to serve as the title?

Depends on what you mean by 'existing capitalization of a link'. --Pm

Another option: WikiWord as you typed

In some language (like in Hungarian) sometimes we use capital letters inside the WikiWord, but not every word starting by capital. If you put this code to the end of your local/config.php then the link that you typed, the appearing page title and the file name will be the same (in the filename the space will be replaced by underline characters). The small and capital characters remain the same as you typed.

$MakePageNamePatterns = array(
    "/'/" => '',                          # strip single-quotes
    "/[^$PageNameChars\\s_]+/" => '',     # delete non-alnums
    "/\\s+/" => '_'                       # convert spaces to underline
    );
function AsSpacedAsIs($str) { return $str; }
$AsSpacedFunction = "AsSpacedAsIs";
$GroupPattern = '[\\w]*(?:-\\w+)*';
$NamePattern = '[\\w]*(?:-\\w+)*';
function RemoveUnderline($str) { return strtr($str, '_', ' '); }
$FmtPV['$Title'] = $FmtPV['$Titlespaced'] = '@$page["title"] ? $page["title"] : RemoveUnderline($name)';

(by prem)

Another option: title option

In the editing zone, used title option. 'text' is the title as it need to be display. (:title text:)

See also

Contributors

User notes +1: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.