Search in sources :

Example 1 with MarkupParser

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

the class MarkupToOPS method parse.

/**
 * Parses the markup file and populates the publication with the result.
 *
 * @param ops
 *            the publication the content will be added to
 * @param markupFile
 *            the WikiText markup file
 * @return the temporary folder used for generating the HTML from markup
 * @since 2.0
 */
public File parse(Publication ops, File markupFile) throws IOException, FileNotFoundException {
    if (markupLanguage == null) {
        // $NON-NLS-1$
        throw new IllegalStateException("must set markupLanguage");
    }
    // Create a temporary working folder
    // $NON-NLS-1$
    File workingFolder = File.createTempFile("wikitext_", null);
    if (workingFolder.delete() && workingFolder.mkdirs()) {
        // $NON-NLS-1$
        File htmlFile = new File(workingFolder.getAbsolutePath() + File.separator + "markup.html");
        FileWriter out = new FileWriter(htmlFile);
        HtmlDocumentBuilder builder = new HtmlDocumentBuilder(out) {

            @Override
            protected XmlStreamWriter createXmlStreamWriter(Writer out) {
                return super.createFormattingXmlStreamWriter(out);
            }
        };
        List<Item> stylesheets = ops.getItemsByMIMEType(Publication.MIMETYPE_CSS);
        for (Item item : stylesheets) {
            File file = new File(item.getFile());
            Stylesheet css = new Stylesheet(file);
            builder.addCssStylesheet(css);
        }
        // Make sure we get the correct XHTML header
        builder.setEmitDtd(true);
        builder.setHtmlDtd(// $NON-NLS-1$
        "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
        builder.setXhtmlStrict(true);
        MarkupParser markupParser = new MarkupParser();
        markupParser.setBuilder(builder);
        markupParser.setMarkupLanguage(markupLanguage);
        markupParser.parse(new FileReader(markupFile));
        ops.setGenerateToc(true);
        ops.setIncludeReferencedResources(true);
        Item item = ops.addItem(htmlFile);
        item.setSourcePath(markupFile.getAbsolutePath());
    }
    return workingFolder;
}
Also used : Item(org.eclipse.mylyn.docs.epub.opf.Item) FileWriter(java.io.FileWriter) FileReader(java.io.FileReader) File(java.io.File) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) XmlStreamWriter(org.eclipse.mylyn.wikitext.util.XmlStreamWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) Stylesheet(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder.Stylesheet) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 2 with MarkupParser

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

the class MarkupToDitaTask method processFile.

