Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aeb0f98361c55a9107032312ebe16d091621902c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************************************
 * Copyright (c) 2009 Red Hat Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Alexander Kurtakov - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.man.parser;

import org.eclipse.linuxtools.internal.man.parser.ManParser;

/**
 * Man page bean to ease fetching html-preformatted different parts of a man
 * page.
 */
public class ManPage {

    private StringBuilder rawContent;
    private StringBuilder strippedTextPage;

    /**
     * Creates the man page which includes retrieving the raw content and
     * changing format symbols to html.
     *
     * @param manPage
     *            The man page.
     */
    public ManPage(String manPage) {
        parse(new ManParser().getRawManPage(manPage));
    }

    // TODO make bold and underline be ranges instead of separate symbols.
    private void parse(StringBuilder rawManPage) {
        StringBuilder sb = new StringBuilder();
        sb.append(rawManPage);

        while (sb.indexOf("_\b") != -1) { //$NON-NLS-1$
            int index = sb.indexOf("_\b"); //$NON-NLS-1$
            sb.replace(index, index + 3,
                    "<u>" + sb.substring(index + 2, index + 3) + "</u>"); //$NON-NLS-1$ //$NON-NLS-2$
        }
        while (sb.indexOf("\b") != -1) { //$NON-NLS-1$
            int index = sb.indexOf("\b"); //$NON-NLS-1$
            sb.replace(index - 1, index + 2,
                    "<b>" + sb.substring(index - 1, index) + "</b>"); //$NON-NLS-1$ //$NON-NLS-2$
        }
        rawContent = sb;
    }

    /**
     * Returns html representation of the man page. The whole man page is kept
     * in one giant &lt;pre&gt; block with bold and underline symbols.
     *
     * @return The whole html man page.
     */
    public StringBuilder getHtmlPage() {
        StringBuilder sb = new StringBuilder();
        sb.append("<pre>").append(rawContent).append("</pre>"); //$NON-NLS-1$ //$NON-NLS-2$
        return sb;
    }

    /**
     * Returns stripped representation of the man page. Stripped parts are:
     * <ul>
     * <li>Header - all the parts before <b>NAME</b></li>
     * <li>Footer - all the parts from <b>AUTHOR</b> till the end</li>
     * </ul>
     *
     * @return The stripped html content of the man page.
     */
    public StringBuilder getStrippedHtmlPage() {
        StringBuilder sb = getStrippedPage();
        sb.insert(0, "<pre>"); //$NON-NLS-1$
        sb.append("</pre>"); //$NON-NLS-1$
        return sb;
    }

    /**
     * Returns stripped representation of the man page in the format it was
     * received from executing man. Stripped parts are:
     * <ul>
     * <li>Header - all the parts before <b>NAME</b></li>
     * <li>Footer - all the parts from <b>AUTHOR</b> till the end</li>
     * </ul>
     *
     * @return The stripped plain text content of the man page.
     * @since 1.1
     */
    public StringBuilder getStrippedPage() {
        StringBuilder sb = new StringBuilder();
        sb.append(rawContent);
        // The raw content may or may not be HTML
        if (sb.indexOf("<b>N</b>") != -1) { //$NON-NLS-1$
            sb.delete(0, sb.indexOf("<b>N</b>")); //$NON-NLS-1$
        } else if (sb.indexOf("NAME") != -1) { //$NON-NLS-1$
            sb.delete(0, sb.indexOf("NAME")); //$NON-NLS-1$
        }

        if (sb.indexOf("<b>A</b><b>U</b><b>T</b><b>H</b><b>O</b><b>R</b>") != -1) { //$NON-NLS-1$
            sb.delete(
                    sb.indexOf("<b>A</b><b>U</b><b>T</b><b>H</b><b>O</b><b>R</b>"), //$NON-NLS-1$
                    sb.length());
        } else if (sb.indexOf("AUTHOR") != -1) { //$NON-NLS-1$
            sb.delete(sb.indexOf("AUTHOR"), //$NON-NLS-1$
                    sb.length());
        }

        return sb;
    }

    /**
     * Removes all HTML markings are returns a text only version.
     *
     * @return a text only version of the manpage
     * @since 1.1
     */
    public StringBuilder getStrippedTextPage() {
        if (this.strippedTextPage == null) {
            this.strippedTextPage = getStrippedPage();
            int index = strippedTextPage.indexOf("<b>"); //$NON-NLS-1$
            while (index != -1) {
                strippedTextPage.replace(index, index + 3, ""); //$NON-NLS-1$
                strippedTextPage.replace(index + 1, index + 5, ""); //$NON-NLS-1$
                index = strippedTextPage.indexOf("<b>"); //$NON-NLS-1$
            }

            index = strippedTextPage.indexOf("<u>"); //$NON-NLS-1$
            while (index != -1) {
                strippedTextPage.replace(index, index + 3, ""); //$NON-NLS-1$
                strippedTextPage.replace(index + 1, index + 5, ""); //$NON-NLS-1$
                index = strippedTextPage.indexOf("<u>"); //$NON-NLS-1$
            }
        }

        return strippedTextPage;
    }
}

Back to the top