1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.parser.pygments; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.text.MessageFormat; |
24 |
|
import java.util.ArrayList; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Map; |
27 |
|
|
28 |
|
import org.apache.commons.lang3.StringUtils; |
29 |
|
import org.python.core.PyNone; |
30 |
|
import org.xwiki.rendering.block.Block; |
31 |
|
import org.xwiki.rendering.block.FormatBlock; |
32 |
|
import org.xwiki.rendering.listener.Format; |
33 |
|
import org.xwiki.rendering.parser.ParseException; |
34 |
|
import org.xwiki.rendering.parser.Parser; |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
@version |
41 |
|
@since |
42 |
|
|
|
|
| 94.7% |
Uncovered Elements: 3 (57) |
Complexity: 20 |
Complexity Density: 0.57 |
|
43 |
|
public class BlocksGeneratorPygmentsListener implements PygmentsListener |
44 |
|
{ |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
private List<Block> blocks = new ArrayList<Block>(); |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
private Parser plainTextParser; |
54 |
|
|
55 |
|
|
56 |
|
@param |
57 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
58 |
11 |
public BlocksGeneratorPygmentsListener(Parser plainTextParser)... |
59 |
|
{ |
60 |
11 |
this.plainTextParser = plainTextParser; |
61 |
|
} |
62 |
|
|
63 |
|
|
64 |
|
@return |
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
66 |
10 |
public List<Block> getBlocks()... |
67 |
|
{ |
68 |
10 |
return this.blocks; |
69 |
|
} |
70 |
|
|
|
|
| 87.5% |
Uncovered Elements: 2 (16) |
Complexity: 4 |
Complexity Density: 0.33 |
|
71 |
537 |
@Override... |
72 |
|
public void format(String tokenType, String value, Map<String, Object> style) |
73 |
|
{ |
74 |
537 |
List<Block> blockList; |
75 |
|
|
76 |
537 |
if (StringUtils.isNotEmpty(value)) { |
77 |
537 |
try { |
78 |
537 |
blockList = this.plainTextParser.parse(new StringReader(value)).getChildren().get(0).getChildren(); |
79 |
|
} catch (ParseException e) { |
80 |
|
|
81 |
|
|
82 |
0 |
throw new RuntimeException("Failed to parse [" + value + "] as plain text.", e); |
83 |
|
} |
84 |
|
|
85 |
537 |
String styleParameter = formatStyle(style); |
86 |
|
|
87 |
537 |
FormatBlock formatBlock = null; |
88 |
537 |
if (styleParameter.length() > 0) { |
89 |
219 |
formatBlock = new FormatBlock(blockList, Format.NONE); |
90 |
219 |
formatBlock.setParameter("style", styleParameter); |
91 |
219 |
this.blocks.add(formatBlock); |
92 |
|
} else { |
93 |
318 |
this.blocks.addAll(blockList); |
94 |
|
} |
95 |
|
} |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
@param |
102 |
|
@return |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
104 |
537 |
protected String formatStyle(Map<String, Object> style)... |
105 |
|
{ |
106 |
537 |
StringBuffer styleOut = new StringBuffer(); |
107 |
|
|
108 |
537 |
appendBold(styleOut, style); |
109 |
537 |
appendItalic(styleOut, style); |
110 |
537 |
appendUnderline(styleOut, style); |
111 |
|
|
112 |
537 |
appendColor(styleOut, style); |
113 |
537 |
appendBgColor(styleOut, style); |
114 |
537 |
appendBorder(styleOut, style); |
115 |
|
|
116 |
537 |
return styleOut.toString(); |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
@param@link |
123 |
|
@param |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
125 |
537 |
protected void appendBold(StringBuffer styleOut, Map<String, Object> styles)... |
126 |
|
{ |
127 |
537 |
appendBoolean(styleOut, "bold", "font-weight: bold; ", styles); |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
@param@link |
132 |
|
@param |
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
134 |
537 |
protected void appendItalic(StringBuffer styleOut, Map<String, Object> styles)... |
135 |
|
{ |
136 |
537 |
appendBoolean(styleOut, "italic", "font-weight: italic; ", styles); |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
|
@param@link |
141 |
|
@param |
142 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
143 |
537 |
protected void appendUnderline(StringBuffer styleOut, Map<String, Object> styles)... |
144 |
|
{ |
145 |
537 |
appendBoolean(styleOut, "underline", "text-decoration: underline; ", styles); |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
|
@param@link |
150 |
|
@param |
151 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
152 |
537 |
protected void appendColor(StringBuffer styleOut, Map<String, Object> styles)... |
153 |
|
{ |
154 |
537 |
appendStringValue(styleOut, "color", "color: #{0}; ", styles); |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
|
@param@link |
159 |
|
@param |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
161 |
537 |
protected void appendBgColor(StringBuffer styleOut, Map<String, Object> styles)... |
162 |
|
{ |
163 |
537 |
appendStringValue(styleOut, "bgcolor", "background-color: #{0}; ", styles); |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
|
@param@link |
168 |
|
@param |
169 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
170 |
537 |
protected void appendBorder(StringBuffer styleOut, Map<String, Object> styles)... |
171 |
|
{ |
172 |
537 |
appendStringValue(styleOut, "border", "border: 1px solid #{0}; ", styles); |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
@param@link |
177 |
|
@param |
178 |
|
@param@link |
179 |
|
@link |
180 |
|
@param |
181 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
182 |
1611 |
private void appendStringValue(StringBuffer styleOut, String pyName, String cssPattern, Map<String, Object> styles)... |
183 |
|
{ |
184 |
1611 |
Object obj = styles.get(pyName); |
185 |
|
|
186 |
1611 |
if (obj != null && !(obj instanceof PyNone)) { |
187 |
220 |
styleOut.append(MessageFormat.format(cssPattern, obj)); |
188 |
|
} |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
@param@link |
193 |
|
@param |
194 |
|
@param@link |
195 |
|
@param |
196 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 4 |
Complexity Density: 1 |
|
197 |
1611 |
private void appendBoolean(StringBuffer styleOut, String pyName, String cssValue, Map<String, Object> styles)... |
198 |
|
{ |
199 |
1611 |
Object obj = styles.get(pyName); |
200 |
|
|
201 |
1611 |
if (obj != null && !(obj instanceof PyNone)) { |
202 |
1611 |
if (((Boolean) obj)) { |
203 |
31 |
styleOut.append(cssValue); |
204 |
|
} |
205 |
|
} |
206 |
|
} |
207 |
|
} |