Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5.  
  6. public class Main {
  7.  
  8.    private static class Node{
  9.         String text;
  10.         Node(){
  11.             text = "";
  12.         }
  13.     }
  14.  
  15.     private static class Bold extends Node{
  16.         Bold(String text){
  17.             super();
  18.             super.text = "<b>" + text + "</b>";
  19.         }
  20.     }
  21.  
  22.     private static class Italics extends Node{
  23.         Italics(String text){
  24.             super();
  25.             super.text = "<em>" + text + "</em>";
  26.         }
  27.     }
  28.  
  29.     private static class Span extends Node{
  30.         Span(String text){
  31.             super();
  32.             super.text = "<ins>" + text + "</ins>";
  33.         }
  34.     }
  35.  
  36.     private static class Del extends Node{
  37.         Del(String text){
  38.             super();
  39.             super.text = "<del>" + text + "</del>";
  40.         }
  41.     }
  42.  
  43.     private static class Url extends Node{
  44.        String link;
  45.         Url(String text){
  46.             super();
  47.             int r = text.indexOf(']');
  48.             link = text.substring(2, r);
  49.             text = text.substring(r);
  50.  
  51.             super.text = "<a href ="+ link + ">" + text + "</a>";
  52.         }
  53.     }
  54.  
  55.  
  56.     private static String simpleParse(String text){
  57.         switch (text.charAt(0)){
  58.             case 'b':
  59.                 return new Bold(text.substring(2)).text;
  60.             case 'i':
  61.                 return new Italics(text.substring(2)).text;
  62.             case 'u':
  63.                 return new Span(text.substring(2)).text;
  64.             case 's':
  65.                 return new Del(text.substring(2)).text;
  66.         }
  67.         return "";
  68.     }
  69.  
  70.     private static String intermediateParse(String text){
  71.         switch (text.charAt(0)){
  72.             case 'u':
  73.                 return new Url(text.substring(2)).text;
  74.         }
  75.         return "";
  76.     }
  77.  
  78.  
  79.  
  80.     private static String create_new_string(String str, String tmp, int l, int r){
  81.         str = str.substring(0, l - 1) + tmp + str.substring(r + 4);
  82.         return str;
  83.     }
  84.  
  85.     private static String validate(String text, String sym, int l){
  86.        if(sym.equals("u") && text.charAt(l + 1) == 'r')
  87.            return "url";
  88.        return sym;
  89.     }
  90.  
  91.  
  92.  
  93.     private static String strong_parser(String text){
  94.         int r = text.indexOf("[/");
  95.         String sym = validate(text, Character.toString(text.charAt(r + 2)), r + 2);
  96.         int l = r - 1;
  97.         if(r == -1)
  98.             return text;
  99.         while(l != 0) {
  100.             l--;
  101.             if(text.charAt(l) == '[' && text.charAt(l + 1) == sym.charAt(0)){
  102.                 if(sym.length() > 1 && text.charAt(l + 2) == sym.charAt(1))
  103.                     break;
  104.                 if(sym.length() == 1)
  105.                     break;
  106.             }
  107.  
  108.         }
  109.         if(sym.length() == 1)
  110.             text = create_new_string(text, simpleParse(text.substring(l + 1, r)), l + 1, r);
  111.         if(sym.length() == 3)
  112.             text = create_new_string(text, intermediateParse(text.substring(l + 1, r)), l + 1, r + 2);
  113.  
  114.         return strong_parser(text);
  115.     }
  116.  
  117.     static void html(String text) throws IOException {
  118.         File htmlFile = new File("output.html");
  119.         htmlFile.createNewFile();
  120.         text = strong_parser(text);
  121.         BufferedWriter bw = new BufferedWriter(new FileWriter(htmlFile, true));
  122.         bw.append(text).append(" ");
  123.         bw.close();
  124.     }
  125.  
  126.  
  127.     public static void main(String[] args) throws IOException {
  128.         //String text = "[s][u][b][i]Text[/i][/b][/u][/s]";
  129.         String text = "[s][url=https://ru.wikipedia.org]Текст[/url][/s]";
  130.         html(text);
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement