smarttitles.pl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/usr/bin/env perl
  2. use strict;
  3. use v5.10;
  4. # ====================[ smarttitles.pl ]====================
  5. =head1 NAME
  6. smarttitles - An Oddmuse module for embedding user-readable titles and subtitles
  7. for a page in the content for that page.
  8. =head1 INSTALLATION
  9. smarttitles is easily installable: move this file into the B<wiki/modules/>
  10. directory of your Oddmuse Wiki.
  11. =cut
  12. AddModuleDescription('smarttitles.pl', 'Smarttitles Extension');
  13. our (%Page, $SiteName, @MyRules, %RuleOrder);
  14. # ....................{ CONFIGURATION }....................
  15. =head1 CONFIGURATION
  16. smarttitles is easily configurable; set these variables in the B<wiki/config.pl>
  17. file for your Oddmuse Wiki.
  18. =cut
  19. our ($SmartTitlesBrowserTitle,
  20. $SmartTitlesBrowserTitleWithoutSubtitle,
  21. $SmartTitlesSubUrlText);
  22. =head2 $SmartTitlesBrowserTitle
  23. The browser title for pages having a subtitle. The browser title is the string
  24. displayed in your browser's titlebar for each page.
  25. smarttitles performs variable substitution on this string, as follows:
  26. =over
  27. =item The first '%s' in this string, if present, is replaced with the Wiki's
  28. C<$SiteName>.
  29. =item The second '%s' in this string, if present, is replaced with this Wiki
  30. page's title.
  31. =item The third '%s' in this string, if present, is replaced with this Wiki
  32. page's subtitle.
  33. =back
  34. =cut
  35. $SmartTitlesBrowserTitle = '%s: %s (%s)';
  36. =head2 $SmartTitlesBrowserTitleWithoutSubtitle
  37. The browser title for pages lacking a subtitle.
  38. smarttitles performs variable substitution on this string, as follows:
  39. =over
  40. =item The first '%s' in this string, if present, is replaced with the Wiki's
  41. C<$SiteName>.
  42. =item The second '%s' in this string, if present, is replaced with this Wiki
  43. page's title.
  44. =back
  45. =cut
  46. $SmartTitlesBrowserTitleWithoutSubtitle = '%s: %s';
  47. $SmartTitlesSubUrlText = undef;
  48. # ....................{ RULES }....................
  49. push(@MyRules, \&SmartTitlesRule);
  50. # "#TITLE" and "#SUBTITLE" conflict with Creole-style numbered lists, and
  51. # "poetry.pl"-style poetry blocks; as such, rules filtering these strings should
  52. # be applied before rules filtering Creole- and "poetry.pl"-style strings. As it
  53. # is likely that these rules, also, conflict with other Oddmuse markup modules,
  54. # this module requests that these rules be applied with high priority -
  55. # presumably before another module is permitted to muck them up.
  56. $RuleOrder{\&SmartTitlesRule} = -50;
  57. =head2 SmartTitlesRule
  58. Strips "#TITLE ...\n" and "#SUBTITLE ...\n" text from the current Wiki page.
  59. Since GetHeaderSmartTitles() already parses this text, it serves little use past
  60. that point.
  61. =cut
  62. sub SmartTitlesRule {
  63. return '' if m/\G (^|\n)? \#(TITLE|SUBTITLE|SUBURL) [ \t]+ (.*?) \s*(\n+|$) /cgx;
  64. return;
  65. }
  66. # ....................{ FUNCTIONS }....................
  67. *GetHeaderSmartTitlesOld = \&GetHeader;
  68. *GetHeader = \&GetHeaderSmartTitles;
  69. =head2 GetSmartTitles
  70. Returns the title and subtitle for this page. (Presumably, this page has been
  71. opened with an earlier call to C<OpenPage()>.)
  72. This function is provided as a separate subroutine so as to permit other
  73. extensions (namely, hibernal) to obtain the title and subtitle for pages.
  74. =cut
  75. sub GetSmartTitles {
  76. my ($title) = $Page{text} =~ m/ (?:^|\n) \#TITLE [ \t]+ (.*?) \s*\n+ /x;
  77. my ($subtitle) = $Page{text} =~ m/ (?:^|\n) \#SUBTITLE [ \t]+ (.*?) \s*\n+ /x;
  78. my ($suburl) = $Page{text} =~ m/ (?:^|\n) \#SUBURL [ \t]+ (.*?) \s*\n+ /x;
  79. return ($title, $subtitle, $suburl);
  80. }
  81. =head2 GetHeaderSmartTitles
  82. Changes the passed page's HTML header to reflect any "#TITLE" or "#SUBTITLE"
  83. within that passed page's Wiki content.
  84. =cut
  85. sub GetHeaderSmartTitles {
  86. my ($page_name, $title, undef, undef, undef, undef, $subtitle) = @_;
  87. my ($smart_title, $smart_subtitle, $smart_suburl);
  88. my $html_header = GetHeaderSmartTitlesOld(@_);
  89. if ($page_name) {
  90. OpenPage($page_name);
  91. $title = NormalToFree($title);
  92. ($smart_title, $smart_subtitle, $smart_suburl) = GetSmartTitles();
  93. }
  94. $smart_title ||= $title;
  95. $smart_subtitle ||= $subtitle;
  96. $smart_title = QuoteHtml($smart_title);
  97. $smart_subtitle = QuoteHtml($smart_subtitle);
  98. $smart_suburl = QuoteHtml($smart_suburl);
  99. $html_header =~ s~\Q>$title</a>\E~>$smart_title</a>~g;
  100. if ($smart_subtitle) {
  101. my $subtitlehtml = '<p class="subtitle">' . $smart_subtitle;
  102. if ($smart_suburl) {
  103. # ApplyRules is too much, we just want links. LinkRules should be enough.
  104. # $subtitlehtml .= ' ' . ToString(sub { ApplyRules($smart_suburl, 1, 1) }) if $smart_suburl;
  105. $_ = $smart_suburl;
  106. $subtitlehtml .= ' ' . ToString(sub {LinkRules(1)});
  107. }
  108. $html_header =~ s~\Q</h1>\E~</h1>$subtitlehtml</p>~;
  109. }
  110. my $smart_header;
  111. if ($SiteName eq $smart_title) { # show "MySite: subtitle" instead of "MySite: MySite (subtitle)"
  112. $smart_header = $smart_subtitle
  113. ? sprintf($SmartTitlesBrowserTitleWithoutSubtitle, $SiteName, $smart_subtitle)
  114. : $SiteName;
  115. } else {
  116. $smart_header = $smart_subtitle
  117. ? sprintf($SmartTitlesBrowserTitle, $SiteName, $smart_title, $smart_subtitle)
  118. : sprintf($SmartTitlesBrowserTitleWithoutSubtitle, $SiteName, $smart_title);
  119. }
  120. $html_header =~ s~\<title\>.*?\<\/title\>~<title>$smart_header</title>~;
  121. return $html_header;
  122. }
  123. =head1 COPYRIGHT AND LICENSE
  124. The information below applies to everything in this distribution,
  125. except where noted.
  126. Copyright 2014-2015 Alex-Daniel Jakimenko <alex.jakimenko@gmail.com>
  127. Copyleft 2008 by B.w.Curry <http://www.raiazome.com>.
  128. Copyright 2006 by Charles Mauch <mailto://cmauch@gmail.com>.
  129. This file is free software; you can redistribute it and/or
  130. modify it under the terms of the GNU General Public License
  131. as published by the Free Software Foundation; either version 2
  132. of the License, or (at your option) any later version.
  133. This file is distributed in the hope that it will be useful,
  134. but WITHOUT ANY WARRANTY; without even the implied warranty of
  135. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  136. GNU General Public License for more details.
  137. You should have received a copy of the GNU General Public License
  138. along with this file; if not, write to the Free Software
  139. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  140. =cut