private void processFile(MarkupLanguage markupLanguage, final File baseDir, final File source) throws BuildException {
    // $NON-NLS-1$
    log(MessageFormat.format(Messages.getString("MarkupToDitaTask.8"), source), Project.MSG_VERBOSE);
    String markupContent = null;
    String name = source.getName();
    if (name.lastIndexOf('.') != -1) {
        name = name.substring(0, name.lastIndexOf('.'));
    }
    // $NON-NLS-1$
    File outputFile = new File(source.getParentFile(), filenameFormat.replace("$1", name));
    if (!outputFile.exists() || overwrite || outputFile.lastModified() < source.lastModified()) {
        if (markupContent == null) {
            markupContent = readFully(source);
        }
        performValidation(source, markupContent);
        OutlineItem outline = new OutlineParser(markupLanguage).parse(markupContent);
        Writer writer;
        try {
            // $NON-NLS-1$
            writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(outputFile)), "utf-8");
        } catch (Exception e) {
            throw new BuildException(// $NON-NLS-1$
            MessageFormat.format(// $NON-NLS-1$
            Messages.getString("MarkupToDitaTask.11"), // $NON-NLS-1$
            outputFile, e.getMessage()), e);
        }
        try {
            if (topicStrategy == BreakStrategy.NONE) {
                DitaTopicDocumentBuilder builder = new DitaTopicDocumentBuilder(new DefaultXmlStreamWriter(writer), formatting);
                builder.setRootTopicTitle(bookTitle);
                MarkupParser parser = new MarkupParser();
                parser.setMarkupLanguage(markupLanguage);
                parser.setBuilder(builder);
                if (topicDoctype != null) {
                    builder.setDoctype(topicDoctype);
                }
                builder.setFilename(outputFile.getName());
                builder.setOutline(outline);
                parser.parse(markupContent);
            } else {
                DitaBookMapDocumentBuilder builder = new DitaBookMapDocumentBuilder(formatting ? new FormattingXMLStreamWriter(new DefaultXmlStreamWriter(writer)) : new DefaultXmlStreamWriter(writer));
                try {
                    builder.setFormattingDependencies(formatting);
                    MarkupParser parser = new MarkupParser();
                    parser.setMarkupLanguage(markupLanguage);
                    parser.setBuilder(builder);
                    builder.setBookTitle(bookTitle == null ? name : bookTitle);
                    if (doctype != null) {
                        builder.setDoctype(doctype);
                    }
                    if (topicDoctype != null) {
                        builder.setTopicDoctype(topicDoctype);
                    }
                    builder.setTargetFile(outputFile);
                    builder.setTopicFolder(topicFolder);
                    builder.setOutline(outline);
                    switch(topicStrategy) {
                        case FIRST:
                            if (!outline.getChildren().isEmpty()) {
                                builder.setTopicBreakLevel(outline.getChildren().get(0).getLevel());
                            } else {
                                builder.setTopicBreakLevel(1);
                            }
                            break;
                        case LEVEL1:
                            builder.setTopicBreakLevel(1);
                            break;
                    }
                    parser.parse(markupContent);
                } finally {
                    try {
                        builder.close();
                    } catch (IOException e) {
                        throw new BuildException(MessageFormat.format(// $NON-NLS-1$
                        Messages.getString("MarkupToDitaTask.12"), // $NON-NLS-1$
                        outputFile, e.getMessage()), e);
                    }
                }
            }
        } finally {
            try {
                writer.close();
            } catch (Exception e) {
                throw new BuildException(MessageFormat.format(// $NON-NLS-1$
                Messages.getString("MarkupToDitaTask.12"), // $NON-NLS-1$
                outputFile, e.getMessage()), e);
            }
        }
    }
}
Also used : DitaBookMapDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.DitaBookMapDocumentBuilder) FormattingXMLStreamWriter(org.eclipse.mylyn.wikitext.util.FormattingXMLStreamWriter) DitaTopicDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.DitaTopicDocumentBuilder) DefaultXmlStreamWriter(org.eclipse.mylyn.wikitext.util.DefaultXmlStreamWriter) IOException(java.io.IOException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) OutlineItem(org.eclipse.mylyn.wikitext.parser.outline.OutlineItem) OutlineParser(org.eclipse.mylyn.wikitext.parser.outline.OutlineParser) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FormattingXMLStreamWriter(org.eclipse.mylyn.wikitext.util.FormattingXMLStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) DefaultXmlStreamWriter(org.eclipse.mylyn.wikitext.util.DefaultXmlStreamWriter) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 3 with MarkupParser

use of org.eclipse.mylyn.wikitext.parser.MarkupParser 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 4 with MarkupParser

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

the class HtmlTextPresentationParserTest method testSignificantWhitespaceLossBug236367.

/**
 * Test for bug# 236367
 */
@Test
public void testSignificantWhitespaceLossBug236367() throws SAXException, IOException {
    String html = new MarkupParser(new TextileLanguage()).parseToHtml("one *two* three *four* five *six* seven");
    parser.parse(html);
    String text = parser.getText();
    assertTrue(text.contains("one two three four five six seven"));
}
Also used : TextileLanguage(org.eclipse.mylyn.wikitext.textile.TextileLanguage) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser) Test(org.junit.Test)

Example 5 with MarkupParser

use of org.eclipse.mylyn.wikitext.parser.MarkupParser 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)

Aggregations

MarkupParser (org.eclipse.mylyn.wikitext.parser.MarkupParser)55 StringWriter (java.io.StringWriter)24 HtmlDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)17 Writer (java.io.Writer)13 Test (org.junit.Test)10 File (java.io.File)8 MarkupLanguage (org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage)8 TextileLanguage (org.eclipse.mylyn.wikitext.textile.TextileLanguage)8 OutputStreamWriter (java.io.OutputStreamWriter)7 FileOutputStream (java.io.FileOutputStream)6 BufferedOutputStream (java.io.BufferedOutputStream)5 IOException (java.io.IOException)5 BuildException (org.apache.tools.ant.BuildException)5 IFile (org.eclipse.core.resources.IFile)4 EventDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.EventDocumentBuilder)4 PrintWriter (java.io.PrintWriter)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HtmlLanguage (org.eclipse.mylyn.wikitext.html.HtmlLanguage)3 Attributes (org.eclipse.mylyn.wikitext.parser.Attributes)3