📄 tohtml.java
字号:
* @param out html document gets written here. * @param mimeType the mime type of the document to be used for syntax highlighting purposes. * @throws IOException if an I/O error occurs. */ public static void htmlifyPlain(Reader in, PrintWriter out) throws IOException { try { ToHTML toHTML = new ToHTML(); toHTML.setLexer(new PlainLexer(in)); toHTML.setOutput(out); toHTML.writeHTMLFragment(); } catch (InvocationTargetException x){ // can't happen } } /** * Write the html encoded version of a document * using syntax highlighting for the given mime type. * <p> * Recognized mime-types include: * <ul> * <li>text/html * <li>text/x-java * <li>text/x-csrc * <li>text/x-chdr * <li>text/x-csrc * <li>text/x-c++hdr * </ul> * If a mime type is not recognized, the html is written * without syntax highlighting. * <p> * The document produced is not a full html document but rather * an html fragment that may be inserted into a full document. * <p> * This is a convenience method that creates an new ToHTML object. * * @param in Data stream that needs to be formatted in html * @param out html document gets written here * @param mimeType the mime type of the document to be used for syntax highlighting purposes. * @throws IOException if an I/O error occurs. */ public static void htmlify(Reader in, PrintWriter out, String mimeType) throws IOException { try { ToHTML toHTML = new ToHTML(); toHTML.setInput(in); toHTML.setOutput(out); toHTML.setMimeType(mimeType); toHTML.writeFullHTML(); } catch (InvocationTargetException x){ // can't happen } catch (CompileException x){ // can't happen } } private void saveLexer(Lexer lexer){ if (!lexers.containsKey(lexerType)){ lexers.put(lexer.getClass().getName(), lexer); } } /** * Return a lexer that is an instance of the given * class name. * If no appropriate lexer is found, return null. * * @return lexer for the class name. * @throws InvocationTargetException if the class could not be instantiated. */ private Lexer getLexerFromClass(String lexerType) throws IOException, InvocationTargetException { Lexer lexer = null; if (lexerType != null){ if (lexers.containsKey(lexerType)){ lexer = (Lexer)lexers.get(lexerType); lexer.reset(in, 0, 0, 0); } else { try { lexer = (Lexer)( Class.forName(lexerType) .getDeclaredConstructor(new Class[] {Class.forName("java.io.Reader")}) .newInstance(new Object[] {in}) ); saveLexer(lexer); } catch (ClassNotFoundException x){ throw new InvocationTargetException(x); } catch (NoSuchMethodException x){ throw new InvocationTargetException(x); } catch (InstantiationException x){ throw new InvocationTargetException(x); } catch (IllegalAccessException x){ throw new InvocationTargetException(x); } } } return lexer; } /** * Return an appropriate lexer for the current mime type. * If no appropriate lexer is found, return null. * * @return lexer for the current mime type. */ private Lexer getLexerFromMime() throws IOException, InvocationTargetException { String lexerType = null; if (mimeType != null){ if (registeredMimeTypes.containsKey(mimeType)){ lexerType = (String)registeredMimeTypes.get(mimeType); } else { int slashIndex = mimeType.indexOf("/"); if (slashIndex > 0){ String mainMime = mimeType.substring(0, slashIndex); if (registeredMimeTypes.containsKey(mainMime)){ lexerType = (String)registeredMimeTypes.get(mainMime); } } } } return getLexerFromClass(lexerType); } /** * Return an appropriate lexer for the current file extension. * If no appropriate lexer is found, return null. * * @return lexer for the current file extension. */ private Lexer getLexerFromExt() throws IOException, InvocationTargetException { String lexerType = null; if (fileExt != null){ if (registeredFileExtensions.containsKey(fileExt.toLowerCase())){ lexerType = (String)registeredFileExtensions.get(fileExt.toLowerCase()); } } if (lexerType == null) lexerType = "com.Ostermiller.Syntax.Lexer.PlainLexer"; return getLexerFromClass(lexerType); } /** * Write the html version of the output from the given lexer as a * stand alone html document with the given title. The html document * will use the style sheet syntax.css. * <p> * This is a convenience method that creates an new ToHTML object. * * @param lexer contains the tokens that need to be html escaped. * @param out html gets written here. * @param title the title to be put on the html document. * @throws IOException if an I/O error occurs. */ public static void htmlify(Reader in, PrintWriter out, String title, String mimeType) throws IOException { try { ToHTML toHTML = new ToHTML(); toHTML.setInput(in); toHTML.setOutput(out); toHTML.setTitle(title); toHTML.setMimeType(mimeType); toHTML.writeFullHTML(); } catch (InvocationTargetException x){ // can't happen } catch (CompileException x){ // can't happen } } /** * Write the html version of a document * using syntax highlighting for the given mime type. * <P> * Conversions between characters and bytes will be done * using the default character set for the system. * <p> * This is a convenience method that creates an new ToHTML object. * * @param in Data stream that needs to be formatted in html. * @param out html document gets written here. * @param mimeType the mime type of the document to be used for syntax highlighting purposes. * @throws IOException if an I/O error occurs. */ public static void htmlify(InputStream in, PrintStream out, String mimeType) throws IOException { try { ToHTML toHTML = new ToHTML(); toHTML.setInput(new InputStreamReader(in)); toHTML.setOutput(new PrintWriter(out)); toHTML.setMimeType(mimeType); toHTML.writeFullHTML(); } catch (InvocationTargetException x){ // can't happen } catch (CompileException x){ // can't happen } } /** * Write the html version of the output from the given lexer. The * document is written without an html head or footer so that it * can be used as part of a larger html document. * <p> * This is a convenience method that creates an new ToHTML object. * * @param lexer contains the tokens that need to be html escaped. * @param out html gets written here. * @throws IOException if an I/O error occurs. */ public static void htmlify(Lexer lexer, PrintWriter out) throws IOException { try { ToHTML toHTML = new ToHTML(); toHTML.setLexer(lexer); toHTML.setOutput(new PrintWriter(out)); toHTML.writeHTMLFragment(); } catch (InvocationTargetException x){ // can't happen } } /** * Write the html version of the output from the given lexer. The * document is written without an html head or footer so that it * can be used as part of a larger html document. * <P> * Conversions between characters and bytes will be done * using the default character set for the system. * <p> * This is a convenience method that creates an new ToHTML object. * * @param lexer contains the tokens that need to be html escaped. * @param out html gets written here. * @throws IOException if an I/O error occurs. */ public static void htmlify(Lexer lexer, PrintStream out) throws IOException { htmlify(lexer, new PrintWriter(out)); } /** * Write the html version of the output from the given lexer as a * stand alone html document with the given title. The html document * will use the style sheet syntax.css. * <p> * This is a convenience method that creates an new ToHTML object. * * @param lexer contains the tokens that need to be html escaped. * @param out html gets written here. * @param title the title to be put on the html document. * @throws IOException if an I/O error occurs. */ public static void htmlify(Lexer lexer, PrintWriter out, String title) throws IOException { try { ToHTML toHTML = new ToHTML(); toHTML.setLexer(lexer); toHTML.setOutput(out); toHTML.setTitle(title); toHTML.writeFullHTML(); } catch (InvocationTargetException x){ // can't happen } catch (CompileException x){ // can't happen } } /** * Write the html version of the output from the given lexer as a * stand alone html document with the given title. The html document * will use the style sheet syntax.css. * <P> * Conversions between characters and bytes will be done * using the default character set for the system. * <p> * This is a convenience method that creates an new ToHTML object. * * @param lexer contains the tokens that need to be html escaped. * @param out html gets written here. * @param title the title to be put on the html document. * @throws IOException if an I/O error occurs. */ public static void htmlify(Lexer lexer, PrintStream out, String title) throws IOException { htmlify(lexer, new PrintWriter(out), title); } /** * Write the string after escaping characters that would hinder * it from rendering in html. * * @param text The string to be escaped and written * @param out output gets written here */ public static void writeEscapedHTML(String text, PrintWriter out){ boolean lastSpace = false; for (int i=0; i < text.length(); i++){ char ch = text.charAt(i); switch(ch){ case '<': { out.print("<"); lastSpace = false; break; } case '>': { out.print(">"); lastSpace = false; break; } case '&': { out.print("&"); lastSpace = false; break; } case '"': { out.print("""); lastSpace = false; break; } default: { out.print(ch); lastSpace = false; break; } } } } /** * Write the string after escaping characters that would hinder * it from rendering in html. * <P> * Conversions between characters and bytes will be done * using the default character set for the system. * * @param text The string to be escaped and written * @param out output gets written here */ public static void writeEscapedHTML(String text, PrintStream out){ writeEscapedHTML(text, new PrintWriter(out)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -