Search in sources :

Example 6 with MarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage in project mylyn.docs by eclipse.

the class WikiTextTest method getMarkupLanguageForFilename.

@Test
public void getMarkupLanguageForFilename() {
    MarkupLanguage markupLanguage = WikiText.getMarkupLanguageForFilename("test.textile");
    assertNotNull(markupLanguage);
    assertEquals("Textile", markupLanguage.getName());
}
Also used : MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) Test(org.junit.Test)

Example 7 with MarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage in project mylyn.docs by eclipse.

the class WikiTextContextStructureBridge method getOutline.

private OutlineItem getOutline(IFile file) {
    // FIXME: is editor integration the way to go?? we probably need some kind of core model
    IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    if (editorPart != null) {
        OutlineItem outline = (OutlineItem) editorPart.getAdapter(OutlineItem.class);
        if (outline != null) {
            return outline;
        }
    }
    MarkupLanguage markupLanguage = WikiText.getMarkupLanguageForFilename(file.getName());
    if (markupLanguage != null) {
        OutlineParser parser = new OutlineParser(markupLanguage);
        try {
            String contents = getContents(file);
            OutlineItem outline = parser.parse(contents);
            outline.setResourcePath(file.getFullPath().toString());
            return outline;
        } catch (Exception e) {
            // ignore
            return null;
        }
    }
    return null;
}
Also used : OutlineParser(org.eclipse.mylyn.wikitext.parser.outline.OutlineParser) IEditorPart(org.eclipse.ui.IEditorPart) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) CoreException(org.eclipse.core.runtime.CoreException) OutlineItem(org.eclipse.mylyn.wikitext.parser.outline.OutlineItem)

Example 8 with MarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage in project mylyn.docs by eclipse.

the class ConvertMarkupToMarkup method handleFile.

@Override
protected void handleFile(ExecutionEvent event, IFile file, String name) throws ExecutionException {
    MarkupLanguage targetMmarkupLanguage = ServiceLocator.getInstance().getMarkupLanguage(event.getParameter(PARAM_MARKUP_LANGUAGE));
    // TODO: better way to get the file extension
    // $NON-NLS-1$ //$NON-NLS-2$
    String extension = targetMmarkupLanguage.getName().toLowerCase().replaceAll("\\W", "");
    // $NON-NLS-1$
    final IFile newFile = file.getParent().getFile(new Path(name + "." + extension));
    if (newFile.exists()) {
        if (!MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), NLS.bind(Messages.ConvertMarkupToMarkup_overwrite_file, targetMmarkupLanguage.getName()), NLS.bind(Messages.ConvertMarkupToMarkup_overwrite_file_detail, new Object[] { newFile.getFullPath() }))) {
            return;
        }
    }
    StringWriter writer = new StringWriter();
    MarkupParser parser = new MarkupParser();
    parser.setMarkupLanguage(markupLanguage);
    parser.setBuilder(targetMmarkupLanguage.createDocumentBuilder(writer));
    try {
        String inputContent = IOUtil.readFully(file);
        parser.parse(inputContent);
        final String targetConent = writer.toString();
        IRunnableWithProgress runnable = new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    if (newFile.exists()) {
                        // $NON-NLS-1$
                        newFile.setContents(// $NON-NLS-1$
                        new ByteArrayInputStream(targetConent.getBytes("utf-8")), // $NON-NLS-1$
                        false, // $NON-NLS-1$
                        true, monitor);
                    } else {
                        // $NON-NLS-1$
                        newFile.create(new ByteArrayInputStream(targetConent.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(NLS.bind(Messages.ConvertMarkupToMarkup_cannot_generate_detail, targetMmarkupLanguage.getName(), e.getMessage()));
        out.println(Messages.ConvertMarkupToMarkup_details_follow);
        e.printStackTrace(out);
        out.close();
        MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), NLS.bind(Messages.ConvertMarkupToMarkup_cannot_generate_title, targetMmarkupLanguage.getName()), message.toString());
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ExecutionException(org.eclipse.core.commands.ExecutionException) 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) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser) PrintWriter(java.io.PrintWriter)

