Index: src/main/java/org/apache/karaf/shell/osgi/Info.java =================================================================== --- src/main/java/org/apache/karaf/shell/osgi/Info.java (revision 1130066) +++ src/main/java/org/apache/karaf/shell/osgi/Info.java (working copy) @@ -17,11 +17,15 @@ package org.apache.karaf.shell.osgi; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.util.List; import org.apache.felix.gogo.commands.Command; +import org.apache.karaf.shell.osgi.wikidoc.AnsiPrintingWikiVisitor; +import org.apache.karaf.shell.osgi.wikidoc.WikiParser; +import org.apache.karaf.shell.osgi.wikidoc.WikiVisitor; import org.apache.karaf.util.StringEscapeUtils; import org.osgi.framework.Bundle; @@ -54,15 +58,21 @@ System.out.println(Util.getUnderlineString(title)); URL bundleInfo = bundle.getEntry("OSGI-INF/bundle.info"); if (bundleInfo != null) { + BufferedReader reader = null; try { - BufferedReader reader = new BufferedReader(new InputStreamReader(bundleInfo.openStream())); - String line; - while ((line = reader.readLine()) != null) { - System.out.println(StringEscapeUtils.unescapeJava(line)); - } - reader.close(); + reader = new BufferedReader(new InputStreamReader(bundleInfo.openStream())); + WikiVisitor visitor = new AnsiPrintingWikiVisitor(System.out); + WikiParser parser = new WikiParser(visitor); + parser.parse(reader); } catch (Exception e) { // ignore + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + } + } } } } Index: src/main/java/org/apache/karaf/shell/osgi/wikidoc/AnsiPrintingWikiVisitor.java =================================================================== --- src/main/java/org/apache/karaf/shell/osgi/wikidoc/AnsiPrintingWikiVisitor.java (revision 0) +++ src/main/java/org/apache/karaf/shell/osgi/wikidoc/AnsiPrintingWikiVisitor.java (revision 0) @@ -0,0 +1,35 @@ +package org.apache.karaf.shell.osgi.wikidoc; + +import java.io.PrintStream; + +import org.fusesource.jansi.Ansi; +import org.fusesource.jansi.Ansi.Attribute; +import org.fusesource.jansi.Ansi.Color; + +/** + * Translates the Wiki tags to Ansi escape sequences to display them on the console + */ +public class AnsiPrintingWikiVisitor implements WikiVisitor { + private PrintStream out; + + public AnsiPrintingWikiVisitor(PrintStream out) { + this.out = out; + } + + @Override + public void heading(int level, String header) { + this.out.print(Ansi.ansi().a(Attribute.INTENSITY_BOLD).a(header) + .a(Attribute.INTENSITY_BOLD_OFF).toString()); + } + + @Override + public void link(String target, String title) { + this.out.print(Ansi.ansi().fg(Color.YELLOW) + .a(target).fg(Color.DEFAULT)); + } + + @Override + public void text(String text) { + this.out.print(text); + } +} Index: src/main/java/org/apache/karaf/shell/osgi/wikidoc/WikiParser.java =================================================================== --- src/main/java/org/apache/karaf/shell/osgi/wikidoc/WikiParser.java (revision 0) +++ src/main/java/org/apache/karaf/shell/osgi/wikidoc/WikiParser.java (revision 0) @@ -0,0 +1,63 @@ +package org.apache.karaf.shell.osgi.wikidoc; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.Reader; +import java.util.StringTokenizer; + +/** + * Parses wiki syntax from a reader and calls a Wikivisitor with the + * tokens it finds + */ +public class WikiParser { + WikiVisitor visitor; + + public WikiParser(WikiVisitor visitor) { + this.visitor = visitor; + } + + public void parse(String line) { + StringTokenizer tokenizer = new StringTokenizer(line , "[h", true); + while (tokenizer.hasMoreTokens()) { + String token = tokenizer.nextToken(); + if ("[".equals(token)) { + parseLink(tokenizer); + } else if ("h".equals(token)) { + parseHeading(tokenizer); + } else { + visitor.text(token); + } + } + } + + private void parseHeading(StringTokenizer tokenizer) { + String level = tokenizer.nextToken("123456789"); + if (!level.matches("[123456789]")) { + visitor.text("h" + level); + return; + } + String dot = tokenizer.nextToken(".\n"); + if (!".".equals(dot)) { + visitor.text("h" + level + dot); + return; + } + String heading = tokenizer.hasMoreTokens() ? tokenizer.nextToken("\n") : ""; + visitor.heading(new Integer(level), heading.trim()); + } + + private void parseLink(StringTokenizer tokenizer) { + String token = tokenizer.nextToken("]"); + visitor.link(token, ""); + tokenizer.nextToken(); + } + + public void parse(Reader reader) throws IOException { + BufferedReader br = new BufferedReader(reader); + String line; + while ((line = br.readLine()) != null) { + parse(line); + visitor.text("\n"); + } + } + +} Index: src/main/java/org/apache/karaf/shell/osgi/wikidoc/WikiVisitor.java =================================================================== --- src/main/java/org/apache/karaf/shell/osgi/wikidoc/WikiVisitor.java (revision 0) +++ src/main/java/org/apache/karaf/shell/osgi/wikidoc/WikiVisitor.java (revision 0) @@ -0,0 +1,10 @@ +package org.apache.karaf.shell.osgi.wikidoc; + +/** + * Will be used by WikiParser to call the respective handler when it recognizes the tag + */ +public interface WikiVisitor { + void link(String target, String title); + void heading(int level, String title); + void text(String text); +} \ No newline at end of file