Search in sources :

Example 6 with CssParser

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);
}
Also used : CssRule(org.eclipse.mylyn.wikitext.parser.css.CssRule) ArrayList(java.util.ArrayList) CssParser(org.eclipse.mylyn.wikitext.parser.css.CssParser)

Example 7 with CssParser

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);
}
Also used : HtmlTextPresentationParser(org.eclipse.mylyn.internal.wikitext.ui.viewer.HtmlTextPresentationParser) IOException(java.io.IOException) GC(org.eclipse.swt.graphics.GC) TextPresentation(org.eclipse.jface.text.TextPresentation) CssParser(org.eclipse.mylyn.wikitext.parser.css.CssParser) SAXException(org.xml.sax.SAXException)

Example 8 with CssParser

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();
}
Also used : CssRule(org.eclipse.mylyn.wikitext.parser.css.CssRule) CssParser(org.eclipse.mylyn.wikitext.parser.css.CssParser)

Aggregations

CssParser (org.eclipse.mylyn.wikitext.parser.css.CssParser)8 CssRule (org.eclipse.mylyn.wikitext.parser.css.CssRule)6 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 Element (org.jsoup.nodes.Element)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 TextPresentation (org.eclipse.jface.text.TextPresentation)1 HtmlTextPresentationParser (org.eclipse.mylyn.internal.wikitext.ui.viewer.HtmlTextPresentationParser)1 GC (org.eclipse.swt.graphics.GC)1 Node (org.jsoup.nodes.Node)1 SAXException (org.xml.sax.SAXException)1