Advertisement
SnackWelle

MiniWiki

Dec 17th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.13 KB | None | 0 0
  1. package ue05_MiniWiki;
  2.  
  3. import javafx.application.Application;
  4. import javafx.geometry.Insets;
  5. import javafx.geometry.Pos;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.Button;
  8. import javafx.scene.control.TextArea;
  9. import javafx.scene.layout.BorderPane;
  10. import javafx.scene.layout.GridPane;
  11. import javafx.scene.web.WebView;
  12. import javafx.stage.Stage;
  13. import java.util.Arrays;
  14. import java.util.regex.Pattern;
  15. import java.util.regex.Matcher;
  16.  
  17. public class MiniWiki extends Application {
  18.  
  19.     final double width = 1280;
  20.     final double height = 700;
  21.     final String header = "<!doctype html>" + "<html lang=\"de\">" + "<body>";
  22.     final String footer = "</body>" + "</html>";
  23.  
  24.  
  25.     public void start(Stage stage) {
  26.         stage.setTitle("MiniWiki");
  27.         BorderPane borderPane = new BorderPane();
  28.         Scene scene = new Scene(borderPane, width, height);
  29.         GridPane gridPane = new GridPane();
  30.         TextArea textArea = new TextArea();
  31.         WebView webView = new WebView();
  32.         Button button = new Button("Compile to HTML");
  33.  
  34.         String html = header + "<p>Tippe Links etwas ein</p>" + footer;
  35.         webView.getEngine().loadContent(html);
  36.  
  37.         textArea.setPrefWidth(scene.getWidth() / 2);
  38.         textArea.setPrefHeight(scene.getHeight() * 2);
  39.         webView.setPrefWidth(scene.getWidth() / 2);
  40.         webView.setPrefHeight(scene.getHeight() * 2);
  41.  
  42.         borderPane.setPadding(new Insets(10, 10, 10, 10));
  43.         button.setPadding(new Insets(10, 10, 10, 10));
  44.         button.setOnAction(event -> webView.getEngine().loadContent(new Implementation(textArea.getText()).res));
  45.  
  46.         gridPane.setHgap(5);
  47.         gridPane.setVgap(5);
  48.         gridPane.add(textArea, 0, 0);
  49.         gridPane.add(webView, 1, 0);
  50.  
  51.         borderPane.setCenter(gridPane);
  52.         borderPane.setBottom(button);
  53.         BorderPane.setAlignment(button, Pos.BOTTOM_CENTER);
  54.  
  55.         stage.setScene(scene);
  56.         stage.setResizable(false);
  57.         stage.show();
  58.     }
  59.  
  60.     public static void main(String[] args) {
  61.         launch(args);
  62.     }
  63.  
  64.     class Implementation {
  65.         String res = "";
  66.         int[] counter = new int[50];
  67.         boolean isEmptyLine = false;
  68.  
  69.         Implementation(String s) {
  70.             for (int i = 0; i < counter.length; i++) counter[i] = 1;
  71.             String lines[] = s.split("\n");
  72.             res += header;
  73.             for (String line : lines) convertText(line);
  74.             res += footer;
  75.         }
  76.  
  77.         private void convertText(String line) {
  78.             Matcher m1 = Pattern.compile("(!+)\\s*(.*)").matcher(line);
  79.             Matcher m2 = Pattern.compile("(\\*+)(.*)").matcher(line);
  80.             Matcher m3 = Pattern.compile("(#+)(.*)").matcher(line);
  81.  
  82.             if (m1.matches()) res += "<h" + m1.group(1).length() + ">" + m1.group(2) + "</h" + m1.group(1).length() + ">";
  83.             else if (m2.matches()) convertUnorderedList(m2);
  84.             else if (m3.matches()) convertOrderedList1(m3);
  85.             else res += line;
  86.             convertBoldOrCursive(line);
  87.             if (Pattern.compile("").matcher(line).matches()) convertParagraph();
  88.             else isEmptyLine = false;
  89.         }
  90.  
  91.         private void convertParagraph() {
  92.             if (!isEmptyLine) {
  93.                 res += "<br><br>";
  94.                 isEmptyLine = true;
  95.             }
  96.         }
  97.  
  98.         private void convertBoldOrCursive(String line) {
  99.             Matcher m1 = Pattern.compile("('')([^']*)('')").matcher(line);
  100.             Matcher m2 = Pattern.compile("(//)([^/]*)(//)").matcher(line);
  101.             Matcher m3 = Pattern.compile("(//)(('')([^/']*)(''))(//)").matcher(line);
  102.  
  103.             while (m3.find()) {
  104.                 while (m2.find()) res = res.replaceAll(m2.group(0), "<i>" + m2.group(2) + "</i>");
  105.                 while (m1.find()) res = res.replaceAll(m1.group(0), "<b>" + m1.group(2) + "</b>");
  106.             }
  107.             while (m1.find()) res = res.replaceAll(m1.group(0), "<b>" + m1.group(2) + "</b>");
  108.             while (m2.find()) res = res.replaceAll(m2.group(0), "<i>" + m2.group(2) + "</i>");
  109.         }
  110.  
  111.         private void convertUnorderedList(Matcher m) {
  112.             String start = "<ul><li>";
  113.             String end = "</li></ul>";
  114.             String lists = "";
  115.  
  116.             if (m.group(1).length() == 1) res += start + m.group(2) + end;
  117.             else {
  118.                 for (int i = 0; i < m.group(1).length() - 1; i++) lists += start + "</li>" + "<br>";
  119.                 lists += start + m.group(2);
  120.                 for (int i = 0; i < m.group(1).length() - 1; i++) lists += end;
  121.                 lists += "</ul>";
  122.                 String helper[] = lists.split("<br>");
  123.                 for (int i = 0; i < m.group(1).length() - 1; i++) res += "<ul>";
  124.                 res += helper[helper.length - 1];
  125.             }
  126.         }
  127.  
  128.         private void convertOrderedList1(Matcher m) {
  129.             String start = "<ol><li>";
  130.             String end = "</li></ol>";
  131.             String lists = "";
  132.  
  133.             if (m.group(1).length() == 1) {
  134.                 start = "<ol start=" + counter[0] + "><li>";
  135.                 res += start + m.group(2) + end;
  136.                 counter[0]++;
  137.                 for (int i = 1; i < counter.length; i++) counter[i] = 1;
  138.             } else {
  139.                 if (m.group(1).length() % 2 == 0) start = "<ol type = a start= " + counter[m.group(1).length() - 1] + "><li>";
  140.                 if (m.group(1).length() % 3 == 0) start = "<ol type = i start= " + counter[m.group(1).length() - 1] + "><li>";
  141.  
  142.                 for (int i = 0; i < m.group(1).length() - 1; i++) lists += start + "</li>" + "<br>";
  143.                 lists += start + m.group(2);
  144.                 for (int i = 0; i < m.group(1).length() - 1; i++) lists += end;
  145.                 lists += "</ol>";
  146.                 String helper[] = lists.split("<br>");
  147.                 for (int i = 0; i < m.group(1).length() - 1; i++) res  += "<ol>";
  148.                 res += helper[helper.length - 1];
  149.                 counter[m.group(1).length() - 1]++;
  150.                 for (int i = m.group(1).length(); i < counter.length; i++) counter[i] = 1;
  151.             }
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement