-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwigBeautify.java
More file actions
201 lines (167 loc) · 7.17 KB
/
TwigBeautify.java
File metadata and controls
201 lines (167 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class TwigBeautify {
private static String generateTabs(int tabs) {
String tabString = "";
for (int i = 0; i < tabs; i++) {
tabString += "\t";
}
return tabString;
}
private static String removeLeadingAndTrailingTilde(String twig) {
twig = twig.replaceAll("\\{\\{\\s~", "{{");
twig = twig.replaceAll("~\\s\\}\\}", "}}");
return twig;
}
private static String beautifyTwig(String twig) {
twig = removeLeadingAndTrailingTilde(twig);
Pattern pattern = Pattern.compile("((?:\\{%-?|\\{\\{-?|\\[\\[)[^\\}\\]]+(?:\\}|\\])(?:\\}|\\])?)(?=((?:\\{%-?|\\{\\{-?|\\[\\[)[^\\}\\]]+(?:\\}|\\])(?:\\}|\\])?))?");
StringBuffer output = new StringBuffer();
Matcher matcher = pattern.matcher(twig);
int tabIndex = 0;
String tag;
String nextTag;
String rep = "";
boolean isDobuleLine = false;
if (!twig.contains("\n")) {
while (matcher.find()) {
tag = matcher.group(1);
nextTag = matcher.group(2);
// set
if (tag.contains("{% set") || tag.contains("{%- set")) {
isDobuleLine = true;
if (tag.contains("=")) {
rep = String.format("%1$s\n%2$s", tag, generateTabs(tabIndex));
} else {
rep = String.format("%s", tag);
}
// endset
} else if (tag.contains("{% endset") || tag.contains("{%- endset")) {
isDobuleLine = true;
rep = String.format("%1$s\n%2$s", tag, generateTabs(tabIndex));
// if
} else if (tag.contains("{% if") || tag.contains("{%- if")) {
tabIndex++;
rep = String.format("%1$s%2$s\n%3$s", isDobuleLine ? "\n" : "", tag, generateTabs(tabIndex));
// elseif and elseif
} else if (tag.contains("{% else") || tag.contains("{%- else")) {
tabIndex--;
rep = String.format("%1$s\n%3$s%2$s\n%3$s\t", isDobuleLine ? "\n" : "", tag, generateTabs(tabIndex));
tabIndex++;
// endif
} else if (tag.contains("{% endif") || tag.contains("{%- endif")) {
tabIndex--;
if (nextTag != null && (nextTag.contains("{% if") || nextTag.contains("{%- if") || nextTag.contains("{{") || nextTag.contains("[["))) {
rep = String.format("%1$s\n%3$s%2$s\n%3$s", isDobuleLine ? "\n" : "", tag, generateTabs(tabIndex));
} else {
rep = String.format("%1$s\n%3$s%2$s", isDobuleLine ? "\n" : "", tag, generateTabs(tabIndex));
}
// <print>
} else if (tag.contains("{{")) {
if (nextTag != null && (nextTag.contains("{% if") || nextTag.contains("{%- if") || nextTag.contains("{{") || nextTag.contains("[["))) {
rep = String.format("%1$s%2$s\n%3$s", isDobuleLine ? "\n" : "", tag, generateTabs(tabIndex));
} else {
rep = String.format("%1$s%2$s", isDobuleLine ? "\n" : "", tag);
}
// <custom twig>
} else if (tag.contains("[[")) {
if (nextTag != null && (nextTag.contains("{% endset") || nextTag.contains("{%- endset"))) {
rep = String.format("%1$s", tag);
} else if (nextTag != null && (nextTag.contains("{% if") || nextTag.contains("{%- if") || nextTag.contains("{{") || nextTag.contains("[["))) {
rep = String.format("%1$s%2$s\n%3$s", isDobuleLine ? "\n" : "", tag, generateTabs(tabIndex));
} else {
rep = String.format("%1$s%2$s", isDobuleLine ? "\n" : "", tag);
}
// <other>
} else {
rep = String.format("%s", tag);
}
if (isDobuleLine && !tag.contains("{% set") && !tag.contains("{%- set") && !tag.contains("{% endset") && !tag.contains("{%- endset")) {
isDobuleLine = false;
}
matcher.appendReplacement(output, rep);
}
matcher.appendTail(output);
return output.toString();
}
return twig;
}
private static String makeOneLineTwig(String twig) {
twig = twig.replaceAll("(?m)^\\s+", "");
twig = twig.replaceAll("\\n", "");
return twig;
}
private static String toggleWhitespaceControl(String twig) {
Pattern pattern = Pattern.compile("(\\{%|\\{\\{)\\h");
Matcher matcher = pattern.matcher(twig);
if (matcher.find()) {
twig = twig.replaceAll("(\\{%|\\{\\{)\\h", "$1- ");
twig = twig.replaceAll("\\h(%\\}|\\}\\})", " -$1");
} else {
twig = twig.replaceAll("(\\{%|\\{\\{)-\\h", "$1 ");
twig = twig.replaceAll("\\h-(%\\}|\\}\\})", " $1");
}
return twig;
}
private static void createGUI() {
JFrame frame = new JFrame("Twig Beautify");
JPanel panel = new JPanel();
JPanel buttonPanel = new JPanel();
JTextArea editor = new JTextArea();
editor.setFont(new Font("Consolas", Font.PLAIN, 12));
editor.setTabSize(2);
JButton beautify = new JButton("Beautify");
beautify.setBounds(10,10,10,10);
beautify.setAlignmentX(Component.CENTER_ALIGNMENT);
beautify.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
editor.setText(beautifyTwig(editor.getText()));
}
});
JButton makeOneLine = new JButton("One line");
makeOneLine.setBounds(10,10,10,10);
makeOneLine.setAlignmentX(Component.CENTER_ALIGNMENT);
makeOneLine.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
editor.setText(makeOneLineTwig(editor.getText()));
}
});
JButton toggleWhitespaceControl = new JButton("Whitespace control");
toggleWhitespaceControl.setBounds(10,10,10,10);
toggleWhitespaceControl.setAlignmentX(Component.CENTER_ALIGNMENT);
toggleWhitespaceControl.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
editor.setText(toggleWhitespaceControl(editor.getText()));
}
});
JScrollPane scroll = new JScrollPane(editor);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
buttonPanel.add(toggleWhitespaceControl);
buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPanel.add(makeOneLine);
buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
buttonPanel.add(beautify);
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
panel.add(scroll);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(buttonPanel);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(540,620);
frame.setResizable(false);
frame.setVisible(true);
}
public static void main(String args[]) {
createGUI();
}
}