I love to make things automate. Recently I have created www.algorithmsdemos.com. For this website, I have to style the code in C, Java, JavaScript. After Styling 30 programs, I felt to make this automate. I am using http://markup.su/highlighter/ to convert programs into beautiful markup. I have clearly observed how this website working. It is using one service at URL markup.su/api/highlighter. By using this service I have created one eclipse plugin convert program into beautiful markup.
Setup
- Download above project and import to your workspace
- Run the project as eclipse plugin project. Now you can see new eclipse window with new workspace
- Create a sample dynamic web project and create a Java or HTML document. Write C or Java or JavaScript code in that document
- Open Project Explorer or Package Explorer and right click on that document and go to Sysntax Hilighter sub menu and then click on C or Java or JavaScript or HTML
Identify File From Package Explorer or Project Explorer
This code is to know the selected file by user in Package Explorer or Project Explorer.IWorkbench workbench = PlatformUI.getWorkbench(); ISelectionService is = workbench.getActiveWorkbenchWindow().getSelectionService(); IStructuredSelection structured = (IStructuredSelection) is .getSelection("org.eclipse.jdt.ui.PackageExplorer"); IFile file = null; if (structured == null) { structured = (IStructuredSelection) is .getSelection("org.eclipse.ui.navigator.ProjectExplorer"); file = (IFile) structured.getFirstElement(); } IPath path = file.getLocation(); // Do something with path.
Sending Post Request to Markup Service
public static String hilight(String code, String lang) throws IOException { String url = "http://markup.su/api/highlighter"; String USER_AGENT = "Mozilla/5.0"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); //add reuqest header con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); //add parameters StringBuilder sb = new StringBuilder(); sb.append(URLEncoder.encode("source", "UTF-8")).append('=').append(URLEncoder.encode(code, "UTF-8")); sb.append("&"+URLEncoder.encode("language", "UTF-8")).append('=').append(URLEncoder.encode(lang, "UTF-8")); sb.append("&"+URLEncoder.encode("theme", "UTF-8")).append('=').append(URLEncoder.encode("Dawn", "UTF-8")); String urlParameters = sb.toString(); // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + urlParameters); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder resp = new StringBuilder(); while ((inputLine = in.readLine()) != null) { resp.append(inputLine); } in.close(); System.out.println(resp.toString()); //return syntax hilighted response return resp.toString(); }
0 comments:
Post a Comment