Search in sources :

Example 1 with SplitOutlineItem

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

the class MarkupToHtmlTask method processFile.

/**
 * process the file
 *
 * @param baseDir
 * @param source
 * @return the lightweight markup, or null if the file was not written
 * @throws BuildException
 */
protected String processFile(MarkupLanguage markupLanguage, final File baseDir, final File source) throws BuildException {
    // $NON-NLS-1$
    log(MessageFormat.format(Messages.getString("MarkupToHtmlTask.14"), source), Project.MSG_VERBOSE);
    String markupContent = null;
    String name = source.getName();
    if (name.lastIndexOf('.') != -1) {
        name = name.substring(0, name.lastIndexOf('.'));
    }
    File htmlOutputFile = computeHtmlFile(source, name);
    if (!htmlOutputFile.exists() || overwrite || htmlOutputFile.lastModified() < source.lastModified()) {
        if (markupContent == null) {
            markupContent = readFully(source);
        }
        performValidation(source, markupContent);
        Writer writer;
        try {
            // $NON-NLS-1$
            writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(htmlOutputFile)), "utf-8");
        } catch (Exception e) {
            throw new BuildException(MessageFormat.format(Messages.getString("MarkupToHtmlTask.16"), htmlOutputFile, e.getMessage()), // $NON-NLS-1$
            e);
        }
        try {
            HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer, formatOutput);
            for (Stylesheet stylesheet : stylesheets) {
                HtmlDocumentBuilder.Stylesheet builderStylesheet;
                if (stylesheet.url != null) {
                    builderStylesheet = new HtmlDocumentBuilder.Stylesheet(stylesheet.url);
                } else {
                    builderStylesheet = new HtmlDocumentBuilder.Stylesheet(stylesheet.file);
                }
                builder.addCssStylesheet(builderStylesheet);
                if (!stylesheet.attributes.isEmpty()) {
                    for (Map.Entry<String, String> attr : stylesheet.attributes.entrySet()) {
                        builderStylesheet.getAttributes().put(attr.getKey(), attr.getValue());
                    }
                }
            }
            builder.setTitle(title == null ? name : title);
            builder.setEmitDtd(emitDoctype);
            if (emitDoctype && htmlDoctype != null) {
                builder.setHtmlDtd(htmlDoctype);
            }
            builder.setUseInlineStyles(useInlineCssStyles);
            builder.setSuppressBuiltInStyles(suppressBuiltInCssStyles);
            builder.setLinkRel(linkRel);
            builder.setDefaultAbsoluteLinkTarget(defaultAbsoluteLinkTarget);
            builder.setPrependImagePrefix(prependImagePrefix);
            builder.setXhtmlStrict(xhtmlStrict);
            builder.setCopyrightNotice(copyrightNotice);
            builder.setHtmlFilenameFormat(htmlFilenameFormat);
            SplittingStrategy splittingStrategy = multipleOutputFiles ? new DefaultSplittingStrategy() : new NoSplittingStrategy();
            SplittingOutlineParser outlineParser = new SplittingOutlineParser();
            outlineParser.setMarkupLanguage(markupLanguage.clone());
            outlineParser.setSplittingStrategy(splittingStrategy);
            SplitOutlineItem item = outlineParser.parse(markupContent);
            item.setSplitTarget(htmlOutputFile.getName());
            SplittingHtmlDocumentBuilder splittingBuilder = new SplittingHtmlDocumentBuilder();
            splittingBuilder.setRootBuilder(builder);
            splittingBuilder.setOutline(item);
            splittingBuilder.setRootFile(htmlOutputFile);
            splittingBuilder.setNavigationImages(navigationImages);
            splittingBuilder.setFormatting(formatOutput);
            MarkupParser parser = new MarkupParser();
            parser.setMarkupLanguage(markupLanguage);
            parser.setBuilder(splittingBuilder);
            parser.parse(markupContent);
            processed(markupContent, item, baseDir, source);
        } finally {
            try {
                writer.close();
            } catch (Exception e) {
                throw new BuildException(MessageFormat.format(// $NON-NLS-1$
                Messages.getString("MarkupToHtmlTask.17"), // $NON-NLS-1$
                htmlOutputFile, e.getMessage()), e);
            }
        }
    }
    return markupContent;
}
Also used : SplittingStrategy(org.eclipse.mylyn.wikitext.splitter.SplittingStrategy) NoSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.NoSplittingStrategy) DefaultSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.DefaultSplittingStrategy) SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) SplittingHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.splitter.SplittingHtmlDocumentBuilder) BuildException(org.apache.tools.ant.BuildException) FileOutputStream(java.io.FileOutputStream) SplittingOutlineParser(org.eclipse.mylyn.wikitext.splitter.SplittingOutlineParser) NoSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.NoSplittingStrategy) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException) SplittingHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.splitter.SplittingHtmlDocumentBuilder) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) DefaultSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.DefaultSplittingStrategy) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 2 with SplitOutlineItem

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

