Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ue05_MiniWiki;
- import javafx.application.Application;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextArea;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.GridPane;
- import javafx.scene.web.WebView;
- import javafx.stage.Stage;
- import java.util.Arrays;
- import java.util.regex.Pattern;
- import java.util.regex.Matcher;
- public class MiniWiki extends Application {
- final double width = 1280;
- final double height = 700;
- final String header = "<!doctype html>" + "<html lang=\"de\">" + "<body>";
- final String footer = "</body>" + "</html>";
- public void start(Stage stage) {
- stage.setTitle("MiniWiki");
- BorderPane borderPane = new BorderPane();
- Scene scene = new Scene(borderPane, width, height);
- GridPane gridPane = new GridPane();
- TextArea textArea = new TextArea();
- WebView webView = new WebView();
- Button button = new Button("Compile to HTML");
- String html = header + "<p>Tippe Links etwas ein</p>" + footer;
- webView.getEngine().loadContent(html);
- textArea.setPrefWidth(scene.getWidth() / 2);
- textArea.setPrefHeight(scene.getHeight() * 2);
- webView.setPrefWidth(scene.getWidth() / 2);
- webView.setPrefHeight(scene.getHeight() * 2);
- borderPane.setPadding(new Insets(10, 10, 10, 10));
- button.setPadding(new Insets(10, 10, 10, 10));
- button.setOnAction(event -> webView.getEngine().loadContent(new Implementation(textArea.getText()).res));
- gridPane.setHgap(5);
- gridPane.setVgap(5);
- gridPane.add(textArea, 0, 0);
- gridPane.add(webView, 1, 0);
- borderPane.setCenter(gridPane);
- borderPane.setBottom(button);
- BorderPane.setAlignment(button, Pos.BOTTOM_CENTER);
- stage.setScene(scene);
- stage.setResizable(false);
- stage.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- class Implementation {
- String res = "";
- int[] counter = new int[50];
- boolean isEmptyLine = false;
- Implementation(String s) {
- for (int i = 0; i < counter.length; i++) counter[i] = 1;
- String lines[] = s.split("\n");
- res += header;
- for (String line : lines) convertText(line);
- res += footer;
- }
- private void convertText(String line) {
- Matcher m1 = Pattern.compile("(!+)\\s*(.*)").matcher(line);
- Matcher m2 = Pattern.compile("(\\*+)(.*)").matcher(line);
- Matcher m3 = Pattern.compile("(#+)(.*)").matcher(line);
- if (m1.matches()) res += "<h" + m1.group(1).length() + ">" + m1.group(2) + "</h" + m1.group(1).length() + ">";
- else if (m2.matches()) convertUnorderedList(m2);
- else if (m3.matches()) convertOrderedList1(m3);
- else res += line;
- convertBoldOrCursive(line);
- if (Pattern.compile("").matcher(line).matches()) convertParagraph();
- else isEmptyLine = false;
- }
- private void convertParagraph() {
- if (!isEmptyLine) {
- res += "<br><br>";
- isEmptyLine = true;
- }
- }
- private void convertBoldOrCursive(String line) {
- Matcher m1 = Pattern.compile("('')([^']*)('')").matcher(line);
- Matcher m2 = Pattern.compile("(//)([^/]*)(//)").matcher(line);
- Matcher m3 = Pattern.compile("(//)(('')([^/']*)(''))(//)").matcher(line);
- while (m3.find()) {
- while (m2.find()) res = res.replaceAll(m2.group(0), "<i>" + m2.group(2) + "</i>");
- while (m1.find()) res = res.replaceAll(m1.group(0), "<b>" + m1.group(2) + "</b>");
- }
- while (m1.find()) res = res.replaceAll(m1.group(0), "<b>" + m1.group(2) + "</b>");
- while (m2.find()) res = res.replaceAll(m2.group(0), "<i>" + m2.group(2) + "</i>");
- }
- private void convertUnorderedList(Matcher m) {
- String start = "<ul><li>";
- String end = "</li></ul>";
- String lists = "";
- if (m.group(1).length() == 1) res += start + m.group(2) + end;
- else {
- for (int i = 0; i < m.group(1).length() - 1; i++) lists += start + "</li>" + "<br>";
- lists += start + m.group(2);
- for (int i = 0; i < m.group(1).length() - 1; i++) lists += end;
- lists += "</ul>";
- String helper[] = lists.split("<br>");
- for (int i = 0; i < m.group(1).length() - 1; i++) res += "<ul>";
- res += helper[helper.length - 1];
- }
- }
- private void convertOrderedList1(Matcher m) {
- String start = "<ol><li>";
- String end = "</li></ol>";
- String lists = "";
- if (m.group(1).length() == 1) {
- start = "<ol start=" + counter[0] + "><li>";
- res += start + m.group(2) + end;
- counter[0]++;
- for (int i = 1; i < counter.length; i++) counter[i] = 1;
- } else {
- if (m.group(1).length() % 2 == 0) start = "<ol type = a start= " + counter[m.group(1).length() - 1] + "><li>";
- if (m.group(1).length() % 3 == 0) start = "<ol type = i start= " + counter[m.group(1).length() - 1] + "><li>";
- for (int i = 0; i < m.group(1).length() - 1; i++) lists += start + "</li>" + "<br>";
- lists += start + m.group(2);
- for (int i = 0; i < m.group(1).length() - 1; i++) lists += end;
- lists += "</ol>";
- String helper[] = lists.split("<br>");
- for (int i = 0; i < m.group(1).length() - 1; i++) res += "<ol>";
- res += helper[helper.length - 1];
- counter[m.group(1).length() - 1]++;
- for (int i = m.group(1).length(); i < counter.length; i++) counter[i] = 1;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement