2008-09-22 10:08:23 +00:00
|
|
|
#!/usr/bin/env perl
|
2015-03-27 03:01:01 +02:00
|
|
|
use strict;
|
2015-08-18 10:48:03 +02:00
|
|
|
use v5.10;
|
2015-03-27 03:01:01 +02:00
|
|
|
|
2008-09-22 10:08:23 +00:00
|
|
|
# ====================[ smarttitles.pl ]====================
|
2006-09-18 15:54:13 +00:00
|
|
|
|
2008-09-22 10:08:23 +00:00
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
|
|
smarttitles - An Oddmuse module for embedding user-readable titles and subtitles
|
|
|
|
|
for a page in the content for that page.
|
|
|
|
|
|
|
|
|
|
=head1 INSTALLATION
|
|
|
|
|
|
|
|
|
|
smarttitles is easily installable: move this file into the B<wiki/modules/>
|
|
|
|
|
directory of your Oddmuse Wiki.
|
|
|
|
|
|
|
|
|
|
=cut
|
2014-08-21 22:23:23 +02:00
|
|
|
AddModuleDescription('smarttitles.pl', 'Smarttitles Extension');
|
2008-09-22 10:08:23 +00:00
|
|
|
|
2015-04-10 13:31:28 +03:00
|
|
|
our (%Page, $SiteName, @MyRules, %RuleOrder);
|
2015-03-27 03:01:01 +02:00
|
|
|
|
2008-09-22 10:08:23 +00:00
|
|
|
# ....................{ CONFIGURATION }....................
|
|
|
|
|
|
|
|
|
|
=head1 CONFIGURATION
|
|
|
|
|
|
|
|
|
|
smarttitles is easily configurable; set these variables in the B<wiki/config.pl>
|
|
|
|
|
file for your Oddmuse Wiki.
|
|
|
|
|
|
|
|
|
|
=cut
|
2015-04-10 13:31:28 +03:00
|
|
|
our ($SmartTitlesBrowserTitle,
|
2015-09-11 00:37:19 +03:00
|
|
|
$SmartTitlesBrowserTitleWithoutSubtitle,
|
|
|
|
|
$SmartTitlesSubUrlText);
|
2008-09-22 10:08:23 +00:00
|
|
|
|
|
|
|
|
=head2 $SmartTitlesBrowserTitle
|
|
|
|
|
|
2008-09-22 10:59:15 +00:00
|
|
|
The browser title for pages having a subtitle. The browser title is the string
|
|
|
|
|
displayed in your browser's titlebar for each page.
|
2008-09-22 10:08:23 +00:00
|
|
|
|
2008-09-22 10:59:15 +00:00
|
|
|
smarttitles performs variable substitution on this string, as follows:
|
|
|
|
|
|
|
|
|
|
=over
|
|
|
|
|
|
|
|
|
|
=item The first '%s' in this string, if present, is replaced with the Wiki's
|
|
|
|
|
C<$SiteName>.
|
|
|
|
|
|
|
|
|
|
=item The second '%s' in this string, if present, is replaced with this Wiki
|
|
|
|
|
page's title.
|
|
|
|
|
|
|
|
|
|
=item The third '%s' in this string, if present, is replaced with this Wiki
|
|
|
|
|
page's subtitle.
|
|
|
|
|
|
|
|
|
|
=back
|
2008-09-22 10:08:23 +00:00
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
$SmartTitlesBrowserTitle = '%s: %s (%s)';
|
|
|
|
|
|
|
|
|
|
=head2 $SmartTitlesBrowserTitleWithoutSubtitle
|
|
|
|
|
|
|
|
|
|
The browser title for pages lacking a subtitle.
|
|
|
|
|
|
2008-09-22 10:59:15 +00:00
|
|
|
smarttitles performs variable substitution on this string, as follows:
|
|
|
|
|
|
|
|
|
|
=over
|
|
|
|
|
|
|
|
|
|
=item The first '%s' in this string, if present, is replaced with the Wiki's
|
|
|
|
|
C<$SiteName>.
|
|
|
|
|
|
|
|
|
|
=item The second '%s' in this string, if present, is replaced with this Wiki
|
|
|
|
|
page's title.
|
|
|
|
|
|
|
|
|
|
=back
|
2008-09-22 10:08:23 +00:00
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
$SmartTitlesBrowserTitleWithoutSubtitle = '%s: %s';
|
|
|
|
|
|
2014-08-23 09:04:12 +03:00
|
|
|
$SmartTitlesSubUrlText = undef;
|
|
|
|
|
|
2008-09-22 10:08:23 +00:00
|
|
|
# ....................{ RULES }....................
|
|
|
|
|
push(@MyRules, \&SmartTitlesRule);
|
|
|
|
|
|
|
|
|
|
# "#TITLE" and "#SUBTITLE" conflict with Creole-style numbered lists, and
|
|
|
|
|
# "poetry.pl"-style poetry blocks; as such, rules filtering these strings should
|
|
|
|
|
# be applied before rules filtering Creole- and "poetry.pl"-style strings. As it
|
|
|
|
|
# is likely that these rules, also, conflict with other Oddmuse markup modules,
|
|
|
|
|
# this module requests that these rules be applied with high priority -
|
|
|
|
|
# presumably before another module is permitted to muck them up.
|
|
|
|
|
$RuleOrder{\&SmartTitlesRule} = -50;
|
|
|
|
|
|
|
|
|
|
=head2 SmartTitlesRule
|
|
|
|
|
|
|
|
|
|
Strips "#TITLE ...\n" and "#SUBTITLE ...\n" text from the current Wiki page.
|
|
|
|
|
Since GetHeaderSmartTitles() already parses this text, it serves little use past
|
|
|
|
|
that point.
|
|
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
sub SmartTitlesRule {
|
2015-09-11 02:55:18 +03:00
|
|
|
return '' if m/\G (^|\n)? \#(TITLE|SUBTITLE|SUBURL) [ \t]+ (.*?) \s*(\n+|$) /cgx;
|
2015-02-27 12:10:18 +02:00
|
|
|
return;
|
2006-10-01 04:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
2008-09-22 10:08:23 +00:00
|
|
|
# ....................{ FUNCTIONS }....................
|
2015-04-11 23:41:33 +03:00
|
|
|
*GetHeaderSmartTitlesOld = \&GetHeader;
|
|
|
|
|
*GetHeader = \&GetHeaderSmartTitles;
|
2008-09-22 10:08:23 +00:00
|
|
|
|
|
|
|
|
=head2 GetSmartTitles
|
2006-09-18 15:54:13 +00:00
|
|
|
|
2008-09-22 10:08:23 +00:00
|
|
|
Returns the title and subtitle for this page. (Presumably, this page has been
|
|
|
|
|
opened with an earlier call to C<OpenPage()>.)
|
|
|
|
|
|
|
|
|
|
This function is provided as a separate subroutine so as to permit other
|
|
|
|
|
extensions (namely, hibernal) to obtain the title and subtitle for pages.
|
|
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
sub GetSmartTitles {
|
2015-09-11 00:37:19 +03:00
|
|
|
my ($title) = $Page{text} =~ m/ (?:^|\n) \#TITLE [ \t]+ (.*?) \s*\n+ /x;
|
|
|
|
|
my ($subtitle) = $Page{text} =~ m/ (?:^|\n) \#SUBTITLE [ \t]+ (.*?) \s*\n+ /x;
|
|
|
|
|
my ($suburl) = $Page{text} =~ m/ (?:^|\n) \#SUBURL [ \t]+ (.*?) \s*\n+ /x;
|
|
|
|
|
return ($title, $subtitle, $suburl);
|
2008-09-22 10:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
=head2 GetHeaderSmartTitles
|
|
|
|
|
|
|
|
|
|
Changes the passed page's HTML header to reflect any "#TITLE" or "#SUBTITLE"
|
|
|
|
|
within that passed page's Wiki content.
|
|
|
|
|
|
|
|
|
|
=cut
|
2014-08-22 22:01:50 +03:00
|
|
|
|
2008-09-22 10:08:23 +00:00
|
|
|
sub GetHeaderSmartTitles {
|
|
|
|
|
my ($page_name, $title, undef, undef, undef, undef, $subtitle) = @_;
|
2015-09-11 00:37:19 +03:00
|
|
|
my ($smart_title, $smart_subtitle, $smart_suburl);
|
2008-09-22 10:08:23 +00:00
|
|
|
my $html_header = GetHeaderSmartTitlesOld(@_);
|
|
|
|
|
|
|
|
|
|
if ($page_name) {
|
|
|
|
|
OpenPage($page_name);
|
2006-09-22 04:36:00 +00:00
|
|
|
$title = NormalToFree($title);
|
2015-09-11 00:37:19 +03:00
|
|
|
($smart_title, $smart_subtitle, $smart_suburl) = GetSmartTitles();
|
2008-09-22 10:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
2014-08-22 22:01:50 +03:00
|
|
|
$smart_title ||= $title;
|
|
|
|
|
$smart_subtitle ||= $subtitle;
|
2006-09-18 16:07:39 +00:00
|
|
|
|
2015-09-11 00:37:19 +03:00
|
|
|
$smart_title = QuoteHtml($smart_title);
|
2014-08-22 22:01:50 +03:00
|
|
|
$smart_subtitle = QuoteHtml($smart_subtitle);
|
2015-09-11 00:37:19 +03:00
|
|
|
$smart_suburl = QuoteHtml($smart_suburl);
|
2008-09-22 10:08:23 +00:00
|
|
|
|
2014-08-22 22:01:50 +03:00
|
|
|
$html_header =~ s~\Q>$title</a>\E~>$smart_title</a>~g;
|
2014-08-23 09:04:12 +03:00
|
|
|
if ($smart_subtitle) {
|
|
|
|
|
my $subtitlehtml = '<p class="subtitle">' . $smart_subtitle;
|
2014-08-24 06:33:50 +03:00
|
|
|
if ($smart_suburl) {
|
2015-09-11 00:37:19 +03:00
|
|
|
# ApplyRules is too much, we just want links. LinkRules should be enough.
|
|
|
|
|
# $subtitlehtml .= ' ' . ToString(sub { ApplyRules($smart_suburl, 1, 1) }) if $smart_suburl;
|
|
|
|
|
$_ = $smart_suburl;
|
|
|
|
|
$subtitlehtml .= ' ' . ToString(sub {LinkRules(1)});
|
2014-08-24 06:33:50 +03:00
|
|
|
}
|
2014-08-23 09:04:12 +03:00
|
|
|
$html_header =~ s~\Q</h1>\E~</h1>$subtitlehtml</p>~;
|
|
|
|
|
}
|
2008-09-22 10:08:23 +00:00
|
|
|
|
2014-08-22 22:01:50 +03:00
|
|
|
my $smart_header;
|
|
|
|
|
if ($SiteName eq $smart_title) { # show "MySite: subtitle" instead of "MySite: MySite (subtitle)"
|
|
|
|
|
$smart_header = $smart_subtitle
|
|
|
|
|
? sprintf($SmartTitlesBrowserTitleWithoutSubtitle, $SiteName, $smart_subtitle)
|
|
|
|
|
: $SiteName;
|
|
|
|
|
} else {
|
|
|
|
|
$smart_header = $smart_subtitle
|
|
|
|
|
? sprintf($SmartTitlesBrowserTitle, $SiteName, $smart_title, $smart_subtitle)
|
|
|
|
|
: sprintf($SmartTitlesBrowserTitleWithoutSubtitle, $SiteName, $smart_title);
|
2008-09-22 10:08:23 +00:00
|
|
|
}
|
2014-08-22 22:01:50 +03:00
|
|
|
$html_header =~ s~\<title\>.*?\<\/title\>~<title>$smart_header</title>~;
|
2008-09-22 10:08:23 +00:00
|
|
|
|
|
|
|
|
return $html_header;
|
2006-09-18 15:54:13 +00:00
|
|
|
}
|
2008-09-22 10:08:23 +00:00
|
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE
|
|
|
|
|
|
|
|
|
|
The information below applies to everything in this distribution,
|
|
|
|
|
except where noted.
|
|
|
|
|
|
2015-09-11 00:37:19 +03:00
|
|
|
Copyright 2014-2015 Alex-Daniel Jakimenko <alex.jakimenko@gmail.com>
|
2008-09-22 10:08:23 +00:00
|
|
|
Copyleft 2008 by B.w.Curry <http://www.raiazome.com>.
|
|
|
|
|
Copyright 2006 by Charles Mauch <mailto://cmauch@gmail.com>.
|
|
|
|
|
|
2016-08-16 14:59:13 +02:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
2008-09-22 10:08:23 +00:00
|
|
|
|
2016-08-16 14:59:13 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
2008-09-22 10:08:23 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2016-08-16 14:59:13 +02:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-09-22 10:08:23 +00:00
|
|
|
|
|
|
|
|
=cut
|