the class WikiToDocTask method execute.

@Override
public void execute() throws ConfigurationException {
    if (dest == null) {
        // $NON-NLS-1$
        throw new ConfigurationException(Messages.getString("WikiToDocTask_specify_dest"));
    }
    if (wikiBaseUrl == null) {
        // $NON-NLS-1$
        throw new ConfigurationException(Messages.getString("WikiToDocTask_specify_wikiBaseUrl"));
    }
    if (paths.isEmpty()) {
        // $NON-NLS-1$
        throw new ConfigurationException(Messages.getString("WikiToDocTask_specify_paths"));
    }
    if (getInternalLinkPattern() == null) {
        setInternalLinkPattern(computeDefaultInternalLinkPattern());
    }
    Set<String> pathNames = new HashSet<String>();
    for (Path path : paths) {
        if (path.name == null) {
            // $NON-NLS-1$
            throw new ConfigurationException(Messages.getString("WikiToDocTask_path_must_have_name"));
        }
        if (path.name != null) {
            if (!pathNames.add(path.name)) {
                throw new ConfigurationException(MessageFormat.format(Messages.getString("WikiToDocTask_path_name_must_be_unique"), // $NON-NLS-1$
                path.name));
            }
        }
        if (!path.includeInUnifiedToc && path.getTocParentName() != null) {
            throw new ConfigurationException(MessageFormat.format(Messages.getString("WikiToDocTask_tocParentName_not_in_unified_toc"), // $NON-NLS-1$
            path.name));
        }
    }
    if (generateUnifiedToc) {
        for (Path path : paths) {
            if (path.getTocParentName() != null) {
                if (!pathNames.contains(path.getTocParentName())) {
                    throw new ConfigurationException(MessageFormat.format(Messages.getString("WikiToDocTask_unknown_tocParentName"), // $NON-NLS-1$
                    path.getTocParentName()));
                }
            }
        }
    }
    MediaWikiLanguage markupLanguage = (MediaWikiLanguage) createMarkupLanguage();
    WikiTemplateResolver templateResolver = new WikiTemplateResolver();
    templateResolver.setWikiBaseUrl(wikiBaseUrl);
    markupLanguage.getTemplateProviders().add(templateResolver);
    markupLanguage.setTemplateExcludes(templateExcludes);
    for (Stylesheet stylesheet : stylesheets) {
        if (stylesheet.url == null && stylesheet.file == null) {
            // $NON-NLS-1$
            throw new BuildException(Messages.getString("WikiToDocTask_stylesheet_file_or_url"));
        }
        if (stylesheet.url != null && stylesheet.file != null) {
            // $NON-NLS-1$
            throw new BuildException(Messages.getString("WikiToDocTask_stylesheet_not_both"));
        }
        if (stylesheet.file != null) {
            if (!stylesheet.file.exists()) {
                throw new BuildException(// $NON-NLS-1$
                MessageFormat.format(// $NON-NLS-1$
                Messages.getString("WikiToDocTask_stylesheet_file_not_exist"), stylesheet.file));
            }
            if (!stylesheet.file.isFile()) {
                throw new BuildException(// $NON-NLS-1$
                MessageFormat.format(// $NON-NLS-1$
                Messages.getString("WikiToDocTask_stylesheet_file_not_file"), stylesheet.file));
            }
            if (!stylesheet.file.canRead()) {
                throw new BuildException(MessageFormat.format(Messages.getString("WikiToDocTask_stylesheet_file_cannot_read"), // $NON-NLS-1$
                stylesheet.file));
            }
        }
    }
    if (!dest.exists()) {
        if (!dest.mkdirs()) {
            throw new BuildException(// $NON-NLS-1$
            MessageFormat.format("Cannot create dest folder: {0}", dest.getAbsolutePath()));
        }
    }
    if (tocFile == null) {
        // $NON-NLS-1$
        tocFile = new File(dest, "toc.xml");
    }
    Map<String, String> pathNameToContent = new HashMap<String, String>();
    Map<String, SplitOutlineItem> pathNameToOutline = new HashMap<String, SplitOutlineItem>();
    for (Path path : paths) {
        getProject().log(// $NON-NLS-1$
        MessageFormat.format(Messages.getString("WikiToDocTask_fetching_content_for_page"), path.name), Project.MSG_VERBOSE);
        URL pathUrl = computeRawUrl(path.name);
        try {
            Reader input = createInputReader(pathUrl);
            try {
                String content = readFully(input);
                content = preprocessMarkup(path, content);
                pathNameToContent.put(path.name, content);
                final File targetFile = computeHtmlOutputFile(path);
                SplitOutlineItem outline = computeOutline(path, markupLanguage, targetFile, content);
                outline.setResourcePath(targetFile.getAbsolutePath());
                pathNameToOutline.put(path.name, outline);
            } finally {
                input.close();
            }
        } catch (final IOException e) {
            // $NON-NLS-1$
            final String message = MessageFormat.format("Cannot read from {0}: {1}", pathUrl, e.getMessage());
            throw new BuildException(message, e);
        }
    }
    for (Path path : paths) {
        // $NON-NLS-1$
        getProject().log(// $NON-NLS-1$
        MessageFormat.format(Messages.getString("WikiToDocTask_processing_page"), path.name), Project.MSG_DEBUG);
        String markupContent = pathNameToContent.get(path.name);
        if (isValidate()) {
            performValidation(markupLanguage, path, markupContent);
        }
        Set<String> imageFilenames = null;
        if (!fetchImages) {
            // $NON-NLS-1$
            getProject().log(Messages.getString("WikiToDocTask_skipping_images"), Project.MSG_WARN);
        } else {
            imageFilenames = fetchImages(markupLanguage, path);
        }
        markupToDoc(markupLanguage, path, markupContent, pathNameToOutline, imageFilenames);
        if (path.isGenerateToc()) {
            createToc(path, pathNameToOutline.get(path.name));
        }
    }
    if (generateUnifiedToc) {
        createToc(paths, pathNameToOutline);
    }
}
Also used : HashMap(java.util.HashMap) WikiTemplateResolver(org.eclipse.mylyn.wikitext.mediawiki.WikiTemplateResolver) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) IOException(java.io.IOException) URL(java.net.URL) MediaWikiLanguage(org.eclipse.mylyn.wikitext.mediawiki.MediaWikiLanguage) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) HashSet(java.util.HashSet)

