Search in sources :

Example 26 with MarkupParser

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

the class TWikiLanguageTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    language = new TWikiLanguage();
    parser = new MarkupParser(language);
}
Also used : TWikiLanguage(org.eclipse.mylyn.wikitext.twiki.TWikiLanguage) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 27 with MarkupParser

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

the class HelpContent method getContent.

/**
 * Get the help content, which may be formatted using HTML markup. If HTML markup is used, the content must be
 * well-formed HTML.
 */
@SuppressWarnings("serial")
public String getContent() throws IOException {
    try {
        URL resource = getResource();
        String content = Resources.toString(resource, StandardCharsets.UTF_8);
        if (resourceContentLanguage == null || "html".equalsIgnoreCase(resourceContentLanguage)) {
            // $NON-NLS-1$
            return content;
        }
        MarkupLanguage markupLanguage = WikiText.getMarkupLanguage(resourceContentLanguage);
        if (markupLanguage == null) {
            throw new IOException(NLS.bind(Messages.HelpContent_noSuchMarkupLanguage, new Object[] { resourceContentLanguage }));
        }
        MarkupParser markupParser = new MarkupParser(markupLanguage);
        return markupParser.parseToHtml(content);
    } catch (final Exception e) {
        throw new IOException(NLS.bind(Messages.HelpContent_cannotAccessContent, new Object[] { provider.getSymbolicName(), resourcePath, e.getMessage() })) {

            @Override
            public Throwable getCause() {
                return e;
            }
        };
    }
}
Also used : MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 28 with MarkupParser

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

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

the class TextileToHubspotConverter method main.

public static void main(String[] args) throws IOException {
    final List<String> inFileNames = new ArrayList<String>();
    String outFilename = null;
    String p1Filename = null;
    String contentsTemplateFilename = null;
    String p2Filename = null;
    String tocTemplateFilename = null;
    String p3Filename = null;
    final Map<String, String> properties = new HashMap<String, String>();
    /*
		 * Parse command-line parameters:
		 */
    for (int i = 0; i < args.length; i++) {
        final String arg = args[i];
        if (arg.equals("-p1") || arg.equals("--p1-file")) {
            p1Filename = args[++i];
            continue;
        }
        if (arg.equals("-c") || arg.equals("--contents-template-file")) {
            contentsTemplateFilename = args[++i];
            continue;
        }
        if (arg.equals("-p2") || arg.equals("--p2-file")) {
            p2Filename = args[++i];
            continue;
        }
        if (arg.equals("-t") || arg.equals("--toc-template-file")) {
            tocTemplateFilename = args[++i];
            continue;
        }
        if (arg.equals("-p3") || arg.equals("--p3-file")) {
            p3Filename = args[++i];
            continue;
        }
        if (arg.equals("-i") || arg.equals("--input")) {
            inFileNames.add(args[++i]);
            continue;
        }
        if (arg.equals("-o") || arg.equals("--output")) {
            outFilename = args[++i];
            continue;
        }
        if (arg.equals("-p") || arg.equals("--property")) {
            final String nameValue = args[++i];
            addTo(properties, nameValue);
            continue;
        }
    }
    assertParameterIsSpecified(p1Filename, "P1 file not specified.");
    assertParameterIsSpecified(contentsTemplateFilename, "Contents template file not specified.");
    assertParameterIsSpecified(p2Filename, "P2 file not specified.");
    assertParameterIsSpecified(tocTemplateFilename, "TOC template file not specified.");
    assertParameterIsSpecified(p3Filename, "P3 file not specified.");
    assertParameterIsSpecified(inFileNames, "Input file(s) not specified.");
    assertParameterIsSpecified(outFilename, "Output file not specified.");
    /*
		 * Setup the TOC document builder:
		 */
    final TableOfContentsBuilder tocBuilder = new TableOfContentsBuilder();
    /*
		 * Concatenate all input files so that the Textile parser sees
		 * everything as one large input file:
		 */
    final Vector<Reader> readers = new Vector<Reader>(inFileNames.size());
    for (final String inFileName : inFileNames) readers.addElement(new FileReader(inFileName));
    Reader in = new SequenceReader(new Vector<Reader>(readers).elements());
    /*
		 * Create, configure, and run the Textile markup parser. It will call
		 * back into the table of contents builder, which will create the table
		 * of contents.
		 */
    MarkupParser markupParser = new MarkupParser();
    markupParser.setMarkupLanguage(ServiceLocator.getInstance().getMarkupLanguage("Textile"));
    markupParser.setBuilder(tocBuilder);
    markupParser.parse(in);
    List<Heading> headings = tocBuilder.getHeadings();
    /*
		 * Setup everything for the Hubspot document builder and process files:
		 */
    final Reader p1 = createFileReader(p1Filename, "P1 file not found");
    final Reader contentsTemplate = createFileReader(contentsTemplateFilename, "Contents template file not found");
    final Reader p2 = createFileReader(p2Filename, "P2 file not found");
    final Reader tocTemplate = createFileReader(tocTemplateFilename, "TOC template file not found");
    final Reader p3 = createFileReader(p3Filename, "P3 file not found");
    final Writer out = new FileWriter(outFilename);
    final HubspotDocumentBuilder docBuilder = new HubspotDocumentBuilder(p1, contentsTemplate, p2, tocTemplate, p3, properties, headings, out);
    /*
		 * Concatenate all input files so that the Textile parser sees
		 * everything as one large input file:
		 */
    /*
		 * Concatenate all input files so that the Textile parser sees
		 * everything as one large input file:
		 */
    readers.clear();
    for (final String inFileName : inFileNames) readers.addElement(new FileReader(inFileName));
    in = new SequenceReader(new Vector<Reader>(readers).elements());
    /*
		 * Create, configure, and run the Textile markup parser. It will call
		 * back into the document builder, which will construct the output
		 * document.
		 */
    markupParser = new MarkupParser();
    markupParser.setMarkupLanguage(ServiceLocator.getInstance().getMarkupLanguage("Textile"));
    markupParser.setBuilder(docBuilder);
    markupParser.parse(in);
    out.close();
}
Also used : HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) Vector(java.util.Vector) FileWriter(java.io.FileWriter) Writer(java.io.Writer) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

