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.macro.code; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.LinkedHashMap; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Map; |
28 |
|
|
29 |
|
import javax.inject.Inject; |
30 |
|
import javax.inject.Named; |
31 |
|
import javax.inject.Singleton; |
32 |
|
|
33 |
|
import org.apache.commons.lang3.StringUtils; |
34 |
|
import org.slf4j.Logger; |
35 |
|
import org.xwiki.component.annotation.Component; |
36 |
|
import org.xwiki.component.manager.ComponentLookupException; |
37 |
|
import org.xwiki.component.manager.ComponentManager; |
38 |
|
import org.xwiki.rendering.block.Block; |
39 |
|
import org.xwiki.rendering.block.FormatBlock; |
40 |
|
import org.xwiki.rendering.block.GroupBlock; |
41 |
|
import org.xwiki.rendering.listener.Format; |
42 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
43 |
|
import org.xwiki.rendering.macro.box.AbstractBoxMacro; |
44 |
|
import org.xwiki.rendering.macro.code.CodeMacroParameters; |
45 |
|
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor; |
46 |
|
import org.xwiki.rendering.parser.HighlightParser; |
47 |
|
import org.xwiki.rendering.parser.ParseException; |
48 |
|
import org.xwiki.rendering.parser.Parser; |
49 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@version |
55 |
|
@since |
56 |
|
|
57 |
|
@Component |
58 |
|
@Named("code") |
59 |
|
@Singleton |
|
|
| 89.7% |
Uncovered Elements: 4 (39) |
Complexity: 10 |
Complexity Density: 0.38 |
|
60 |
|
public class CodeMacro extends AbstractBoxMacro<CodeMacroParameters> |
61 |
|
{ |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private static final String DESCRIPTION = "Highlights code snippets of various programming languages"; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
private static final String LANGUAGE_NONE = "none"; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
private static final String CONTENT_DESCRIPTION = "the content to highlight"; |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@Inject |
81 |
|
@Named("plain/1.0") |
82 |
|
private Parser plainTextParser; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
@Inject |
88 |
|
private ComponentManager componentManager; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
@Inject |
94 |
|
private Logger logger; |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
99 |
15 |
public CodeMacro()... |
100 |
|
{ |
101 |
15 |
super("Code", DESCRIPTION, new DefaultContentDescriptor(CONTENT_DESCRIPTION), CodeMacroParameters.class); |
102 |
15 |
setDefaultCategory(DEFAULT_CATEGORY_FORMATTING); |
103 |
|
} |
104 |
|
|
|
|
| 85% |
Uncovered Elements: 3 (20) |
Complexity: 5 |
Complexity Density: 0.36 |
|
105 |
16 |
@Override... |
106 |
|
protected List<Block> parseContent(CodeMacroParameters parameters, String content, |
107 |
|
MacroTransformationContext context) throws MacroExecutionException |
108 |
|
{ |
109 |
16 |
List<Block> result; |
110 |
16 |
try { |
111 |
16 |
if (LANGUAGE_NONE.equalsIgnoreCase(parameters.getLanguage())) { |
112 |
3 |
if (StringUtils.isEmpty(content)) { |
113 |
0 |
result = Collections.emptyList(); |
114 |
|
} else { |
115 |
3 |
result = this.plainTextParser.parse(new StringReader(content)).getChildren().get(0).getChildren(); |
116 |
|
} |
117 |
|
} else { |
118 |
13 |
result = highlight(parameters, content); |
119 |
|
} |
120 |
|
} catch (Exception e) { |
121 |
0 |
throw new MacroExecutionException("Failed to highlight content", e); |
122 |
|
} |
123 |
|
|
124 |
16 |
Map<String, String> formatParameters = new LinkedHashMap<String, String>(); |
125 |
16 |
formatParameters.put("class", "code"); |
126 |
|
|
127 |
16 |
if (context.isInline()) { |
128 |
2 |
result = Arrays.<Block> asList(new FormatBlock(result, Format.NONE, formatParameters)); |
129 |
|
} else { |
130 |
14 |
result = Arrays.<Block> asList(new GroupBlock(result, formatParameters)); |
131 |
|
} |
132 |
|
|
133 |
16 |
return result; |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
@param |
140 |
|
@param |
141 |
|
@return |
142 |
|
@throws |
143 |
|
@throws |
144 |
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
145 |
13 |
protected List<Block> highlight(CodeMacroParameters parameters, String content) throws ParseException,... |
146 |
|
ComponentLookupException |
147 |
|
{ |
148 |
13 |
HighlightParser parser; |
149 |
|
|
150 |
13 |
if (parameters.getLanguage() != null) { |
151 |
11 |
if (this.componentManager.hasComponent(HighlightParser.class, parameters.getLanguage())) { |
152 |
1 |
try { |
153 |
1 |
parser = this.componentManager.getInstance(HighlightParser.class, parameters.getLanguage()); |
154 |
1 |
return parser.highlight(parameters.getLanguage(), new StringReader(content)); |
155 |
|
} catch (ComponentLookupException e) { |
156 |
0 |
this.logger.error("Faild to load highlighting parser for language [{}]", parameters.getLanguage(), |
157 |
|
e); |
158 |
|
} |
159 |
|
} |
160 |
|
} |
161 |
|
|
162 |
12 |
this.logger.debug( |
163 |
|
"Can't find any specific highlighting parser for language [{}]. Trying the default highlighting parser.", |
164 |
|
parameters.getLanguage()); |
165 |
|
|
166 |
12 |
parser = this.componentManager.getInstance(HighlightParser.class, "default"); |
167 |
|
|
168 |
12 |
return parser.highlight(parameters.getLanguage(), new StringReader(content)); |
169 |
|
} |
170 |
|
} |