| 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.IOException; |
| 23 |
|
import java.io.InputStream; |
| 24 |
|
import java.io.Reader; |
| 25 |
|
import java.io.StringReader; |
| 26 |
|
import java.util.Collections; |
| 27 |
|
import java.util.List; |
| 28 |
|
|
| 29 |
|
import javax.inject.Inject; |
| 30 |
|
import javax.inject.Named; |
| 31 |
|
import javax.inject.Singleton; |
| 32 |
|
import javax.script.ScriptContext; |
| 33 |
|
import javax.script.ScriptEngine; |
| 34 |
|
import javax.script.ScriptEngineManager; |
| 35 |
|
import javax.script.ScriptException; |
| 36 |
|
import javax.script.SimpleScriptContext; |
| 37 |
|
|
| 38 |
|
import org.apache.commons.io.IOUtils; |
| 39 |
|
import org.xwiki.component.annotation.Component; |
| 40 |
|
import org.xwiki.component.phase.Initializable; |
| 41 |
|
import org.xwiki.component.phase.InitializationException; |
| 42 |
|
import org.xwiki.rendering.block.Block; |
| 43 |
|
import org.xwiki.rendering.block.NewLineBlock; |
| 44 |
|
import org.xwiki.rendering.parser.AbstractHighlightParser; |
| 45 |
|
import org.xwiki.rendering.parser.HighlightParser; |
| 46 |
|
import org.xwiki.rendering.parser.ParseException; |
| 47 |
|
import org.xwiki.rendering.parser.Parser; |
| 48 |
|
import org.xwiki.rendering.syntax.Syntax; |
| 49 |
|
import org.xwiki.rendering.syntax.SyntaxType; |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
@version |
| 55 |
|
@since |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
@Component(roles = {HighlightParser.class }) |
| 61 |
|
@Singleton |
| |
|
| 83% |
Uncovered Elements: 9 (53) |
Complexity: 14 |
Complexity Density: 0.36 |
|
| 62 |
|
public class PygmentsParser extends AbstractHighlightParser implements Initializable |
| 63 |
|
{ |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
private static final String PY_STYLE_VARNAME = "style"; |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
private static final String PY_LISTENER_VARNAME = "listener"; |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
private static final String PY_CODE_VARNAME = "code"; |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
private static final String PY_LANGUAGE_VARNAME = "language"; |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
private static final String PY_LEXER_VARNAME = "pygmentLexer"; |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
private static final String ENGINE_ID = "python"; |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
private Syntax syntax; |
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
@Inject |
| 103 |
|
@Named("plain/1.0") |
| 104 |
|
private Parser plainTextParser; |
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
@Inject |
| 110 |
|
private PygmentsParserConfiguration configuration; |
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
private ScriptEngine engine; |
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
private String script; |
| 121 |
|
|
| |
|
| 70.6% |
Uncovered Elements: 5 (17) |
Complexity: 4 |
Complexity Density: 0.31 |
|
| 122 |
12 |
@Override... |
| 123 |
|
public void initialize() throws InitializationException |
| 124 |
|
{ |
| 125 |
12 |
ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); |
| 126 |
|
|
| 127 |
|
|
| 128 |
12 |
InputStream is = getClass().getResourceAsStream("/pygments/code.py"); |
| 129 |
12 |
if (is != null) { |
| 130 |
12 |
try { |
| 131 |
12 |
this.script = IOUtils.toString(is, "UTF8"); |
| 132 |
|
} catch (Exception e) { |
| 133 |
0 |
throw new InitializationException("Failed to read resource /pygments/code.py resource", e); |
| 134 |
|
} finally { |
| 135 |
12 |
IOUtils.closeQuietly(is); |
| 136 |
|
} |
| 137 |
|
} else { |
| 138 |
0 |
throw new InitializationException("Failed to find resource /pygments/code.py resource"); |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
|
| 142 |
12 |
this.engine = scriptEngineManager.getEngineByName(ENGINE_ID); |
| 143 |
|
|
| 144 |
12 |
if (this.engine == null) { |
| 145 |
0 |
throw new InitializationException("Failed to find engine for Python script language"); |
| 146 |
|
} |
| 147 |
|
|
| 148 |
12 |
String highlightSyntaxId = getSyntaxId() + "-highlight"; |
| 149 |
12 |
this.syntax = new Syntax(new SyntaxType(highlightSyntaxId, highlightSyntaxId), "1.0"); |
| 150 |
|
} |
| 151 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 152 |
0 |
@Override... |
| 153 |
|
public Syntax getSyntax() |
| 154 |
|
{ |
| 155 |
0 |
return this.syntax; |
| 156 |
|
} |
| 157 |
|
|
| |
|
| 88.2% |
Uncovered Elements: 2 (17) |
Complexity: 7 |
Complexity Density: 0.54 |
|
| 158 |
12 |
@Override... |
| 159 |
|
public List<Block> highlight(String syntaxId, Reader source) throws ParseException |
| 160 |
|
{ |
| 161 |
12 |
String code; |
| 162 |
12 |
try { |
| 163 |
12 |
code = IOUtils.toString(source); |
| 164 |
|
} catch (IOException e) { |
| 165 |
0 |
throw new ParseException("Failed to read source", e); |
| 166 |
|
} |
| 167 |
|
|
| 168 |
12 |
if (code.length() == 0) { |
| 169 |
1 |
return Collections.emptyList(); |
| 170 |
|
} |
| 171 |
|
|
| 172 |
11 |
List<Block> blocks; |
| 173 |
11 |
try { |
| 174 |
11 |
blocks = highlight(syntaxId, code); |
| 175 |
|
} catch (ScriptException e) { |
| 176 |
0 |
throw new ParseException("Failed to highlight code", e); |
| 177 |
|
} |
| 178 |
|
|
| 179 |
|
|
| 180 |
11 |
if (code.charAt(code.length() - 1) != '\n' && !blocks.isEmpty() |
| 181 |
|
&& blocks.get(blocks.size() - 1) instanceof NewLineBlock) { |
| 182 |
10 |
blocks.remove(blocks.size() - 1); |
| 183 |
|
} |
| 184 |
|
|
| 185 |
11 |
return blocks; |
| 186 |
|
} |
| 187 |
|
|
| 188 |
|
|
| 189 |
|
|
| 190 |
|
|
| 191 |
|
@param |
| 192 |
|
@param |
| 193 |
|
@return |
| 194 |
|
@throws |
| 195 |
|
@throws |
| 196 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 2 |
Complexity Density: 0.17 |
|
| 197 |
11 |
private List<Block> highlight(String syntaxId, String code) throws ScriptException, ParseException... |
| 198 |
|
{ |
| 199 |
11 |
BlocksGeneratorPygmentsListener listener = new BlocksGeneratorPygmentsListener(this.plainTextParser); |
| 200 |
|
|
| 201 |
11 |
ScriptContext scriptContext = new SimpleScriptContext(); |
| 202 |
|
|
| 203 |
11 |
scriptContext.setAttribute(PY_LANGUAGE_VARNAME, syntaxId, ScriptContext.ENGINE_SCOPE); |
| 204 |
11 |
scriptContext.setAttribute(PY_CODE_VARNAME, code, ScriptContext.ENGINE_SCOPE); |
| 205 |
11 |
scriptContext.setAttribute(PY_STYLE_VARNAME, this.configuration.getStyle(), ScriptContext.ENGINE_SCOPE); |
| 206 |
11 |
scriptContext.setAttribute(PY_LISTENER_VARNAME, listener, ScriptContext.ENGINE_SCOPE); |
| 207 |
|
|
| 208 |
11 |
this.engine.eval(this.script, scriptContext); |
| 209 |
|
|
| 210 |
11 |
List<Block> blocks; |
| 211 |
11 |
if (scriptContext.getAttribute(PY_LEXER_VARNAME) != null) { |
| 212 |
10 |
blocks = listener.getBlocks(); |
| 213 |
|
} else { |
| 214 |
1 |
blocks = this.plainTextParser.parse(new StringReader(code)).getChildren().get(0).getChildren(); |
| 215 |
|
} |
| 216 |
|
|
| 217 |
11 |
return blocks; |
| 218 |
|
} |
| 219 |
|
} |