Example 30 with MarkupParser

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

the class MarkupToXslfoTask method processFile.

/**
 * process the file
 *
 * @param baseDir
 * @param source
 * @return
 * @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("MarkupToXslfoTask.7"), source), Project.MSG_VERBOSE);
    String markupContent = null;
    String name = source.getName();
    if (name.lastIndexOf('.') != -1) {
        name = name.substring(0, name.lastIndexOf('.'));
    }
    File outputFile = computeXslfoFile(source, name);
    if (targetdir != null) {
        outputFile = new File(targetdir, outputFile.getName());
    }
    if (!outputFile.exists() || overwrite || outputFile.lastModified() < source.lastModified()) {
        if (markupContent == null) {
            markupContent = readFully(source);
        }
        performValidation(source, markupContent);
        Writer out;
        try {
            // $NON-NLS-1$
            out = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(outputFile)), "utf-8");
        } catch (Exception e) {
            throw new BuildException(MessageFormat.format(Messages.getString("MarkupToXslfoTask.8"), outputFile, e.getMessage()), // $NON-NLS-1$
            e);
        }
        try {
            XslfoDocumentBuilder builder = new XslfoDocumentBuilder(out);
            XslfoDocumentBuilder.Configuration configuration = this.configuration.clone();
            if (configuration.getTitle() == null) {
                configuration.setTitle(name);
            }
            builder.setConfiguration(configuration);
            builder.setBase(source.getParentFile().toURI());
            MarkupParser parser = new MarkupParser();
            parser.setMarkupLanguage(markupLanguage);
            parser.setBuilder(builder);
            if (generateBookmarks) {
                OutlineItem outline = new OutlineParser(markupLanguage).parse(markupContent);
                builder.setOutline(outline);
            }
            parser.parse(markupContent);
        } finally {
            try {
                out.close();
            } catch (Exception e) {
                throw new BuildException(MessageFormat.format(// $NON-NLS-1$
                Messages.getString("MarkupToXslfoTask.9"), // $NON-NLS-1$
                outputFile, e.getMessage()), e);
            }
        }
    }
    return markupContent;
}
Also used : 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) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) XslfoDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.XslfoDocumentBuilder) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser)

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