Search in sources :

Example 1 with MarkupToEclipseToc

use of org.eclipse.mylyn.wikitext.parser.util.MarkupToEclipseToc in project mylyn.docs by eclipse.

the class MarkupToEclipseHelpTask method processed.

@Override
void processed(String markupContent, SplitOutlineItem item, final File baseDir, final File source) {
    super.processed(markupContent, item, baseDir, source);
    String name = source.getName();
    if (name.lastIndexOf('.') != -1) {
        name = name.substring(0, name.lastIndexOf('.'));
    }
    File tocOutputFile = computeTocFile(source, name);
    if (!tocOutputFile.exists() || overwrite || tocOutputFile.lastModified() < source.lastModified()) {
        File htmlOutputFile = computeHtmlFile(source, name);
        Writer writer;
        try {
            // $NON-NLS-1$
            writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(tocOutputFile)), "utf-8");
        } catch (Exception e) {
            throw new // $NON-NLS-1$
            BuildException(// $NON-NLS-1$
            String.format("Cannot write to file '%s': %s", tocOutputFile, e.getMessage()), e);
        }
        try {
            MarkupToEclipseToc toEclipseToc = new SplittingMarkupToEclipseToc();
            toEclipseToc.setHelpPrefix(helpPrefix);
            toEclipseToc.setAnchorLevel(tocAnchorLevel);
            // $NON-NLS-1$//$NON-NLS-2$
            System.out.println("Help: " + baseDir + " " + htmlOutputFile);
            toEclipseToc.setBookTitle(title == null ? name : title);
            toEclipseToc.setCopyrightNotice(getCopyrightNotice());
            String basePath = baseDir.getAbsolutePath().replace('\\', '/');
            String outputFilePath = htmlOutputFile.getAbsolutePath().replace('\\', '/');
            if (outputFilePath.startsWith(basePath)) {
                String filePath = outputFilePath.substring(basePath.length());
                if (filePath.startsWith("/")) {
                    // $NON-NLS-1$
                    filePath = filePath.substring(1);
                }
                if (filePath.lastIndexOf('/') != -1) {
                    String relativePart = filePath.substring(0, filePath.lastIndexOf('/'));
                    toEclipseToc.setHelpPrefix(helpPrefix == null ? relativePart : helpPrefix + '/' + relativePart);
                }
            }
            toEclipseToc.setHtmlFile(htmlOutputFile.getName());
            String tocXml = toEclipseToc.createToc(item);
            try {
                writer.write(tocXml);
            } catch (Exception e) {
                // $NON-NLS-1$
                throw new BuildException(String.format("Cannot write to file '%s': %s", tocXml, e.getMessage()), e);
            }
        } finally {
            try {
                writer.close();
            } catch (Exception e) {
                throw new BuildException(// $NON-NLS-1$
                String.format(// $NON-NLS-1$
                "Cannot write to file '%s': %s", // $NON-NLS-1$
                tocOutputFile, e.getMessage()), e);
            }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) SplittingMarkupToEclipseToc(org.eclipse.mylyn.wikitext.splitter.SplittingMarkupToEclipseToc) OutputStreamWriter(java.io.OutputStreamWriter) MarkupToEclipseToc(org.eclipse.mylyn.wikitext.parser.util.MarkupToEclipseToc) SplittingMarkupToEclipseToc(org.eclipse.mylyn.wikitext.splitter.SplittingMarkupToEclipseToc) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException)

Example 2 with MarkupToEclipseToc

use of org.eclipse.mylyn.wikitext.parser.util.MarkupToEclipseToc in project mylyn.docs by eclipse.

the class MarkupToEclipseHelpMojo method createMarkupToEclipseToc.

protected MarkupToEclipseToc createMarkupToEclipseToc(String relativePath, File htmlOutputFile, String name) {
    MarkupToEclipseToc toEclipseToc = new SplittingMarkupToEclipseToc();
    toEclipseToc.setBookTitle(title == null ? name : title);
    toEclipseToc.setCopyrightNotice(copyrightNotice);
    toEclipseToc.setAnchorLevel(tocAnchorLevel);
    toEclipseToc.setHelpPrefix(calculateHelpPrefix(relativePath));
    toEclipseToc.setHtmlFile(htmlOutputFile.getName());
    return toEclipseToc;
}
Also used : SplittingMarkupToEclipseToc(org.eclipse.mylyn.wikitext.splitter.SplittingMarkupToEclipseToc) MarkupToEclipseToc(org.eclipse.mylyn.wikitext.parser.util.MarkupToEclipseToc) SplittingMarkupToEclipseToc(org.eclipse.mylyn.wikitext.splitter.SplittingMarkupToEclipseToc)

Example 3 with MarkupToEclipseToc

use of org.eclipse.mylyn.wikitext.parser.util.MarkupToEclipseToc in project mylyn.docs by eclipse.

the class MarkupToEclipseHelpMojo method createEclipseHelpToc.

