All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.bliki.wiki.tags.TableOfContentTag Maven / Gradle / Ivy

The newest version!
package info.bliki.wiki.tags;

import info.bliki.Messages;
import info.bliki.wiki.filter.Encoder;
import info.bliki.wiki.filter.ITextConverter;
import info.bliki.wiki.filter.SectionHeader;
import info.bliki.wiki.model.ITableOfContent;
import info.bliki.wiki.model.IWikiModel;
import info.bliki.wiki.tags.util.IBodyTag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Wiki tag for rendering the "table of contents" inside a wikipedia
 * article
 *
 */
public class TableOfContentTag extends HTMLTag implements IBodyTag,
        ITableOfContent {
    private List fTableOfContent = null;

    private boolean fShowToC;

    private boolean fIsTOCIdentifier;

    public TableOfContentTag(String name) {
        super(name);
        this.fShowToC = false;
        this.fIsTOCIdentifier = false;
    }

    @Override
    public void renderHTML(ITextConverter converter, Appendable writer,
            IWikiModel model) throws IOException {
        if (!model.isNoToc()) {
            if (fShowToC && fTableOfContent != null
                    && fTableOfContent.size() > 0) {
                String contentString = Messages.getString(
                        model.getResourceBundle(),
                        Messages.WIKI_TAGS_TOC_CONTENT, "Contents");
                writer.append("\n" + "\n" + "
\n" + "
\n" + "

"); writer.append(contentString); writer.append("

\n
"); renderToC(writer, fTableOfContent, 0); writer.append("

\n"); } } } private void renderToC(Appendable writer, List toc, int level) throws IOException { writer.append("\n
    "); boolean counted = false; boolean setLI = false; for (Object tocItem : toc) { if (tocItem instanceof SectionHeader) { if (setLI) { setLI = false; writer.append("\n"); } if (!counted) { level++; counted = true; } SectionHeader pair = (SectionHeader) tocItem; String head = Encoder.encodeHtml(pair.getFirst()); String anchor = pair.getSecond(); writer.append("\n
  • ") .append(head).append(""); setLI = true; } else { @SuppressWarnings("unchecked") final List list = (List) tocItem; renderToC(writer, list, level); if (setLI) { setLI = false; writer.append("\n"); } } } if (setLI) { writer.append("\n"); } writer.append("\n"); } @Override public boolean isReduceTokenStack() { return false; } public boolean isShowToC() { return fShowToC; } /** * Enable or disable the rendering of the "table of content" * * @param showToC * if true render the "table of content" */ @Override public void setShowToC(boolean showToC) { fShowToC = showToC; } @Override public Object clone() { TableOfContentTag tocTag = (TableOfContentTag) super.clone(); tocTag.fShowToC = this.fShowToC; tocTag.fIsTOCIdentifier = this.fIsTOCIdentifier; if (this.fTableOfContent == null) { tocTag.fTableOfContent = null; } else { tocTag.fTableOfContent = new ArrayList<>(this.fTableOfContent); } return tocTag; } public List getTableOfContent() { if (fTableOfContent == null) { fTableOfContent = new ArrayList<>(); } return fTableOfContent; } public boolean isTOCIdentifier() { return fIsTOCIdentifier; } public void setTOCIdentifier(boolean isTOCIdentifier) { fIsTOCIdentifier = isTOCIdentifier; } @Override public List getSectionHeaders() { List resultList = new ArrayList<>(); extractSectionHeaders(fTableOfContent, resultList); return resultList; } private void extractSectionHeaders(List toc, List resultList) { for (Object tocItem : toc) { if (tocItem instanceof SectionHeader) { SectionHeader header = (SectionHeader) tocItem; resultList.add(header); } else { @SuppressWarnings("unchecked") final List list = (List) tocItem; extractSectionHeaders(list, resultList); } } } }