1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.parser.pygments

File BlocksGeneratorPygmentsListener.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

10
35
12
1
207
98
20
0.57
2.92
12
1.67

Classes

Class Line # Actions
BlocksGeneratorPygmentsListener 43 35 0% 20 3
0.9473684494.7%
 

Contributing tests

This file is covered by 11 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Transforms Pygments tokens into XWiki Rendering blocks. This class is overwritten in python an methods are called
38    * from there.
39    *
40    * @version $Id: 415ba177f01b940e20872f13c4d8e48545101bc2 $
41    * @since 1.7RC1
42    */
 
43    public class BlocksGeneratorPygmentsListener implements PygmentsListener
44    {
45    /**
46    * The highlighted result block.
47    */
48    private List<Block> blocks = new ArrayList<Block>();
49   
50    /**
51    * Used to convert Pygment token values into blocks.
52    */
53    private Parser plainTextParser;
54   
55    /**
56    * @param plainTextParser the parser we'll use to parse Pygment token values into blocks
57    */
 
58  11 toggle public BlocksGeneratorPygmentsListener(Parser plainTextParser)
59    {
60  11 this.plainTextParser = plainTextParser;
61    }
62   
63    /**
64    * @return the highlighted result block.
65    */
 
66  10 toggle public List<Block> getBlocks()
67    {
68  10 return this.blocks;
69    }
70   
 
71  537 toggle @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    // This shouldn't happen since the parser cannot throw an exception since the source is a memory
81    // String.
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    * Create css style from Pygments style map.
100    *
101    * @param style the Pygments style map.
102    * @return the css style.
103    */
 
104  537 toggle 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    * Add css bold style to style property.
121    *
122    * @param styleOut the {@link StringBuffer} to append to.
123    * @param styles the Pygments style map.
124    */
 
125  537 toggle protected void appendBold(StringBuffer styleOut, Map<String, Object> styles)
126    {
127  537 appendBoolean(styleOut, "bold", "font-weight: bold; ", styles);
128    }
129   
130    /**
131    * @param styleOut the {@link StringBuffer} to append to.
132    * @param styles the Pygments style map.
133    */
 
134  537 toggle protected void appendItalic(StringBuffer styleOut, Map<String, Object> styles)
135    {
136  537 appendBoolean(styleOut, "italic", "font-weight: italic; ", styles);
137    }
138   
139    /**
140    * @param styleOut the {@link StringBuffer} to append to.
141    * @param styles the Pygments style map.
142    */
 
143  537 toggle protected void appendUnderline(StringBuffer styleOut, Map<String, Object> styles)
144    {
145  537 appendBoolean(styleOut, "underline", "text-decoration: underline; ", styles);
146    }
147   
148    /**
149    * @param styleOut the {@link StringBuffer} to append to.
150    * @param styles the Pygments style map.
151    */
 
152  537 toggle protected void appendColor(StringBuffer styleOut, Map<String, Object> styles)
153    {
154  537 appendStringValue(styleOut, "color", "color: #{0}; ", styles);
155    }
156   
157    /**
158    * @param styleOut the {@link StringBuffer} to append to.
159    * @param styles the Pygments style map.
160    */
 
161  537 toggle protected void appendBgColor(StringBuffer styleOut, Map<String, Object> styles)
162    {
163  537 appendStringValue(styleOut, "bgcolor", "background-color: #{0}; ", styles);
164    }
165   
166    /**
167    * @param styleOut the {@link StringBuffer} to append to.
168    * @param styles the Pygments style map.
169    */
 
170  537 toggle 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 styleOut the {@link StringBuffer} to append to.
177    * @param pyName the name of the Pygments property.
178    * @param cssPattern the {@link MessageFormat} style pattern to append with the found value to the css
179    * {@link StringBuffer}.
180    * @param styles the Pygments style map.
181    */
 
182  1611 toggle 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 styleOut the {@link StringBuffer} to append to.
193    * @param pyName the name of the Pygments property.
194    * @param cssValue the css to append to provided @link StringBuffer}.
195    * @param styles the Pygments style map.
196    */
 
197  1611 toggle 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    }