Example 3 with SplitOutlineItem

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

the class WikiToDocTask method createToc.

private void createToc(final Path path, OutlineItem rootItem) {
    SplittingMarkupToEclipseToc markupToEclipseToc = new SplittingMarkupToEclipseToc() {

        @Override
        protected String computeFile(OutlineItem item) {
            if (item instanceof SplitOutlineItem) {
                return computeTocRelativeFile(item, path);
            }
            return super.computeFile(item);
        }
    };
    markupToEclipseToc.setBookTitle(path.getTitle());
    markupToEclipseToc.setHtmlFile(computeTocRelativeFile(rootItem, path));
    String tocContents = markupToEclipseToc.createToc(rootItem);
    // $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
    File tocFile = new File(dest, path.name.replaceAll("[^a-zA-Z0-9]", "-") + "-toc.xml");
    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 : FileOutputStream(java.io.FileOutputStream) SplittingMarkupToEclipseToc(org.eclipse.mylyn.wikitext.splitter.SplittingMarkupToEclipseToc) SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) OutlineItem(org.eclipse.mylyn.wikitext.parser.outline.OutlineItem)

Example 4 with SplitOutlineItem

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

the class WikiToDocTask method markupToDoc.

private void markupToDoc(MarkupLanguage markupLanguage, Path path, String markupContent, Map<String, SplitOutlineItem> pathNameToOutline, Set<String> imageFilenames) throws BuildException {
    File htmlOutputFile = computeHtmlOutputFile(path);
    File pathDir = htmlOutputFile.getParentFile();
    if (!pathDir.exists()) {
        if (!pathDir.mkdirs()) {
            throw new BuildException(// $NON-NLS-1$
            MessageFormat.format(// $NON-NLS-1$
            Messages.getString("WikiToDocTask_cannot_create_dest_folder"), pathDir.getAbsolutePath()));
        }
    }
    Writer writer;
    try {
        // $NON-NLS-1$
        writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(htmlOutputFile)), "utf-8");
    } catch (Exception e) {
        throw new BuildException(// $NON-NLS-1$
        MessageFormat.format(// $NON-NLS-1$
        Messages.getString("WikiToDocTask_cannot_create_output_file"), // $NON-NLS-1$
        htmlOutputFile, e.getMessage()), e);
    }
    try {
        HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer, formatOutput);
        for (Stylesheet stylesheet : stylesheets) {
            HtmlDocumentBuilder.Stylesheet builderStylesheet = createBuilderStylesheet(pathDir, stylesheet);
            builder.addCssStylesheet(builderStylesheet);
        }
        builder.setTitle(computeTitle(path));
        builder.setEmitDtd(emitDoctype);
        if (emitDoctype && htmlDoctype != null) {
            builder.setHtmlDtd(htmlDoctype);
        }
        builder.setUseInlineStyles(useInlineCssStyles);
        builder.setSuppressBuiltInStyles(suppressBuiltInCssStyles);
        builder.setLinkRel(linkRel);
        builder.setDefaultAbsoluteLinkTarget(defaultAbsoluteLinkTarget);
        builder.setPrependImagePrefix(prependImagePrefix);
        builder.setXhtmlStrict(xhtmlStrict);
        MarkupLanguage markupLanguageClone = markupLanguage.clone();
        if (markupLanguageClone instanceof MediaWikiLanguage) {
            MediaWikiLanguage mediaWikiLanguage = (MediaWikiLanguage) markupLanguageClone;
            mediaWikiLanguage.setPageMapping(new PathPageMapping(path, paths, pathNameToOutline));
            if (imageFilenames != null) {
                mediaWikiLanguage.setImageNames(imageFilenames);
            }
        }
        SplitOutlineItem item = pathNameToOutline.get(path.name);
        SplittingHtmlDocumentBuilder splittingBuilder = new SplittingHtmlDocumentBuilder();
        splittingBuilder.setRootBuilder(builder);
        splittingBuilder.setOutline(item);
        splittingBuilder.setRootFile(htmlOutputFile);
        splittingBuilder.setNavigationImages(navigationImages);
        splittingBuilder.setFormatting(formatOutput);
        splittingBuilder.setNavigationImagePath(computeNavigationImagePath(pathDir));
        MarkupParser parser = new MarkupParser();
        parser.setMarkupLanguage(markupLanguageClone);
        parser.setBuilder(splittingBuilder);
        parser.parse(markupContent);
    } finally {
        try {
            writer.close();
        } catch (Exception e) {
            throw new BuildException(MessageFormat.format(// $NON-NLS-1$
            Messages.getString("WikiToDocTask_cannot_write_output_file"), // $NON-NLS-1$
            htmlOutputFile, e.getMessage()), e);
        }
    }
}
Also used : SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) SplittingHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.splitter.SplittingHtmlDocumentBuilder) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) FileOutputStream(java.io.FileOutputStream) MediaWikiLanguage(org.eclipse.mylyn.wikitext.mediawiki.MediaWikiLanguage) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) SplittingHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.splitter.SplittingHtmlDocumentBuilder) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 5 with SplitOutlineItem

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

the class WikiToDocTask method computeOutline.

protected SplitOutlineItem computeOutline(Path path, MarkupLanguage markupLanguage, File defaultFile, String markupContent) {
    SplittingStrategy splittingStrategy = multipleOutputFiles ? new DefaultSplittingStrategy() : new NoSplittingStrategy();
    SplittingOutlineParser outlineParser = new SplittingOutlineParser();
    outlineParser.setMarkupLanguage(markupLanguage);
    outlineParser.setSplittingStrategy(splittingStrategy);
    SplitOutlineItem item = outlineParser.parse(markupContent);
    item.setSplitTarget(defaultFile.getName());
    item.setLabel(computeTitle(path));
    return item;
}
Also used : SplittingStrategy(org.eclipse.mylyn.wikitext.splitter.SplittingStrategy) NoSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.NoSplittingStrategy) DefaultSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.DefaultSplittingStrategy) SplittingOutlineParser(org.eclipse.mylyn.wikitext.splitter.SplittingOutlineParser) NoSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.NoSplittingStrategy) SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) DefaultSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.DefaultSplittingStrategy)

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