Search in sources :

Example 11 with SplitOutlineItem

use of org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem in project mylyn.docs by eclipse.

the class WikiToDocTask method createToc.

private void createToc(List<Path> paths, final Map<String, SplitOutlineItem> pathNameToOutline) {
    // $NON-NLS-1$
    getProject().log(// $NON-NLS-1$
    MessageFormat.format(Messages.getString("WikiToDocTask_writing_toc"), tocFile), Project.MSG_VERBOSE);
    final OutlineItem rootItem = new // $NON-NLS-1$
    OutlineItem(// $NON-NLS-1$
    null, // $NON-NLS-1$
    0, // $NON-NLS-1$
    "<root>", // $NON-NLS-1$
    0, // $NON-NLS-1$
    -1, title == null ? computeTitle(paths.get(0)) : title);
    final Map<OutlineItem, Path> outlineItemToPath = new HashMap<OutlineItem, Path>();
    final Map<String, OutlineItem> nameToItem = new HashMap<String, OutlineItem>();
    // create root-level items
    for (Path path : paths) {
        if (path.includeInUnifiedToc) {
            SplitOutlineItem pathItem = pathNameToOutline.get(path.name);
            outlineItemToPath.put(pathItem, path);
            nameToItem.put(path.name, pathItem);
            if (path.getTocParentName() == null) {
                rootItem.getChildren().add(pathItem);
            }
        }
    }
    for (Path path : paths) {
        if (path.includeInUnifiedToc) {
            if (path.getTocParentName() != null) {
                SplitOutlineItem pathItem = pathNameToOutline.get(path.name);
                if (nameToItem.containsKey(path.getTocParentName())) {
                    nameToItem.get(path.getTocParentName()).getChildren().add(pathItem);
                } else {
                    throw new ConfigurationException(MessageFormat.format(Messages.getString("WikiToDocTask_unknown_tocParentName"), // $NON-NLS-1$
                    path.getTocParentName()));
                }
            }
        }
    }
    SplittingMarkupToEclipseToc markupToEclipseToc = new SplittingMarkupToEclipseToc() {

        @Override
        protected String computeFile(OutlineItem item) {
            if (item instanceof SplitOutlineItem) {
                return computeTocRelativeFile(outlineItemToPath, item);
            }
            return super.computeFile(item);
        }
    };
    markupToEclipseToc.setBookTitle(rootItem.getLabel());
    markupToEclipseToc.setHtmlFile(computeTocRelativeFile(outlineItemToPath, rootItem.getChildren().get(0)));
    String tocContents = markupToEclipseToc.createToc(rootItem);
    try {
        // $NON-NLS-1$
        Writer writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(tocFile)), "UTF-8");
        try {
            writer.write(tocContents);
        } finally {
            writer.close();
        }
    } catch (IOException e) {
        // $NON-NLS-1$
        String message = MessageFormat.format("Cannot write {0}: {1}", tocFile, e.getMessage());
        throw new BuildException(message, e);
    }
}
Also used : HashMap(java.util.HashMap) SplittingMarkupToEclipseToc(org.eclipse.mylyn.wikitext.splitter.SplittingMarkupToEclipseToc) SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) IOException(java.io.IOException) SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) OutlineItem(org.eclipse.mylyn.wikitext.parser.outline.OutlineItem) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException) BufferedOutputStream(java.io.BufferedOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

SplitOutlineItem (org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem)11 File (java.io.File)8 OutputStreamWriter (java.io.OutputStreamWriter)6 HtmlDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)6 SplittingHtmlDocumentBuilder (org.eclipse.mylyn.wikitext.splitter.SplittingHtmlDocumentBuilder)6 FileOutputStream (java.io.FileOutputStream)5 Writer (java.io.Writer)5 BuildException (org.apache.tools.ant.BuildException)5 BufferedOutputStream (java.io.BufferedOutputStream)4 IOException (java.io.IOException)4 MarkupParser (org.eclipse.mylyn.wikitext.parser.MarkupParser)4 DefaultSplittingStrategy (org.eclipse.mylyn.wikitext.splitter.DefaultSplittingStrategy)4 SplittingOutlineParser (org.eclipse.mylyn.wikitext.splitter.SplittingOutlineParser)4 StringWriter (java.io.StringWriter)3 HashMap (java.util.HashMap)3 NoSplittingStrategy (org.eclipse.mylyn.wikitext.splitter.NoSplittingStrategy)3 SplittingStrategy (org.eclipse.mylyn.wikitext.splitter.SplittingStrategy)3 MediaWikiLanguage (org.eclipse.mylyn.wikitext.mediawiki.MediaWikiLanguage)2 OutlineItem (org.eclipse.mylyn.wikitext.parser.outline.OutlineItem)2 SplittingMarkupToEclipseToc (org.eclipse.mylyn.wikitext.splitter.SplittingMarkupToEclipseToc)2