Skip to content

Commit

Permalink
Item10529: toc plugin modified to use book generated by bookmaker, an…
Browse files Browse the repository at this point in the history
…d bookmaker now supports all existing formats of tocplugin. One interesting effect of this was that tocplugin now supports topics spread across different webs.

git-svn-id: http://svn.foswiki.org/trunk/TocPlugin@11199 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
CrawfordCurrie authored and CrawfordCurrie committed Mar 24, 2011
1 parent 02f5838 commit 7e8123a
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 75 deletions.
229 changes: 213 additions & 16 deletions data/System/TocPlugin.txt
@@ -1,15 +1,15 @@
%META:TOPICINFO{author="WolfgangDenk" date="1271416286" format="1.1" version="1.2"}%
---++ <nop>%TOPIC%

<!--
* Set SHORTDESCRIPTION = %$SHORTDESCRIPTION%
-->
%SHORTDESCRIPTION%

This plugin adds powerful support for generation and maintenance of tables
of contents and cross-references *across multiple topics*.

---+++ Syntax Rules
%TOC%

Adds a number of new tags:
Supports the generation of tables of contents, together with symbolic
cross-references that operate _within_ and _between_ topics.

Adds a number of new macros:
* %<nop>SECTIONn% - inserts an anchored section header
* %<nop>CONTENTS% - generates a table of contents for a topic or an entire web, with full expansion depth control
* %<nop>REF% - inserts a symbolically named cross-reference URL
Expand All @@ -19,26 +19,223 @@ Example:

%ATTACHURL%/screenshot.png

TocPluginHelp uses the plugin, and contains complete help information.
The rest of this topic is written using the plugin, so you will see macros such as %<nop>SECTION0% if the plugin
is not installed and enabled.

---++ <nop>%TOPIC% Settings
%SECTION1{name="WebOrder"}% Books
The plugin depends on the existance of a topic called "WebOrder". This topic is referred to as the "book", and
should contain a list of
the topics that you want to generate section numbers for. Each topic in the book is allocated a =SECTION0=
section number i.e. the first topic is section 1, the second is section 2 etc.

<!--
* Set SHORTDESCRIPTION = %$SHORTDESCRIPTION%
-->
The book topic is a simple list - either a bulleted list or a numbered list - and can be created interactively
using the %IF{"istopic 'BookmakerPlugin'" then="BookmakerPlugin" else="Foswiki$nop:Extensions.BookmakerPlugin"}%.

Each entry in the book must be the name of a topic in the current web. This plugin ships with a simple WebOrder, which
you can view (but are advised not to change).

e.g.
%ANCHOR{type="Example" name="WebOrder",isplay="no"}% An example Weborder topic
<verbatim>
* [[An Overview of Perl]]
1 [[Getting Started]]
2 [[Natural and Artificial Languages]]
3 AGradeExample
2 [[The Gory Details]]
* LexicalTexture
</verbatim>

%SECTION1{name="SECTION"}% The =SECTION= macro
<i>Supported attributes:</i> =name= <p>

Subsections may be inserted in any topic using the =SECTIONn= macro,
where =n= is the required subsection level. The heading of the section
is taken as all text after the macro up to the end of line. For example,
the heading at the top of this section is marked with
<pre>
%<nop>SECTION1{name="SECTION"}% The =%<nop>SECTION= macro
</pre>

*NOTES*
* See also %REF{type="Section" name="IndentedWebOrder"}% for information about modifying section numbering from the book topic.
* Sections do not _have_ to be named, but if they are not then they can only be referred to by knowing the exact section number. Section names must be unique within the topic.
* The only way to _close_ a section is to start a new section with a different level, or to end the topic.
* You can still use standard HTML heading tags such as &lt;H1>, but sections marked this way will *not* be included in the table of contents.

%SECTION3% The %SECTION<nop>0% macro
If a %<nop>SECTION0% macro occurs in a topic, the heading of that section will replace the topic name in the table of contents.

*NOTES*
* The =name= attribute cannot be used to refer to a %<nop>SECTION0% macro.

%SECTION2{name="TOC"}% Building the table of contents
<i>Supported attributes:</i> =depth topic=

You can build a table of contents by inserting
<pre>
%<nop>CONTENTS%
</pre>
in a topic. The first level of the table of contents is normally the
topics in the order of the list in WebOrder, though see
%REF{type="Section" name="IndentedWebOrder"}% for information about
modifying section numbering from the WebOrder topic. Subsections
listed in the table are automatically linked to the target =SECTION=.

* The =topic= attribute may be used to generate a table of contents for just one topic.
* The =depth= attribute may be used to set the maximum number of levels to generate.

%SECTION3% Output from %<nop>CONTENTS{depth=2}% macro for this web
%CONTENTS{depth="2"}%
%ANCHOR{type="Example" name="TOC"}% Table of contents for this web

%SECTION3% Output from %<nop>CONTENTS% macro for this topic
%CONTENTS{topic="%TOPIC%"}%
%ANCHOR{type="Example" name="TopicTOC"}% Table of contents for this topic

%X% Note that the CONTENTS macro is quite slow. This is because it has to open and analyse all topics it refers to.

%I% You can still use the standard =%<nop>TOC%= macro in topics that use !TocPlugin. However only sections defined using the standard heading syntax will be visible.

%SECTION2{name="TOCCHECK"}% The =TOCCHECK= macro

Any topic (but most usually the WebOrder topic) may include the
<pre>%TOCCHECK<nop>%</pre>
macro. This causes the entries in the WebOrder topic to be
cross-referenced against the files actually stored in the web (see
WebIndex). Any topics which exist as files in the web but are missing
from the WebOrder will be listed.

%I% Any topics that begin with the characters "Web" are special topics and are excluded from the list, though they can still be listed in the WebOrder and will appear in the table of contents.

%SECTION1% Anchors and References - the =ANCHOR=, =REF= and =REFTABLE= macros
Bookmarks and references can be inserted into text using the ANCHOR
and REF macros. These can be used for references, for example, to tables
or figures.

%SECTION2{name="ANCHOR"}% The =ANCHOR= macro
<i>Supported attributes:</i> =type name display=

The ANCHOR macro creates a jump target suitable for jumping to from
somewhere else. The =type= adds the anchor to a "group"; this group is
required when generating a reference to the anchor, and may be used to
generate tables of same-type anchors (see
%REF{type="Section" name="REFTABLE"}% below). The =type= can be any name,
though convention suggests the use of types such as =Figure= and
=Table=. The special group =Section= is used internally to refer to
sections and subsections. Avoid using it for an =ANCHOR= or you may
see strange results.

The =ANCHOR= macro is normally visible in the output, though it may be
made invisible by setting the =display= attribute to =no= . For
example: %ANCHOR{type="Figure" name="A" display="no"}% Here be sea monsters
<pre>%<nop>ANCHOR{type="Figure" name="A" display="no"}% Here be sea
monsters</pre> will generate an invisible anchor on the text (there's
one one the line above, honest!) and
<pre>&lt;A name="#Figure_A"> &lt;/A></pre>
<pre>%<nop>ANCHOR{type="Table" name="A"}% A wooden table</pre>
will generate:
%ANCHOR{type="Table" name="A" display="yes"}% A wooden table

All the text between the anchor and the next end-of-line will be
used to create the anchor. If the anchor is invisible, this text will
be invisible too.

%SECTION2{name="REF"}% The =REF= macro
<i>Supported attributes:</i> =type topic name=

The =REF= macro may be used to refer to an anchor. Anchors are
automatically inserted by =SECTION= macros or may be added using the
=ANCHOR= macro. For a =REF= macro to work, the type of the target must be
known. For example:
<pre>
See %<nop>REF{type="Example" name="WebOrder"}% for more information about WebOrder
</pre>
will generate:

See %REF{type="Example" name="WebOrder"}% for more information about WebOrder

To refer to anchors in a different topic, use the =topic= attribute.
You can refer to sections by name by using the special type =Section=
e.g. %<nop>REF{type="Section" name="TOCCHECK"}%.

If you refer to a non-existant anchor you are warned: for example,
<pre>%<nop>REF{type="Reference" name="NonExistantAnchor"}%</pre>generates<p>

%REF{type="Reference" name="NonExistantAnchor"}%

%SECTION2{name="REFTABLE"}% The =REFTABLE= macro
<i>Supported attributes:</i> =type=

The =REFTABLE= macro can be used to build tables of references based on
the type assigned to anchors. For example, if you have a lot of
anchors of type =Example= you can build a table of all these anchors
thus:
<pre>%<nop>REFTABLE{type="Example"}%</pre>
%ANCHOR{type="Example" name="example1" display="no"}% REFTABLE{type="Table"} example
This will insert a table like this:
%REFTABLE{type="Example"}%
and <pre>%<nop>REFTABLE{type=Figure}%</pre>
will insert a table like this:
%ANCHOR{type="Example" name="example2" display="no"}% REFTABLE{type="Figure"} example
%REFTABLE{type="Figure"}%
All topics listed in the WebOrder are scanned, but only anchors of the
requested type will be listed.

%I% If you use =REFTABLE= with the type =Section= the table will contain a list of all _named_ sections. %ANCHOR{type="Example" name="example2" display="no"}% %REFTABLE{type="Section"}%

