Search in sources :

Example 1 with WordWrap

use of org.eclipse.ceylon.common.tool.WordWrap in project ceylon by eclipse.

the class CeylonHelpTool method run.

@Override
public void run() {
    if (wantsPager && OSUtil.isWindows() && tool != null) {
        if (openHelpInBrowser()) {
            return;
        }
    }
    docBuilder.setIncludeHidden(includeHidden);
    Doc doc;
    if (tool != null) {
        doc = docBuilder.buildDoc(tool);
    } else {
        final ToolModel<CeylonTool> root = toolLoader.loadToolModel("");
        doc = docBuilder.buildDoc(root, true);
    }
    Process pagerProcess = null;
    if (wantsPager && !OSUtil.isWindows()) {
        pagerProcess = startPager();
    }
    OutputStream pagerPipe = null;
    if (pagerProcess != null)
        pagerPipe = pagerProcess.getOutputStream();
    try {
        final WordWrap wrap = getWrap(pagerPipe);
        Visitor plain = new PlainVisitor(wrap);
        if (synopsis) {
            plain = new SynopsisOnlyVisitor(plain);
        } else if (options != null) {
            plain = new OptionsOnlyVisitor(plain, new HashSet<String>(Arrays.asList(options.trim().split("\\s*,\\s*"))));
        }
        doc.accept(plain);
        wrap.flush();
    } finally {
        // shutdown pager
        if (pagerPipe != null) {
            try {
                pagerPipe.close();
            } catch (IOException e) {
            // Seems common on MacOS so let's just ignore this
            }
            // wait for pager to be done, there's no point doing anything else meanwhile
            try {
                int errorCode = pagerProcess.waitFor();
                if (errorCode != 0) {
                    throw new ToolUsageError("Pager process returned an error exit code: " + errorCode + ". Try fixing your $CEYLON_PAGER or $PAGER environment variable or invoke with the --no-pager command-line option.");
                }
            } catch (InterruptedException e) {
                throw new ToolUsageError("Pager process interrupted. Try fixing your $CEYLON_PAGER or $PAGER environment variable or invoke with the --no-pager command-line option.");
            }
        }
    }
}
Also used : Visitor(org.eclipse.ceylon.common.tools.help.model.Visitor) OutputStream(java.io.OutputStream) CeylonTool(org.eclipse.ceylon.common.tools.CeylonTool) IOException(java.io.IOException) WordWrap(org.eclipse.ceylon.common.tool.WordWrap) ToolUsageError(org.eclipse.ceylon.common.tool.ToolUsageError) Doc(org.eclipse.ceylon.common.tools.help.model.Doc)

Example 2 with WordWrap

use of org.eclipse.ceylon.common.tool.WordWrap in project ceylon by eclipse.

the class PlaintextTests method renderAndCompare.

public void renderAndCompare(String mdFile, String txtFile) throws Exception {
    StringWriter sw = new StringWriter();
    try (InputStreamReader stream = new InputStreamReader(PlaintextTests.class.getResourceAsStream(mdFile))) {
        Parser parser = new Parser(stream);
        Document document = parser.parse();
        PlaintextMarkdownVisitor emitter = new PlaintextMarkdownVisitor(new WordWrap(sw));
        document.accept(emitter);
    }
    StringBuilder sb = new StringBuilder();
    try (BufferedReader r = new BufferedReader(new InputStreamReader(PlaintextTests.class.getResourceAsStream(txtFile)))) {
        String line = r.readLine();
        while (line != null) {
            sb.append(line).append("\n");
            line = r.readLine();
        }
    }
    Assert.assertEquals(sb.toString(), sw.toString().replace("\r\n", "\n"));
}
Also used : StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) WordWrap(org.eclipse.ceylon.common.tool.WordWrap) BufferedReader(java.io.BufferedReader) PlaintextMarkdownVisitor(org.eclipse.ceylon.common.tools.help.PlaintextMarkdownVisitor) Document(org.tautua.markdownpapers.ast.Document) Parser(org.tautua.markdownpapers.parser.Parser)

Aggregations

WordWrap (org.eclipse.ceylon.common.tool.WordWrap)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 StringWriter (java.io.StringWriter)1 ToolUsageError (org.eclipse.ceylon.common.tool.ToolUsageError)1 CeylonTool (org.eclipse.ceylon.common.tools.CeylonTool)1 PlaintextMarkdownVisitor (org.eclipse.ceylon.common.tools.help.PlaintextMarkdownVisitor)1 Doc (org.eclipse.ceylon.common.tools.help.model.Doc)1 Visitor (org.eclipse.ceylon.common.tools.help.model.Visitor)1 Document (org.tautua.markdownpapers.ast.Document)1 Parser (org.tautua.markdownpapers.parser.Parser)1