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.formula; |
21 |
|
|
22 |
|
import java.util.Collections; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Singleton; |
28 |
|
|
29 |
|
import org.apache.commons.lang3.StringUtils; |
30 |
|
import org.slf4j.Logger; |
31 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.component.manager.ComponentManager; |
34 |
|
import org.xwiki.formula.FormulaRenderer; |
35 |
|
import org.xwiki.formula.FormulaRenderer.FontSize; |
36 |
|
import org.xwiki.formula.FormulaRenderer.Type; |
37 |
|
import org.xwiki.model.reference.AttachmentReference; |
38 |
|
import org.xwiki.rendering.block.Block; |
39 |
|
import org.xwiki.rendering.block.ImageBlock; |
40 |
|
import org.xwiki.rendering.block.ParagraphBlock; |
41 |
|
import org.xwiki.rendering.block.WordBlock; |
42 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
43 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
44 |
|
import org.xwiki.rendering.macro.AbstractMacro; |
45 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
46 |
|
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor; |
47 |
|
import org.xwiki.rendering.macro.formula.FormulaMacroConfiguration; |
48 |
|
import org.xwiki.rendering.macro.formula.FormulaMacroParameters; |
49 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@version |
55 |
|
@since |
56 |
|
|
57 |
|
@Component |
58 |
|
@Named("formula") |
59 |
|
@Singleton |
|
|
| 69% |
Uncovered Elements: 13 (42) |
Complexity: 11 |
Complexity Density: 0.34 |
|
60 |
|
public class FormulaMacro extends AbstractMacro<FormulaMacroParameters> |
61 |
|
{ |
62 |
|
|
63 |
|
public static final String CONTENT_MISSING_ERROR = "The mandatory formula text is missing."; |
64 |
|
|
65 |
|
|
66 |
|
public static final String WRONG_CONTENT_ERROR = "The formula text is not valid, please correct it."; |
67 |
|
|
68 |
|
|
69 |
|
private static final String DESCRIPTION = "Displays a mathematical formula."; |
70 |
|
|
71 |
|
|
72 |
|
private static final String CONTENT_DESCRIPTION = "The mathematical formula, in LaTeX syntax"; |
73 |
|
|
74 |
|
|
75 |
|
@Inject |
76 |
|
private ComponentManager manager; |
77 |
|
|
78 |
|
|
79 |
|
@Inject |
80 |
|
private FormulaMacroConfiguration configuration; |
81 |
|
|
82 |
|
|
83 |
|
@Inject |
84 |
|
private DocumentAccessBridge dab; |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@Inject |
90 |
|
private Logger logger; |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
95 |
2 |
public FormulaMacro()... |
96 |
|
{ |
97 |
2 |
super("Formula", DESCRIPTION, new DefaultContentDescriptor(CONTENT_DESCRIPTION), FormulaMacroParameters.class); |
98 |
2 |
setDefaultCategory(DEFAULT_CATEGORY_CONTENT); |
99 |
|
} |
100 |
|
|
|
|
| 58.3% |
Uncovered Elements: 10 (24) |
Complexity: 7 |
Complexity Density: 0.39 |
|
101 |
2 |
@Override... |
102 |
|
public List<Block> execute(FormulaMacroParameters parameters, String content, MacroTransformationContext context) |
103 |
|
throws MacroExecutionException |
104 |
|
{ |
105 |
2 |
if (StringUtils.isEmpty(content)) { |
106 |
0 |
throw new MacroExecutionException(CONTENT_MISSING_ERROR); |
107 |
|
} |
108 |
|
|
109 |
2 |
String rendererHint = this.configuration.getRenderer(); |
110 |
2 |
FontSize size = parameters.getFontSize(); |
111 |
2 |
Type type = parameters.getImageType(); |
112 |
2 |
Block result; |
113 |
2 |
try { |
114 |
2 |
result = render(content, context.isInline(), size, type, rendererHint); |
115 |
|
} catch (MacroExecutionException ex) { |
116 |
0 |
this.logger.debug("Failed to render content with the [{}] renderer. Falling back to the safe renderer.", |
117 |
|
rendererHint, ex); |
118 |
0 |
try { |
119 |
0 |
result = render(content, context.isInline(), size, type, this.configuration.getSafeRenderer()); |
120 |
|
} catch (IllegalArgumentException ex2) { |
121 |
0 |
throw new MacroExecutionException(WRONG_CONTENT_ERROR); |
122 |
|
} |
123 |
|
} catch (IllegalArgumentException ex) { |
124 |
0 |
throw new MacroExecutionException(WRONG_CONTENT_ERROR); |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
2 |
if (result == null) { |
129 |
0 |
result = new WordBlock(content); |
130 |
|
} |
131 |
|
|
132 |
2 |
if (!context.isInline()) { |
133 |
2 |
result = new ParagraphBlock(Collections.<Block> singletonList(result)); |
134 |
|
} |
135 |
2 |
return Collections.singletonList(result); |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
@param |
142 |
|
@param |
143 |
|
@param |
144 |
|
@param |
145 |
|
@param |
146 |
|
@return |
147 |
|
@throws |
148 |
|
|
149 |
|
@throws |
150 |
|
|
|
|
| 90.9% |
Uncovered Elements: 1 (11) |
Complexity: 2 |
Complexity Density: 0.18 |
|
151 |
2 |
private Block render(String formula, boolean inline, FontSize fontSize, Type imageType, String rendererHint)... |
152 |
|
throws MacroExecutionException, IllegalArgumentException |
153 |
|
{ |
154 |
2 |
try { |
155 |
2 |
FormulaRenderer renderer = this.manager.getInstance(FormulaRenderer.class, rendererHint); |
156 |
2 |
String imageName = renderer.process(formula, inline, fontSize, imageType); |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
2 |
AttachmentReference attachmentReference = |
164 |
|
new AttachmentReference(imageName, this.dab.getCurrentDocumentReference()); |
165 |
2 |
String url = this.dab.getAttachmentURL(attachmentReference, false); |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
2 |
url = url.replace("/download/", "/tex/"); |
170 |
|
|
171 |
2 |
ResourceReference imageReference = new ResourceReference(url, ResourceType.URL); |
172 |
2 |
ImageBlock result = new ImageBlock(imageReference, false); |
173 |
|
|
174 |
2 |
result.setParameter("alt", formula); |
175 |
2 |
return result; |
176 |
|
} catch (Exception e) { |
177 |
0 |
throw new MacroExecutionException( |
178 |
|
String.format("Failed to render formula using the [%s] renderer", rendererHint), e); |
179 |
|
} |
180 |
|
} |
181 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
182 |
0 |
@Override... |
183 |
|
public boolean supportsInlineMode() |
184 |
|
{ |
185 |
0 |
return true; |
186 |
|
} |
187 |
|
} |