%SECTION1{name="AddTocButtons"}% Adding navigation buttons
When using the =WebOrder= special topic to collect a list of topics
into a somewhat "linearized" form (a "book"), it is often very
convenient to be able to add navigation buttons to the previous and
the next pages as well as to the home page (table of contents). This
can be done by adding the %<nop>TOCBUTTONS% macro to your pages. For
example, you can use it in a template which is included either at the
top or the bottom of your pages. The included
="view.tocbuttons.tmpl"= template (intended to be used with !NatSkin)
adds the =%<nop>TOCBUTTONS%= macro to the content footer of all pages in
the web. To activate it, use ="* Set SKIN = tocbuttons, nat"= in your
<nop>WebPreferences.

Note that the "Prev", "Home" and "Next" links will be added only for
such topics that are listed in the =WebOrder= special topic, and they
will only be inserted when viewing a page, i. e. they will for
example not show up when printing such a topic or the whole "book".

%SECTION1{name="IndentedWebOrder"}% Getting clever
It is possible to change the way the table of contents for the web is
ordered by using extra levels of indent in the WebOrder. If you indent
a topic below another topic, then that topic will be treated as a
section of the parent topic. Section numbers within the subtopic are
adjusted accordingly. For example, say the WebOrder contains
<pre>
* <nop>[[Top level topic]]
* <nop>AnotherTopLevelTopic
</pre>
<nop>TopLevelTopic will be numbered 1., and the first =SECTION1= within <nop>TopLevelTopic will be 1.1. <nop>AnotherTopLevelTopic will be numbered 2. If, instead, WebOrder contains
<pre>
* <nop>[[Top level topic]]
* <nop>[[Second level topic]]
* <nop>AnotherTopLevelTopic
</pre>
<nop>TopLevelTopic will still be numbered 1., but now <nop>SecondLevelTopic will be numbered 1.1.,
and the first =SECTION1= within <nop>SecondLevelTopic will be 1.1.1. The first =SECTION1= within
<nop>TopLevelTopic will now be numbered 1.2. <nop>AnotherTopLevelTopic will still be numbered 2.

%SECTION1{name="tips"}% Hints and Tips
* Include a %<nop>TOCCHECK% macro at the end of the table of contents topic.
* Name all sections. This makes it easier to refer to them by symbolic names rather than trying to REF numbered sections.

---+++ Plugin Installation Instructions
---+++ Installation Instructions
%$INSTALL_INSTRUCTIONS%
* If you see a link here: %REF{topic=TocPluginHelp,type=Section,name=installation}% then installation was successful
* Check the TocPluginHelp topic. You should see expanded section numbers.

---+++ Plugin Info
---+++ Info

| One Line Description: | Table of contents and cross-reference management |
| Author: | Foswiki:Main.CrawfordCurrie http://c-dot.co.uk |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: |
| 23 Mar 2011 | Made compatible with !BookmakerPlugin, which it now depends on. Now supports topics in different webs. |
| 22 Dec 2009 | Updated for Foswiki |
| 20 Apr 2006 | Minor doc update, headings no longer marked up as anchor text (Foswiki:Main.SteffenPoulsen) |
| 1 Oct 2001 | Corrected directory naming (no thanks to <nop>WindowsME!) |
Expand Down
3 changes: 1 addition & 2 deletions data/System/WebOrder.txt
@@ -1,4 +1,3 @@
* TocPluginHelp
* TocPlugin
1 TocPlugin

%TOCCHECK%
2 changes: 1 addition & 1 deletion lib/Foswiki/Plugins/TocPlugin.pm
Expand Up @@ -17,7 +17,7 @@
package Foswiki::Plugins::TocPlugin;

our $VERSION = '$Rev$';
our $RELEASE = '2.0';
our $RELEASE = '2.1';
our $wif;
our $SHORTDESCRIPTION = 'Sophisticated table of contents generation';
our $NO_PREFS_IN_TOPIC = 1;
Expand Down
1 change: 1 addition & 0 deletions lib/Foswiki/Plugins/TocPlugin/DEPENDENCIES
@@ -0,0 +1 @@
Foswiki::Plugins::BookmakerPlugin,>=0,perl,Required, available from Foswiki.org
13 changes: 7 additions & 6 deletions lib/Foswiki/Plugins/TocPlugin/Section.pm
Expand Up @@ -18,6 +18,7 @@ package Foswiki::Plugins::TocPlugin::Section;

use strict;
use integer;
use Assert;