private void createEclipseHelpToc(SplitOutlineItem rootTocItem, File sourceFile, String relativePath, File htmlOutputFile, String name) {
    File tocOutputFile = computeTocFile(htmlOutputFile, name);
    if (!tocOutputFile.exists() || tocOutputFile.lastModified() < sourceFile.lastModified()) {
        Writer writer = createWriter(tocOutputFile);
        try {
            MarkupToEclipseToc toEclipseToc = createMarkupToEclipseToc(relativePath, htmlOutputFile, name);
            String tocXml = toEclipseToc.createToc(rootTocItem);
            writer.write(tocXml);
        } catch (IOException e) {
            throw new BuildFailureException(format("Cannot write to file {0}: {1}", tocOutputFile, e.getMessage()), e);
        } finally {
            close(writer, tocOutputFile);
        }
    }
}
Also used : MarkupToEclipseToc(org.eclipse.mylyn.wikitext.parser.util.MarkupToEclipseToc) SplittingMarkupToEclipseToc(org.eclipse.mylyn.wikitext.splitter.SplittingMarkupToEclipseToc) IOException(java.io.IOException) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Example 4 with MarkupToEclipseToc

use of org.eclipse.mylyn.wikitext.parser.util.MarkupToEclipseToc in project mylyn.docs by eclipse.

the class MarkupToEclipseTocTest method setUp.

@Override
public void setUp() {
    markupToEclipseToc = new MarkupToEclipseToc();
    markupToEclipseToc.setMarkupLanguage(new TextileLanguage());
}
Also used : TextileLanguage(org.eclipse.mylyn.wikitext.textile.TextileLanguage) MarkupToEclipseToc(org.eclipse.mylyn.wikitext.parser.util.MarkupToEclipseToc)

Example 5 with MarkupToEclipseToc

use of org.eclipse.mylyn.wikitext.parser.util.MarkupToEclipseToc in project mylyn.docs by eclipse.

the class ConvertMarkupToEclipseHelp method handleFile.

@Override
protected void handleFile(final IFile file, String name) {
    super.handleFile(file, name);
    // $NON-NLS-1$
    final IFile newFile = file.getParent().getFile(new Path(name + "-toc.xml"));
    if (newFile.exists()) {
        if (!MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.ConvertMarkupToEclipseHelp_overwrite, NLS.bind(Messages.ConvertMarkupToEclipseHelp_fileExistsOverwrite, new Object[] { newFile.getFullPath() }))) {
            return;
        }
    }
    IPath parentFullPath = file.getParent().getFullPath();
    IPath pluginPathToHelp = parentFullPath.removeFirstSegments(1);
    final MarkupToEclipseToc markupToEclipseToc = new MarkupToEclipseToc();
    markupToEclipseToc.setMarkupLanguage(markupLanguage);
    markupToEclipseToc.setBookTitle(name);
    // $NON-NLS-1$
    String htmlFilePath = name + ".html";
    if (pluginPathToHelp.segmentCount() > 0) {
        String pathPart = pluginPathToHelp.toString();
        if (!pathPart.endsWith("/")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            pathPart = pathPart + "/";
        }
        htmlFilePath = pathPart + htmlFilePath;
    }
    markupToEclipseToc.setHtmlFile(htmlFilePath);
    try {
        IRunnableWithProgress runnable = new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    String content = IOUtil.readFully(file);
                    final String tocXml = markupToEclipseToc.parse(content);
                    if (newFile.exists()) {
                        // $NON-NLS-1$
                        newFile.setContents(// $NON-NLS-1$
                        new ByteArrayInputStream(tocXml.getBytes("utf-8")), // $NON-NLS-1$
                        false, // $NON-NLS-1$
                        true, monitor);
                    } else {
                        // $NON-NLS-1$
                        newFile.create(new ByteArrayInputStream(tocXml.getBytes("utf-8")), false, monitor);
                    }
                    // $NON-NLS-1$
                    newFile.setCharset("utf-8", monitor);
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        };
        try {
            PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
        } catch (InterruptedException e) {
            return;
        } catch (InvocationTargetException e) {
            throw e.getCause();
        }
    } catch (Throwable e) {
        StringWriter message = new StringWriter();
        PrintWriter out = new PrintWriter(message);
        out.println(Messages.ConvertMarkupToEclipseHelp_cannotConvert + e.getMessage());
        out.println(Messages.ConvertMarkupToEclipseHelp_detailsFollow);
        e.printStackTrace(out);
        out.close();
        MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.ConvertMarkupToEclipseHelp_cannotCompleteOperation, message.toString());
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) MarkupToEclipseToc(org.eclipse.mylyn.wikitext.parser.util.MarkupToEclipseToc) PrintWriter(java.io.PrintWriter)

Aggregations

MarkupToEclipseToc (org.eclipse.mylyn.wikitext.parser.util.MarkupToEclipseToc)7 File (java.io.File)4 SplittingMarkupToEclipseToc (org.eclipse.mylyn.wikitext.splitter.SplittingMarkupToEclipseToc)3 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 Test (org.junit.Test)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 BuildException (org.apache.tools.ant.BuildException)1 IFile (org.eclipse.core.resources.IFile)1 IPath (org.eclipse.core.runtime.IPath)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Path (org.eclipse.core.runtime.Path)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1 TextileLanguage (org.eclipse.mylyn.wikitext.textile.TextileLanguage)1