Tips/GooglePrettifyRhino のバックアップ(No.9)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Tips/GooglePrettifyRhino へ行く。
- 1 (2011-01-25 (火) 23:52:05)
- 2 (2012-03-09 (金) 16:15:15)
- 3 (2012-03-09 (金) 17:15:19)
- 4 (2012-05-28 (月) 17:41:58)
- 5 (2012-06-24 (日) 04:30:37)
- 6 (2012-06-25 (月) 20:22:34)
- 7 (2012-06-30 (土) 04:44:14)
- 8 (2012-10-02 (火) 21:18:52)
- 9 (2012-10-03 (水) 15:10:49)
- 10 (2012-10-03 (水) 18:08:57)
- 11 (2012-10-03 (水) 19:19:15)
- 12 (2012-10-03 (水) 20:54:28)
- 13 (2012-10-04 (木) 15:26:38)
- 14 (2012-10-04 (木) 22:05:16)
- 15 (2012-10-06 (土) 23:15:41)
- 16 (2012-12-04 (火) 12:12:12)
- 17 (2013-03-29 (金) 03:23:06)
- 18 (2013-07-21 (日) 00:19:15)
- 19 (2013-08-01 (木) 19:20:09)
- 20 (2014-09-03 (水) 18:13:53)
- 21 (2014-09-14 (日) 01:52:25)
- 22 (2014-10-02 (木) 15:20:03)
- 23 (2014-12-20 (土) 13:27:48)
- 24 (2015-12-18 (金) 16:20:25)
- 25 (2017-03-30 (木) 14:21:32)
- 26 (2018-01-22 (月) 18:42:56)
- 27 (2018-02-27 (火) 15:59:24)
- 28 (2018-07-30 (月) 04:06:43)
- 29 (2018-08-30 (木) 18:03:51)
- 30 (2018-12-10 (月) 16:47:38)
- 31 (2019-05-22 (水) 19:35:38)
TITLE:Rhinoでgoogle-prettify.jsを実行する
Posted by aterai at 2011-01-25
Rhinoでgoogle-prettify.jsを実行する
#adsense2
Rhinoでgoogle-prettify.jsを実行し、ソースコードをハイライトされたHtml(google sites用)に変換します。
サンプルコード
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.script.*;
import javax.swing.*;
import javax.swing.text.html.*;
public class GooglePrettifyRhinoTest {
private final JTextArea src = new JTextArea();
private final JTextArea dst = new JTextArea();
private final JEditorPane editor = new JEditorPane();
private final ScriptEngine engine = createEngine();
public JComponent makeUI() {
try (Reader reader = new BufferedReader(new InputStreamReader(
new FileInputStream("GooglePrettifyRhinoTest.java"), "UTF-8"))) {
src.read(reader, "");
} catch(Exception ex) {
ex.printStackTrace();
}
StyleSheet styleSheet = new StyleSheet();
styleSheet.addRule(".str {color:#008800}");
styleSheet.addRule(".kwd {color:#000088}");
styleSheet.addRule(".com {color:#880000}");
styleSheet.addRule(".typ {color:#660066}");
styleSheet.addRule(".lit {color:#006666}");
styleSheet.addRule(".pun {color:#666600}");
styleSheet.addRule(".pln {color:#000000}");
styleSheet.addRule(".tag {color:#000088}");
styleSheet.addRule(".atn {color:#660066}");
styleSheet.addRule(".atv {color:#008800}");
styleSheet.addRule(".dec {color:#660066}");
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
htmlEditorKit.setStyleSheet(styleSheet);
editor.setEditorKit(htmlEditorKit);
JButton b = new JButton((new AbstractAction("Convert to google sites") {
String pre = "<pre>";
@Override public void actionPerformed(ActionEvent e) {
String txt = src.getText();
txt = txt.replace("&", "&").replace("<", "<").replace(">", ">");
String str = prettify(engine, txt);
editor.setText(pre+str+"\n</pre>");
str = str.replace("class=\"str\"", "style=\"color:#080\"");
str = str.replace("class=\"kwd\"", "style=\"color:#008\"");
str = str.replace("class=\"com\"", "style=\"color:#800\"");
str = str.replace("class=\"typ\"", "style=\"color:#606\"");
str = str.replace("class=\"lit\"", "style=\"color:#066\"");
str = str.replace("class=\"pun\"", "style=\"color:#660\"");
str = str.replace("class=\"pln\"", "style=\"color:#000\"");
str = str.replace("class=\"tag\"", "style=\"color:#008\"");
str = str.replace("class=\"atn\"", "style=\"color:#606\"");
str = str.replace("class=\"atv\"", "style=\"color:#080\"");
str = str.replace("class=\"dec\"", "style=\"color:#606\"");
dst.setText(pre+str+"\n</pre>");
}
}));
JTabbedPane tab = new JTabbedPane();
tab.addTab("Google sites html", new JScrollPane(dst));
tab.addTab("JEditorPane preview", new JScrollPane(editor));
JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
sp.setResizeWeight(.5);
sp.setTopComponent(new JScrollPane(src));
sp.setBottomComponent(tab);
JPanel p = new JPanel(new BorderLayout());
p.add(b, BorderLayout.SOUTH);
p.add(sp);
return p;
}
public static ScriptEngine createEngine() {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// ScriptEngineFactory factory = engine.getFactory();
// String name = factory.getEngineName();
// String version = factory.getEngineVersion();
// System.out.printf("\tScript Engine: %s (%s)\n", name, version);
//String p = "http://google-code-prettify.googlecode.com/svn/trunk/src/prettify.js";
String p = "http://google-code-prettify.googlecode.com/svn-history/r120/trunk/src/prettify.js";
//String p = "http://terai.xrea.jp/skin/irid/prettify.js";
try (Reader reader = new BufferedReader(
new InputStreamReader(new URL(p).openStream()))) {
engine.eval("var window={};var navigator=null;");
engine.eval(reader);
return engine;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static String prettify(ScriptEngine engine, String src) {
try {
Object w = engine.get("window");
return (String)((Invocable)engine).invokeMethod(w, "prettyPrintOne", src);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new GooglePrettifyRhinoTest().makeUI());
f.setSize(640, 640);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
解説
上記のサンプルでは、new ScriptEngineManager().getEngineByName("JavaScript"); で取得したJavaScriptエンジン(Rhino)に、prettify.jsを読み込んで、prettify.jsのprettyPrintOneメソッドを実行しています。 google sitesでは、cssファイルも使用できないので、replace("class=\"kwd\"", "style=\"color:#008\"");のように、クラスをスタイルの色に全部置換しています。
- org.mozilla.javascript.Context などを使用する場合
> "%JAVA_HOME%\bin\javac" -cp .;rhino-1.7R4.jar GooglePrettifyRhinoTest.java > "%JAVA_HOME%\bin\java" -cp .;rhino-1.7R4.jar GooglePrettifyRhinoTest
String txt = src.getText();
txt = txt.replace("&", "&").replace("<", "<").replace(">", ">");
String str = "";
String p = "http://google-code-prettify.googlecode.com/svn-history/r120/trunk/src/prettify.js";
try (Reader reader1 = new StringReader("var window={}, navigator=null;");
Reader reader2 = new BufferedReader(new InputStreamReader(new URL(p).openStream()))) {
Context cx = Context.enter();
ScriptableObject scope = cx.initStandardObjects();
Object result = cx.evaluateReader(scope, reader1, "", 1, null);
result = cx.evaluateReader(scope, reader2, "prettify.js", 1, null);
ScriptableObject w = (ScriptableObject)scope.get("window", scope);
Function fct = (Function)w.get("prettyPrintOne", w);
result = fct.call(cx, w, w, new Object[] {txt, true});
str = (String)Context.jsToJava(result, String.class);
} catch (Exception ex) {
ex.printStackTrace();
} finally{
Context.exit();
}
editor.setText(pre+str+"\n</pre>");
//...
参考リンク
- google-code-prettify - Project Hosting on Google Code
- Rhino - JavaScript for Java
- JEditorPaneのHTMLEditorKitにCSSを適用