Skip to content

Commit

Permalink
Merge pull request #1025 from jdufner/feature/toc_of_page_headings
Browse files Browse the repository at this point in the history
Implemented table of content of all page headings
  • Loading branch information
amolenaar committed Nov 14, 2017
2 parents 16db1c3 + 2a2312d commit 8d1747c
Show file tree
Hide file tree
Showing 24 changed files with 965 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
| this is ''italic'' text | this is <i>italic</i> text | italic widget |
| this is '''bold''' text | this is <b>bold</b> text | bold widget |
| !c This is centered text | <center>This is centered text</center> |
| !1 header | <h1>header</h1> |
| !2 header | <h2>header</h2> |
| !3 header | <h3>header</h3> |
| !4 header | <h4>header</h4> |
| !5 header | <h5>header</h5> |
| !6 header | <h6>header</h6> |
|!1 header |<h1 id="header">header</h1> |
|!2 header |<h2 id="header">header</h2> |
|!3 header |<h3 id="header">header</h3> |
|!4 header |<h4 id="header">header</h4> |
|!5 header |<h5 id="header">header</h5> |
|!6 header |<h6 id="header">header</h6> |
| http://files/x | <a href="files/x">http://files/x</a> | file link |
| http://fitnesse.org | <a href="http://fitnesse.org">http://fitnesse.org</a> | http link |
| SomePage | SomePage<a title="create page" href="SomePage\?edit&nonExistent=true">\[\?\]</a> | missing wiki word |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<properties>
<Edit/>
<Files/>
<Properties/>
<RecentChanges/>
<Refactor/>
<Search/>
<Suites>slim</Suites>
<Test/>
<Versions/>
<WhereUsed/>
<saveId>1233779413308</saveId>
<ticketId>-5607632332474131237</ticketId>
<Edit/>
<Files/>
<Properties/>
<RecentChanges/>
<Refactor/>
<Search/>
<Suites>slim</Suites>
<Test/>
<Versions/>
<WhereUsed/>
<saveId>1233779413308</saveId>
<ticketId>-5607632332474131237</ticketId>
</properties>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
This page tests the headings on a wiki page.

First create a page with some headings.

!|Page Creator |
|page name |page contents |page attributes|valid?|
|HeadingsPage|!-!headings
!1 Title of Page
!2 Heading
!3 Bla Bla Bla
!4 Foo
!5 Bar
!6 Baz-!| |true |

Then request the page.

!|Response Requester|
|uri |valid?|
|HeadingsPage|true |

Examine the page to be sure it contains the headings list.

!|Response Examiner |
|type |pattern |matches?|value|
|contents|Contents: |true | |
|contents|<a href="#TitleofPage">Title of Page</a> |true | |
|contents|<li class="heading1">.*<a href="#TitleofPage">Title of Page</a>.*</li>|true | |
|contents|<li class="heading2">.*<a href="#Heading">Heading</a>.*</li> |true | |
|contents|<li class="heading3">.*<a href="#BlaBlaBla">Bla Bla Bla</a>.*</li> |true | |
|contents|<li class="heading4">.*<a href="#Foo">Foo</a>.*</li> |true | |
|contents|<li class="heading5">.*<a href="#Bar">Bar</a>.*</li> |true | |
|contents|<li class="heading6">.*<a href="#Baz">Baz</a>.*</li> |true | |
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
Headers are created by prefixing a line with !1 or !2 or !3

|!c '''Markup Text'''|!c '''Displayed as'''|
|!- !1 Title -!|!1 Title|
|!- !2 Header -!|!2 Header|
|!- !3 Second Header -!|!3 Second Header|




Headers are created by prefixing a line with !1 or !2 or !3 or !4 or !5 or !6

|!c '''Markup Text''' |!c '''Displayed as'''|
|!- !1 Title -!|!1 Title |
|!- !2 Header -!|!2 Header |
|!- !3 Second Header -!|!3 Second Header |
|!- !4 Third Header -!|!4 Third Header |
|!- !5 Fourth Header -!|!5 Fourth Header |
|!- !6 Fifth Header -!|!6 Fifth Header |
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Headings are created by prefixing a line with !headings

|!c '''Markup Text''' |!c '''Displayed as'''|
|!- !heading -!|Table of contents |

Markup:
{{{!headings -style decimal}}}

|''' argument '''|''' description '''|
|-style |The CSS style of the list items, possible values: decimal, decimal-leading-zero, lower-roman, upper-roman, lower-alpha, upper-alpha, none. Default value: decimal. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<properties>
<Files/>
<LastModified>20090228113826</LastModified>
<RecentChanges>true</RecentChanges>
<WhereUsed/>
</properties>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The !-FitNesse-! markup language is straightforward to learn, and powerful eno
| [[Style][>MarkupStyle]] | ''Setting the style of some text.'' |
| [[Cross Reference][>MarkupCrossReference]] | ''Add a cross reference to another page.'' |
| [[Headers][>MarkupHeaders]] | ''Setting a line as a title or section header.'' |
| [[Headings][>MarkupHeadings]] | ''Generate a table of contents of all headers from within a wiki page.'' |
| [[Centering][>MarkupCenter]] | ''Centering a line.'' |
| [[Notes][>MarkupNote]] | ''Making a note.'' |
| [[Horizontal Rules][>MarkupHorizontalRule]] | ''Drawing a horizontal separator.'' |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@
|${CODE}''over-text''${NUL}'''!-----------!'''${NUL}''under-text''${CODEend}|thicker |
)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!define HEADINGS (${BANG}'''1''' ''largest heading text''
!define HEADER LINES (${BANG}'''1''' ''largest heading text''
${BANG}'''2''' ''middle heading text''
${BANG}'''3''' ''smaller heading text''
and so on up to ${BANG}'''6'''
)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!define HEADINGS (${BANG}'''!headings''' generates a list of all header lines
)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!define CollapsibleEXPANDED (${BANG}'''*''' ${OPT}''title''${OPTend}
${SPC}${SPC}'' multi-line wiki text ''
${SPC}${SPC}'' N.B.: Multiple asterisks are allowed, e.g.,'' ${BANG}'''****''' ${TEXT}
Expand Down Expand Up @@ -106,7 +109,7 @@ ${SPC}${SPC}'' N.B.: Multiple asterisks are allowed, e.g.,'' ${BANG}'''****'''${

)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!define TocInsert (| Contents List | ${CODE} ${BANG}'''contents''' ${CODEend} |
!define TocInsert (| Contents List | ${CODE} ${BANG}'''contents''' ${CODEend} |
| Contents Tree | ${CODE} ${BANG}'''contents -R''' ${CODEend} |
| Contents Sub-tree | ${CODE} ${BANG}'''contents -R'''${OPT}''nn''${OPTend}${CODEend} |
| Contents List - Graceful | ${CODE} ${BANG}'''contents -g''' ${CODEend} |
Expand Down
5 changes: 5 additions & 0 deletions src/fitnesse/html/HtmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ private static String replaceStrings(String value, String[] originalStrings, Str
result = result.replace(originalStrings[i], replacementStrings[i]);
return result;
}

public static String remainRfc3986UnreservedCharacters(final String heading) {
return heading.replaceAll("[^A-Za-z0-9\\-._~]", "");
}

}
52 changes: 52 additions & 0 deletions src/fitnesse/wiki/SymbolUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package fitnesse.wiki;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import fitnesse.wikitext.parser.Symbol;
import fitnesse.wikitext.parser.SymbolType;

/**
* Utilities for Handling a tree of {@link fitnesse.wikitext.parser.Symbol} aka syntax tree.
*/
public class SymbolUtil {

/**
* Walks through the syntax tree either in pre- oder postorder, looking for all symbols of the
* given type and collects it in a list.
*
* @param syntaxTree The syntax tree is a tree of {@link Symbol}.
* @param type The {@link SymbolType} of the searched {@link Symbol}. If type is null,
* each type is accepted.
* @param preorder Walks through the syntax tree in preorder when <code>true</code>, in
* postorder when <code>fals</code>.
* @return A List of Symbols.
*/
public static List<Symbol> findSymbolsByType(final Symbol syntaxTree, final SymbolType type,
final boolean preorder) {
final List<Symbol> symbols = new LinkedList<>();
if (preorder) {
addToSymbolsIfIsOfType(symbols, syntaxTree, type);
}
for (Symbol subTree : syntaxTree.getChildren()) {
symbols.addAll(findSymbolsByType(subTree, type, preorder));
}
if (!preorder) {
addToSymbolsIfIsOfType(symbols, syntaxTree, type);
}
return Collections.unmodifiableList(symbols);
}

private static void addToSymbolsIfIsOfType(final List<Symbol> symbols, final Symbol symbol,
final SymbolType type) {
if (type == null) {
symbols.add(symbol);
} else {
if (symbol.isType(type)) {
symbols.add(symbol);
}
}
}

}
33 changes: 9 additions & 24 deletions src/fitnesse/wiki/WikiPageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import fitnesse.wikitext.parser.Alias;
import fitnesse.wikitext.parser.See;
import fitnesse.wikitext.parser.Symbol;
import fitnesse.wikitext.parser.SymbolTreeWalker;
import fitnesse.wikitext.parser.SymbolType;

public class WikiPageUtil {

Expand Down Expand Up @@ -88,26 +85,14 @@ public static File resolveFileUri(String fullPageURI, File rootPath) {

public static List<String> getXrefPages(WikiPage page) {
if (page instanceof WikitextPage) {
final List<String> xrefPages = new ArrayList<>();
((WikitextPage) page).getSyntaxTree().walkPreOrder(new SymbolTreeWalker() {
@Override
public boolean visit(Symbol node) {
if (node.isType(See.symbolType)) {
if(node.childAt(0).isType(Alias.symbolType)) {
xrefPages.add(node.childAt(0).lastChild().childAt(0).getContent());
} else {
xrefPages.add(node.childAt(0).getContent());
}
}
return true;
}

@Override
public boolean visitChildren(Symbol node) {
return true;
}
});
return xrefPages;
return WikitextPageUtil.getXrefPages((WikitextPage) page);
}
return Collections.emptyList();
}

public static List<Symbol> getSymbols(final WikiPage page, final SymbolType symbolType) {
if (page instanceof WikitextPage) {
return WikitextPageUtil.getSymbols((WikitextPage) page, symbolType);
}
return Collections.emptyList();
}
Expand Down
9 changes: 9 additions & 0 deletions src/fitnesse/wiki/WikiSourcePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import fitnesse.wikitext.parser.Maybe;
import fitnesse.wikitext.parser.SourcePage;
import fitnesse.wikitext.parser.Symbol;
import fitnesse.wikitext.parser.SymbolType;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class WikiSourcePage implements SourcePage {
private WikiPage page;
Expand Down Expand Up @@ -128,4 +131,10 @@ private boolean isParentOf(WikiPage possibleParent) {
public int compareTo(SourcePage other) {
return getName().compareTo(other.getName());
}

@Override
public List<Symbol> getSymbols(final SymbolType symbolType) {
return WikiPageUtil.getSymbols(page, symbolType);
}

}
49 changes: 49 additions & 0 deletions src/fitnesse/wiki/WikitextPageUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package fitnesse.wiki;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import fitnesse.wikitext.parser.Alias;
import fitnesse.wikitext.parser.See;
import fitnesse.wikitext.parser.Symbol;
import fitnesse.wikitext.parser.SymbolTreeWalker;
import fitnesse.wikitext.parser.SymbolType;

public class WikitextPageUtil {

public static List<String> getXrefPages(WikitextPage page) {
final List<String> xrefPages = new ArrayList<>();
page.getSyntaxTree().walkPreOrder(new SymbolTreeWalker() {
@Override
public boolean visit(Symbol node) {
if (node.isType(See.symbolType)) {
if (node.childAt(0).isType(Alias.symbolType)) {
xrefPages.add(node.childAt(0).lastChild().childAt(0).getContent());
} else {
xrefPages.add(node.childAt(0).getContent());
}
}
return true;
}

@Override
public boolean visitChildren(Symbol node) {
return true;
}
});
return xrefPages;
}

public static List<Symbol> getSymbols(final WikitextPage page, final SymbolType symbolType) {
final List<Symbol> symbols = new LinkedList<>();
for (final Symbol symbol : page.getSyntaxTree().getChildren()) {
if (symbol.isType(symbolType)) {
symbols.add(symbol);
}
}
return Collections.unmodifiableList(symbols);
}

}
Loading

0 comments on commit 8d1747c

Please sign in to comment.