Search in sources :

Example 26 with HtmlDocumentBuilder

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

the class SplittingHtmlDocumentBuilder method beginHeading.

@Override
public void beginHeading(int level, Attributes attributes) {
    SplitOutlineItem item = outline.getOutlineItemById(attributes.getId());
    if (item != null && !currentFile.getName().equals(item.getSplitTarget())) {
        try {
            documentFooter();
            out.endDocument();
            if (writer != null) {
                writer.close();
                writer = null;
            }
            currentFile = new File(rootFile.getParent(), item.getSplitTarget());
            // $NON-NLS-1$
            writer = new OutputStreamWriter(new FileOutputStream(currentFile), "UTF-8");
            HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer, formatting);
            rootBuilder.copyConfiguration(builder);
            if (item.getLabel() != null) {
                String title = rootBuilder.getTitle();
                if (title == null) {
                    title = item.getLabel();
                } else {
                    // $NON-NLS-1$
                    title += " - " + item.getLabel();
                }
                builder.setTitle(title);
            }
            out = builder;
            out.beginDocument();
            documentHeader();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    out.beginHeading(level, attributes);
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)

Example 27 with HtmlDocumentBuilder

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

the class ConvertMarkupToHtml method handleFile.

@Override
protected void handleFile(final IFile file, String name) {
    // $NON-NLS-1$
    final IFile newFile = file.getParent().getFile(new Path(name + ".html"));
    if (newFile.exists()) {
        if (!MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.ConvertMarkupToHtml_overwrite, NLS.bind(Messages.ConvertMarkupToHtml_fileExistsOverwrite, new Object[] { newFile.getFullPath() }))) {
            return;
        }
    }
    final StringWriter writer = new StringWriter();
    HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer, true);
    final MarkupParser parser = new MarkupParser();
    parser.setMarkupLanguage(markupLanguage);
    parser.setBuilder(builder);
    builder.setEmitDtd(true);
    try {
        IRunnableWithProgress runnable = new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    String inputContent = IOUtil.readFully(file);
                    parser.parse(inputContent);
                    String xhtmlContent = writer.toString();
                    if (newFile.exists()) {
                        // $NON-NLS-1$
                        newFile.setContents(// $NON-NLS-1$
                        new ByteArrayInputStream(xhtmlContent.getBytes("utf-8")), // $NON-NLS-1$
                        false, // $NON-NLS-1$
                        true, monitor);
                    } else {
                        // $NON-NLS-1$
                        newFile.create(new ByteArrayInputStream(xhtmlContent.getBytes("utf-8")), false, monitor);
                    }
                    newFile.setCharset(StandardCharsets.UTF_8.name(), 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.ConvertMarkupToHtml_cannotConvert + e.getMessage());
        out.println(Messages.ConvertMarkupToHtml_detailsFollow);
        e.printStackTrace(out);
        out.close();
        MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.ConvertMarkupToHtml_cannotCompleteOperation, message.toString());
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) 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) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser) PrintWriter(java.io.PrintWriter)

Example 28 with HtmlDocumentBuilder

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

the class AsciiDocIdGenerationStrategy method parseToHtml.

private static String parseToHtml(String markup) {
    MarkupParser localParser = new MarkupParser(new AsciiDocLanguage());
    StringWriter out = new StringWriter();
    HtmlDocumentBuilder builder = new HtmlDocumentBuilder(out);
    builder.setEmitAsDocument(false);
    localParser.setBuilder(builder);
    localParser.parse(markup);
    return out.toString();
}
Also used : StringWriter(java.io.StringWriter) AsciiDocLanguage(org.eclipse.mylyn.wikitext.asciidoc.AsciiDocLanguage) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 29 with HtmlDocumentBuilder

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

the class SplittingHtmlDocumentBuilderTest method generateContents.

