source: trunk/ExplicitNumberingPlugin/lib/TWiki/Plugins/ExplicitNumberingPlugin.pm @ 1050

Revision 1050, 4.8 KB checked in by OlivierRaginel, 4 years ago (diff)

Item329: Fixed tagline

Line 
1# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
2#
3# Copyright (C) 2000-2003 Andrea Sterbini, a.sterbini@flashnet.it
4# Copyright (C) 2001-2003 Peter Thoeny, peter@thoeny.com
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License
8# as published by the Free Software Foundation; either version 2
9# of the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details, published at
15# http://www.gnu.org/copyleft/gpl.html
16#
17# =========================
18#
19
20# =========================
21package TWiki::Plugins::ExplicitNumberingPlugin; 
22
23# =========================
24use vars qw(
25        $web $topic $user $installWeb $VERSION $RELEASE $pluginName
26        $debug
27    );
28
29# This should always be $Rev: 12029 $ so that TWiki can determine the checked-in
30# status of the plugin. It is used by the build automation tools, so
31# you should leave it alone.
32$VERSION = '$Rev: 12029 $';
33
34# This is a free-form string you can use to "name" your own plugin version.
35# It is *not* used by the build automation tools, but is reported as part
36# of the version number in PLUGINDESCRIPTIONS.
37$RELEASE = 'Dakar';
38
39$pluginName = 'ExplicitNumberingPlugin';  # Name of this Plugin
40
41
42my $maxLevels = 6;              # Maximum number of levels
43my %Sequences;                  # Numberings, addressed by the numbering name
44my $lastLevel = $maxLevels - 1; # Makes the code more readable
45my @alphabet = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
46
47# =========================
48sub initPlugin
49{
50    ( $topic, $web, $user, $installWeb ) = @_;
51
52    # check for Plugins.pm versions
53    if( $TWiki::Plugins::VERSION < 1 ) {
54        TWiki::Func::writeWarning( "Version mismatch between $pluginName and Plugins.pm" );
55        return 0;
56    }
57
58
59    $debug = TWiki::Func::getPreferencesFlag( "\U$pluginName\E_DEBUG" );
60
61    # Plugin correctly initialized
62    ##TWiki::Func::writeDebug( "- TWiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK" ) if $debug;
63
64    return 1;
65}
66
67# =========================
68# Need to move =makeExplicitNumber= into =commonTagsHandler= to support
69# auto-numbering of heading levels, otherwise the TOC lines will have
70# different number than the heading line (must be done before TOC).
71
72sub commonTagsHandler
73{
74### my ( $text ) = @_;   # do not uncomment, use $_[0] instead
75
76    ##TWiki::Func::writeDebug( "- ${pluginName}::commonTagsHandler( $web.$topic )" ) if $debug;
77
78    return if $_[3];   # Called in an include; do not number yet.
79
80    %Sequences = ();
81
82    $_[0] =~ s/\-\-\-(\#\#*) /&makeHeading(length($1))/geo;
83    $_[0] =~ s/\#\#(\w+\#)?([0-9]+)?\.(\.*)([a-z]?)/&makeExplicitNumber($1,$2,length($3),$4)/geo;
84}
85
86# =========================
87
88sub makeHeading {
89    my $headerlvl = shift || 0;
90    my $headerlevel = ($headerlvl)?'---':'';
91    my $numlevel = '##';
92    for (my $i=0;$i<$headerlvl;$i++) {
93      $headerlevel .= '+';
94      $numlevel .= '.';
95    }
96    return $headerlevel . $numlevel . ' ';
97}
98
99# Build the explicit outline number
100sub makeExplicitNumber
101{
102
103    ##TWiki::Func::writeDebug( "- ${pluginName}::makeExplicitNumber( $_[0], $_[1], $_[2], $_[3] )" ) if $debug;
104
105    my $name = '-default-';
106    my $init = '';
107    my $level = $_[2];
108    my $alist = '';
109    $name = $_[0] if defined $_[0];
110    $init = $_[1] if defined $_[1];
111    $alist = $_[3] if defined $_[3];
112    if ( $alist ne '' ) {
113        $level++;
114    }
115
116    my $text = '';
117
118    #...Truncate the level count to maximum allowed
119    if ($level > $lastLevel) { $level = $lastLevel; }
120
121    #...Initialize a new, or get the current, numbering from the Sequences
122    my @Numbering = ();
123    if ( ! defined( $Sequences{$name} ) ) {
124        for $i ( 0 .. $lastLevel ) { $Numbering[$i] = 0; }
125    } else {
126        @Numbering = split(':', $Sequences{$name} );
127        #...Re-initialize the sequence
128        if ( defined $_[1] ) {
129          $init = (int $init);
130          if ( $init ) {
131            $Numbering[$level] = $init - 1;
132          } else {
133            for $i ( 0 .. $lastLevel ) { $Numbering[$i] = 0; }
134        }
135        }
136    }
137
138    #...Increase current level number
139    $Numbering[ $level ] += 1;
140
141    #...Reset all higher level counts
142    if ( $level < $lastLevel ) {
143        for $i ( ($level+1) .. $lastLevel ) { $Numbering[$i] = 0; }
144    }
145
146    #...Save the altered numbering
147    $Sequences{$name} =  join( ':', @Numbering );
148
149    #...Construct the number
150    if ( $alist eq '' ) {
151        for $i ( 0 .. $level ) {
152            $text .= "$Numbering[$i]";
153            $text .= '.' if ( $i < $level );
154        }
155    } else {
156        #...Level is 1-origin, indexing is 0-origin
157        $text .= $alphabet[$Numbering[$level]-1]
158    }
159
160    return $text;
161}
162
163# =========================
164
1651;
Note: See TracBrowser for help on using the repository browser.