use Foswiki::Plugins::TocPlugin::Attrs ();
use Foswiki::Plugins::TocPlugin::Anchor ();
Expand All @@ -44,7 +45,7 @@ sub new {
$this->{PARENT} = undef;
# position in the parent section's subsection list
$this->{POSITION} = -1;
# files only - wiki name of this section
# files only - wiki name of this section (web.topic)
$this->{WIKINAME} = undef;
# file only - has been loaded
$this->{IS_LOADED} = undef;
Expand Down Expand Up @@ -182,7 +183,7 @@ sub _getLastSubsection {
sub _addSection {
my ($this, $newEntry) = @_;

die "Section depth not relative to root" if
die "Section depth not relative to root ".$newEntry->level()." <= ".$this->level() if
$newEntry->level() <= $this->level();

if ($newEntry->level() == $this->level() + 1) {
Expand Down Expand Up @@ -500,13 +501,14 @@ sub _processREFTag {
# Load the topic into this section
sub parseTopicText {
my ( $this, $text) = @_;

my $i = 0;

while ($text =~ s/%(SECTION[0-9]+|ANCHOR)({[^%]*})?%(.*)//o) {
my $key = $1;
my ($key, $title) = ($1, $3);
my $attrs = Foswiki::Plugins::TocPlugin::Attrs->new($2);
my $title = $3;
$title =~ s/(^\s+|\s+$)//go;
$attrs->set("text", $title);
$attrs->{name} ||= $i++;

if ($key =~ s/([0-9]+)//o) {
$key = $1;
Expand Down Expand Up @@ -580,7 +582,6 @@ sub toPrint {
my $ct = $toc->_findTopic($this->wikiName);
my $text = $wif->readTopic($this->wikiName);

# $res .= "Expanding wikiName= " . $wif->webName . "." . $this->wikiName() . " ";
$res .= Foswiki::Plugins::TocPlugin::TOC::_printWithTOCTags($toc, $wif, $ct, $text);

}
Expand Down
7 changes: 3 additions & 4 deletions lib/Foswiki/Plugins/TocPlugin/TOC.pm
Expand Up @@ -76,9 +76,9 @@ sub _processTOCTags {
if ($tag =~ s/SECTION([0-9]+)//o) {
my $level = $1;
$attrs->set("level", $ct->level() + $level);
$text =~ s/\<TOC_Mark\>/&_processTag($toc, $wif, $toc->currentTopic, "SECTION", $attrs)/eo;
$text =~ s/\<TOC_Mark\>/&_processTag($toc, $wif, $toc->currentTopic, "SECTION", $attrs)/e;
} else {
$text =~ s/\<TOC_Mark\>/&_processTag($toc, $wif, $toc->currentTopic, "ANCHOR", $attrs)/eo;
$text =~ s/\<TOC_Mark\>/&_processTag($toc, $wif, $toc->currentTopic, "ANCHOR", $attrs)/e;
}
}
}
Expand All @@ -95,8 +95,7 @@ sub _processTOCTags {
my $toc = undef;
my $mess = undef;

sub processTOCBUTTONSTag
{
sub processTOCBUTTONSTag {
my ($toc, $wif, $topic) = @_;

return "" unless (defined $toc);
Expand Down
22 changes: 7 additions & 15 deletions lib/Foswiki/Plugins/TocPlugin/TOCIF.pm
Expand Up @@ -39,32 +39,24 @@ sub getInterface {
$singleton = $this;
}
$this->{WEBNAME} = $web;
$this->{TOPIC} = $topic;
$this->{TOPICNAME} = $topic;
return bless($this, $class);
}

sub webName {
my $this = shift;
return $this->{WEBNAME};
}

sub topicName {
my $this = shift;
return $this->{TOPIC};
}

sub topicExists {
my ($this, $topic) = @_;
return Foswiki::Func::topicExists($this->webName(), $topic);
my ($w, $t) = Foswiki::Func::normalizeWebTopicName($this->{WEBNAME}, $topic);
return Foswiki::Func::topicExists($w, $t);
}

sub readTopic {
my ($this, $topic) = @_;
my $text = "";
# read the topic
$text = Foswiki::Func::readTopic($this->webName(), $topic);
my ($w, $t) = Foswiki::Func::normalizeWebTopicName($this->{WEBNAME}, $topic);
$text = Foswiki::Func::readTopic($w, $t);
# expand the variables -- in the context of the appropriate topic
$text = Foswiki::Func::expandCommonVariables($text, $topic, $this->webName());
$text = Foswiki::Func::expandCommonVariables($text, $t, $w);
# this can't be right -- turn verbatim tags into pre tags
$text =~ s/<verbatim>/<pre>/go;
$text =~ s/<\/verbatim>/<\/pre>/go;
Expand All @@ -73,7 +65,7 @@ sub readTopic {

sub webDirList {
my $this = shift;
return Foswiki::Func::getTopicList($this->webName());
return Foswiki::Func::getTopicList($this->{WEBNAME});
}

1;

0 comments on commit 7e8123a

Please sign in to comment.