private void generateContents(String markup, boolean embeddedTableOfContents) throws IOException, FileNotFoundException {
    try (OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(outputFile), StandardCharsets.UTF_8)) {
        rootBuilder = new HtmlDocumentBuilder(out, true);
        SplittingOutlineParser outlineParser = new SplittingOutlineParser();
        outlineParser.setMarkupLanguage(new TextileLanguage());
        outlineParser.setSplittingStrategy(new DefaultSplittingStrategy());
        SplitOutlineItem outline = outlineParser.parse(markup);
        outline.setSplitTarget(outputFile.getName());
        builder.setEmbeddedTableOfContents(embeddedTableOfContents);
        builder.setRootBuilder(rootBuilder);
        builder.setRootFile(outputFile);
        builder.setFormatting(true);
        builder.setOutline(outline);
        MarkupParser parser = new MarkupParser(new TextileLanguage());
        parser.setBuilder(builder);
        parser.parse(markup);
    }
}
Also used : TextileLanguage(org.eclipse.mylyn.wikitext.textile.TextileLanguage) FileOutputStream(java.io.FileOutputStream) SplittingOutlineParser(org.eclipse.mylyn.wikitext.splitter.SplittingOutlineParser) OutputStreamWriter(java.io.OutputStreamWriter) SplitOutlineItem(org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) SplittingHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.splitter.SplittingHtmlDocumentBuilder) DefaultSplittingStrategy(org.eclipse.mylyn.wikitext.splitter.DefaultSplittingStrategy) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 30 with HtmlDocumentBuilder

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

the class AbstractSourceSpanTest method assertParseToHtml.

public void assertParseToHtml(String expected, String markup) {
    StringWriter writer = new StringWriter();
    HtmlDocumentBuilder builder = new SimplifiedHtmlDocumentBuilder(writer);
    builder.setEmitAsDocument(false);
    builder.beginDocument();
    InlineParser parser = new InlineParser(span, new AllCharactersSpan());
    List<Inline> inlines = parser.parse(ProcessingContext.builder().build(), new TextSegment(ImmutableList.of(new Line(1, 0, markup))));
    for (Inline inline : inlines) {
        inline.emit(builder);
    }
    builder.endDocument();
    assertEquals(expected, writer.toString());
}
Also used : Line(org.eclipse.mylyn.wikitext.commonmark.internal.Line) StringWriter(java.io.StringWriter) SimplifiedHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.commonmark.internal.spec.SimplifiedHtmlDocumentBuilder) TextSegment(org.eclipse.mylyn.wikitext.commonmark.internal.TextSegment) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) SimplifiedHtmlDocumentBuilder(org.eclipse.mylyn.wikitext.commonmark.internal.spec.SimplifiedHtmlDocumentBuilder)

Aggregations

HtmlDocumentBuilder (org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder)58 StringWriter (java.io.StringWriter)46 Test (org.junit.Test)17 MarkupParser (org.eclipse.mylyn.wikitext.parser.MarkupParser)16 SplittingHtmlDocumentBuilder (org.eclipse.mylyn.wikitext.splitter.SplittingHtmlDocumentBuilder)10 AbstractMarkupGenerationTest (org.eclipse.mylyn.wikitext.toolkit.AbstractMarkupGenerationTest)10 File (java.io.File)8 Writer (java.io.Writer)7 StringReader (java.io.StringReader)6 Attributes (org.eclipse.mylyn.wikitext.parser.Attributes)6 SplitOutlineItem (org.eclipse.mylyn.wikitext.splitter.SplitOutlineItem)6 OutputStreamWriter (java.io.OutputStreamWriter)5 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)4 TextileLanguage (org.eclipse.mylyn.wikitext.textile.TextileLanguage)4 PrintWriter (java.io.PrintWriter)3 LinkAttributes (org.eclipse.mylyn.wikitext.parser.LinkAttributes)3 DefaultSplittingStrategy (org.eclipse.mylyn.wikitext.splitter.DefaultSplittingStrategy)3 SplittingOutlineParser (org.eclipse.mylyn.wikitext.splitter.SplittingOutlineParser)3 Before (org.junit.Before)3