Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- public class Main {
- private static class Node{
- String text;
- Node(){
- text = "";
- }
- }
- private static class Bold extends Node{
- Bold(String text){
- super();
- super.text = "<b>" + text + "</b>";
- }
- }
- private static class Italics extends Node{
- Italics(String text){
- super();
- super.text = "<em>" + text + "</em>";
- }
- }
- private static class Span extends Node{
- Span(String text){
- super();
- super.text = "<ins>" + text + "</ins>";
- }
- }
- private static class Del extends Node{
- Del(String text){
- super();
- super.text = "<del>" + text + "</del>";
- }
- }
- private static class Url extends Node{
- String link;
- Url(String text){
- super();
- int r = text.indexOf(']');
- link = text.substring(2, r);
- text = text.substring(r);
- super.text = "<a href ="+ link + ">" + text + "</a>";
- }
- }
- private static String simpleParse(String text){
- switch (text.charAt(0)){
- case 'b':
- return new Bold(text.substring(2)).text;
- case 'i':
- return new Italics(text.substring(2)).text;
- case 'u':
- return new Span(text.substring(2)).text;
- case 's':
- return new Del(text.substring(2)).text;
- }
- return "";
- }
- private static String intermediateParse(String text){
- switch (text.charAt(0)){
- case 'u':
- return new Url(text.substring(2)).text;
- }
- return "";
- }
- private static String create_new_string(String str, String tmp, int l, int r){
- str = str.substring(0, l - 1) + tmp + str.substring(r + 4);
- return str;
- }
- private static String validate(String text, String sym, int l){
- if(sym.equals("u") && text.charAt(l + 1) == 'r')
- return "url";
- return sym;
- }
- private static String strong_parser(String text){
- int r = text.indexOf("[/");
- String sym = validate(text, Character.toString(text.charAt(r + 2)), r + 2);
- int l = r - 1;
- if(r == -1)
- return text;
- while(l != 0) {
- l--;
- if(text.charAt(l) == '[' && text.charAt(l + 1) == sym.charAt(0)){
- if(sym.length() > 1 && text.charAt(l + 2) == sym.charAt(1))
- break;
- if(sym.length() == 1)
- break;
- }
- }
- if(sym.length() == 1)
- text = create_new_string(text, simpleParse(text.substring(l + 1, r)), l + 1, r);
- if(sym.length() == 3)
- text = create_new_string(text, intermediateParse(text.substring(l + 1, r)), l + 1, r + 2);
- return strong_parser(text);
- }
- static void html(String text) throws IOException {
- File htmlFile = new File("output.html");
- htmlFile.createNewFile();
- text = strong_parser(text);
- BufferedWriter bw = new BufferedWriter(new FileWriter(htmlFile, true));
- bw.append(text).append(" ");
- bw.close();
- }
- public static void main(String[] args) throws IOException {
- //String text = "[s][u][b][i]Text[/i][/b][/u][/s]";
- String text = "[s][url=https://ru.wikipedia.org]Текст[/url][/s]";
- html(text);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement