use of org.eclipse.mylyn.wikitext.parser.css.CssParser in project mylyn.docs by eclipse.
the class SpanStrategies method calculateAlternateSpanStrategy.
private SpanStrategy calculateAlternateSpanStrategy(Attributes attributes) {
List<SpanStrategy> strategies = new ArrayList<>();
String cssStyle = attributes.getCssStyle();
if (cssStyle != null) {
Iterator<CssRule> rules = new CssParser().createRuleIterator(cssStyle);
while (rules.hasNext()) {
CssRule rule = rules.next();
if (rule.name.equals("font-weight") && rule.value.equals("bold")) {
// $NON-NLS-1$ //$NON-NLS-2$
strategies.add(calculateAlternateSpanStrategy(SpanType.BOLD));
} else if (rule.name.equals("font-style") && rule.value.equals("italic")) {
// $NON-NLS-1$ //$NON-NLS-2$
strategies.add(calculateAlternateSpanStrategy(SpanType.ITALIC));
} else if (rule.name.equals("text-decoration") && rule.value.equalsIgnoreCase("underline")) {
// $NON-NLS-1$ //$NON-NLS-2$
strategies.add(calculateAlternateSpanStrategy(SpanType.UNDERLINED));
} else if (rule.name.equals("text-decoration") && rule.value.equalsIgnoreCase("line-through")) {
// $NON-NLS-1$ //$NON-NLS-2$
strategies.add(calculateAlternateSpanStrategy(SpanType.DELETED));
} else if (rule.name.equals("font-family") && isFontFamilyMonospace(rule)) {
// $NON-NLS-1$
strategies.add(new SubstitutionWithoutCssSpanStrategy(SpanType.MONOSPACE));
}
}
}
strategies = ImmutableList.copyOf(FluentIterable.from(strategies).filter(not(isNull())));
if (strategies.isEmpty()) {
return null;
} else if (strategies.size() == 1) {
return strategies.get(0);
}
return new CompositeSpanStrategy(strategies);
}
use of org.eclipse.mylyn.wikitext.parser.css.CssParser in project mylyn.docs by eclipse.
the class MarkupViewerPreferencePage method updatePreview.
private void updatePreview() {
TextPresentation textPresentation = new TextPresentation();
HtmlTextPresentationParser parser = new HtmlTextPresentationParser();
parser.setDefaultFont(previewViewer.getTextWidget().getFont());
parser.setAnnotationModel(previewViewer.getAnnotationModel());
parser.setPresentation(textPresentation);
parser.setStylesheet(new CssParser().parse(sourceViewer.getDocument().get()));
GC gc = new GC(previewViewer.getTextWidget());
try {
parser.setGC(gc);
parser.parse(createPreviewHtml());
} catch (SAXException e) {
throw new IllegalStateException(e);
} catch (IOException e) {
throw new IllegalStateException(e);
} finally {
gc.dispose();
}
htmlViewerConfiguration.setTextPresentation(textPresentation);
previewViewer.changeTextPresentation(textPresentation, true);
}
use of org.eclipse.mylyn.wikitext.parser.css.CssParser in project mylyn.docs by eclipse.
the class CssStyleFieldEditor method doCheckState.
/**
* validate the input
*/
@Override
protected boolean doCheckState() {
String value = getStringValue();
if (value != null && value.trim().length() > 0) {
// here we validate that the value looks like it is composed of valid CSS rules
int offset = 0;
Iterator<CssRule> ruleIterator = new CssParser().createRuleIterator(value);
while (ruleIterator.hasNext()) {
CssRule rule = ruleIterator.next();
// detect gaps between rules. Such gaps are areas of text that weren't detected as a CSS rule.
if (rule.offset > offset) {
String gap = value.substring(offset, rule.offset);
if (gap.trim().length() != 0) {
setErrorMessage(NLS.bind(Messages.CssStyleFieldEditor_unexpectedToken, new Object[] { gap.trim(), offset }));
return false;
}
}
offset = rule.offset + rule.length;
// unknown rules should create an error
if (!cssStyleManager.isKnownRule(rule)) {
StringBuilder recognizedNames = new StringBuilder();
for (String recognizedName : cssStyleManager.getRecognizedRuleNames()) {
if (recognizedNames.length() > 0) {
recognizedNames.append(Messages.CssStyleFieldEditor_1);
}
recognizedNames.append(recognizedName);
}
setErrorMessage(NLS.bind(Messages.CssStyleFieldEditor_unsupportedRule, new Object[] { rule.name, recognizedNames }));
return false;
}
if (CssStyleManager.RULE_COLOR.equals(rule.name) || CssStyleManager.RULE_BACKGROUND_COLOR.equals(rule.name)) {
Integer rgb = CssStyleManager.cssColorRgb(rule.value);
if (rgb == null) {
setErrorMessage(NLS.bind(Messages.CssStyleFieldEditor_invalidColor, new Object[] { rule.value }));
return false;
}
}
}
// detect trailing text that wasn't detected as a CSS rule.
if (offset < value.length() - 1) {
String gap = value.substring(offset, value.length());
if (gap.trim().length() != 0) {
setErrorMessage(NLS.bind(Messages.CssStyleFieldEditor_unexpectedToken, new Object[] { gap.trim(), offset }));
return false;
}
}
}
return super.doCheckState();
}
Aggregations