Example 9 with MarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage 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 10 with MarkupLanguage

use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage in project mylyn.docs by eclipse.

the class WikiTextUiPlugin method getTemplates.

/**
 * get templates mapped by their markup language name
 *
 * @return the templates
 */
public Map<String, Templates> getTemplates() {
    if (templates == null) {
        Map<String, Templates> templates = new HashMap<>();
        IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(getPluginId(), EXTENSION_POINT_CONTENT_ASSIST);
        if (extensionPoint != null) {
            IConfigurationElement[] configurationElements = extensionPoint.getConfigurationElements();
            for (IConfigurationElement element : configurationElements) {
                String declaringPluginId = element.getDeclaringExtension().getContributor().getName();
                if (EXTENSION_POINT_TEMPLATES.equals(element.getName())) {
                    try {
                        // $NON-NLS-1$
                        String markupLanguage = element.getAttribute("markupLanguage");
                        if (markupLanguage == null) {
                            throw new Exception(Messages.WikiTextUiPlugin_markupLanguageRequired);
                        } else if (!WikiText.getMarkupLanguageNames().contains(markupLanguage)) {
                            throw new Exception(NLS.bind(Messages.WikiTextUiPlugin_invalidMarkupLanguage, new Object[] { markupLanguage }));
                        }
                        Templates markupLanguageTemplates = new Templates();
                        markupLanguageTemplates.setMarkupLanguageName(markupLanguage);
                        for (IConfigurationElement templatesChild : element.getChildren()) {
                            if (EXTENSION_POINT_TEMPLATE.equals(templatesChild.getName())) {
                                try {
                                    // process the template
                                    // $NON-NLS-1$
                                    String name = templatesChild.getAttribute("name");
                                    // $NON-NLS-1$
                                    String description = templatesChild.getAttribute("description");
                                    // $NON-NLS-1$
                                    String content = templatesChild.getAttribute("content");
                                    // $NON-NLS-1$
                                    String autoInsert = templatesChild.getAttribute("autoInsert");
                                    // $NON-NLS-1$
                                    String block = templatesChild.getAttribute("block");
                                    if (name == null || name.length() == 0) {
                                        throw new Exception(NLS.bind(Messages.WikiTextUiPlugin_nameRequired, new Object[] { EXTENSION_POINT_TEMPLATE }));
                                    }
                                    if (description == null || description.length() == 0) {
                                        throw new Exception(NLS.bind(Messages.WikiTextUiPlugin_descriptionRequired, new Object[] { EXTENSION_POINT_TEMPLATE }));
                                    }
                                    if (content == null || content.length() == 0) {
                                        throw new Exception(NLS.bind(Messages.WikiTextUiPlugin_contentRequired, new Object[] { EXTENSION_POINT_TEMPLATE }));
                                    }
                                    // $NON-NLS-1$//$NON-NLS-2$
                                    content = content.replace("\\t", "\t");
                                    content = // $NON-NLS-1$
                                    content.replace("\\r\\n", Text.DELIMITER).replace(// $NON-NLS-1$
                                    "\\r", Text.DELIMITER).replace("\\n", // $NON-NLS-1$
                                    Text.DELIMITER).replace("\\\\", // $NON-NLS-1$ //$NON-NLS-2$
                                    "\\");
                                    if (// $NON-NLS-1$
                                    content.endsWith("$") && !(content.endsWith("\\$") || content.endsWith("$$"))) {
                                        // $NON-NLS-1$ //$NON-NLS-2$
                                        content = content.substring(0, content.length() - 1);
                                    }
                                    if (content.startsWith("^")) {
                                        // $NON-NLS-1$
                                        content = content.substring(1);
                                    }
                                    // $NON-NLS-1$ //$NON-NLS-2$
                                    content = content.replace("\\$", "$$");
                                    markupLanguageTemplates.addTemplate(new Template(name, description, MarkupTemplateCompletionProcessor.CONTEXT_ID, content, autoInsert == null || // $NON-NLS-1$
                                    !"false".equalsIgnoreCase(autoInsert)), // $NON-NLS-1$
                                    block != null && "true".equalsIgnoreCase(block));
                                } catch (Exception e) {
                                    log(IStatus.ERROR, NLS.bind(Messages.WikiTextUiPlugin_invalidExtension, new Object[] { declaringPluginId, EXTENSION_POINT_CONTENT_ASSIST, e.getMessage() }), e);
                                }
                            } else {
                                log(IStatus.ERROR, NLS.bind(Messages.WikiTextUiPlugin_unexpectedExtensionElement, new Object[] { declaringPluginId, EXTENSION_POINT_CONTENT_ASSIST, templatesChild.getName() }), null);
                            }
                        }
                        Templates previous = templates.put(markupLanguageTemplates.getMarkupLanguageName(), markupLanguageTemplates);
                        if (previous != null) {
                            markupLanguageTemplates.addAll(previous);
                        }
                    } catch (Exception e) {
                        log(IStatus.ERROR, NLS.bind(Messages.WikiTextUiPlugin_invalidExtension, new Object[] { declaringPluginId, EXTENSION_POINT_TEMPLATES, e.getMessage() }), e);
                    }
                } else {
                    log(IStatus.ERROR, NLS.bind(Messages.WikiTextUiPlugin_unexpectedExtensionElement, new Object[] { declaringPluginId, EXTENSION_POINT_CONTENT_ASSIST, element.getName() }), null);
                }
            }
        }
        // now that we have the basic templates, check for language extensions and connect the hierarchy
        // first ensure that all language names have templates defined
        Set<String> languageNames = WikiText.getMarkupLanguageNames();
        for (String languageName : languageNames) {
            Templates languageTemplates = templates.get(languageName);
            if (languageTemplates == null) {
                languageTemplates = new Templates();
                templates.put(languageName, languageTemplates);
            }
        }
        // next connect the hierarchy
        for (String languageName : languageNames) {
            MarkupLanguage markupLanguage = WikiText.getMarkupLanguage(languageName);
            if (markupLanguage != null && markupLanguage.getExtendsLanguage() != null) {
                Templates languageTemplates = templates.get(languageName);
                Templates parentLanguageTemplates = templates.get(markupLanguage.getExtendsLanguage());
                languageTemplates.setParent(parentLanguageTemplates);
            }
        }
        this.templates = Collections.unmodifiableMap(templates);
    }
    return templates;
}
Also used : IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) HashMap(java.util.HashMap) Templates(org.eclipse.mylyn.internal.wikitext.ui.editor.assist.Templates) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) CoreException(org.eclipse.core.runtime.CoreException) Template(org.eclipse.jface.text.templates.Template)

Aggregations

MarkupLanguage (org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage)60 Test (org.junit.Test)18 AbstractMarkupLanguage (org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage)15 MarkupParser (org.eclipse.mylyn.wikitext.parser.MarkupParser)8 File (java.io.File)7 BuildException (org.apache.tools.ant.BuildException)7 CoreException (org.eclipse.core.runtime.CoreException)7 MockMarkupLanguage (org.eclipse.mylyn.internal.wikitext.MockMarkupLanguage)7 StringWriter (java.io.StringWriter)6 HashMap (java.util.HashMap)5 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)5 FileSet (org.apache.tools.ant.types.FileSet)5 OutlineItem (org.eclipse.mylyn.wikitext.parser.outline.OutlineItem)5 Point (org.eclipse.swt.graphics.Point)5 IOException (java.io.IOException)4 IFile (org.eclipse.core.resources.IFile)4 IExtensionPoint (org.eclipse.core.runtime.IExtensionPoint)4 TreeMap (java.util.TreeMap)3 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3