Search in sources :

Example 1 with CssParser

use of org.eclipse.mylyn.wikitext.parser.css.CssParser in project mylyn.docs by eclipse.

the class CssParserTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    parser = new CssParser();
}
Also used : CssParser(org.eclipse.mylyn.wikitext.parser.css.CssParser)

Example 2 with CssParser

use of org.eclipse.mylyn.wikitext.parser.css.CssParser in project mylyn.docs by eclipse.

the class RepairBrokenCSSColorStylesProcessor method process.

@Override
public void process(Document document) {
    Element body = document.body();
    CssParser cssParser = new CssParser();
    for (Element element : Selector.select("[style]", body)) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        String style = element.attr("style");
        // $NON-NLS-1$
        String newStyle = "";
        List<CssRule> rules = null;
        CssRule newRule = null;
        if (style != null && style.length() > 0) {
            rules = cssParser.parseBlockContent(style);
            Iterator<CssRule> ruleIt = rules.iterator();
            while (ruleIt.hasNext()) {
                CssRule rule = ruleIt.next();
                if ("color".equals(rule.name)) {
                    // $NON-NLS-1$
                    String color = rule.value;
                    // no 3- or 6-character CSS color names are written in hex characters
                    Matcher invalidHexColorMatcher = Pattern.compile(// $NON-NLS-1$
                    "^\\s*([0-9a-fA-F]{6}|[0-9a-fA-F]{3})(?:\\s+(.+))?\\s*$").matcher(color);
                    if (invalidHexColorMatcher.matches()) {
                        // $NON-NLS-1$
                        String newColor = "#" + invalidHexColorMatcher.group(1);
                        String additionalDeclarations = invalidHexColorMatcher.group(2);
                        if (additionalDeclarations != null) {
                            // $NON-NLS-1$
                            newColor += " " + additionalDeclarations;
                        }
                        ruleIt.remove();
                        // $NON-NLS-1$
                        newRule = new CssRule("color", newColor.trim(), 0, 0, 0, 0);
                    }
                }
            }
        }
        if (rules != null && newRule != null) {
            newStyle = addRuleToStyle(newStyle, newRule);
            for (CssRule rule : rules) {
                newStyle = addRuleToStyle(newStyle, rule);
            }
            // $NON-NLS-1$
            element.attr("style", newStyle);
        }
    }
}
Also used : CssRule(org.eclipse.mylyn.wikitext.parser.css.CssRule) Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) CssParser(org.eclipse.mylyn.wikitext.parser.css.CssParser)

Example 3 with CssParser

use of org.eclipse.mylyn.wikitext.parser.css.CssParser in project mylyn.docs by eclipse.

the class DocBookDocumentBuilder method emitImage.

private void emitImage(Attributes attributes, String url, boolean inline) {
    // see http://www.docbook.org/tdg/en/html/imagedata-x.html
    ensureBlockElementsOpen();
    // $NON-NLS-1$ //$NON-NLS-2$
    writer.writeStartElement(inline ? "inlinemediaobject" : "mediaobject");
    applyAttributes(attributes);
    // $NON-NLS-1$
    writer.writeStartElement("imageobject");
    // $NON-NLS-1$
    writer.writeEmptyElement("imagedata");
    // $NON-NLS-1$
    writer.writeAttribute("fileref", makeUrlAbsolute(url));
    String cssStyle = attributes.getCssStyle();
    if (cssStyle != null) {
        String width = null;
        String depth = null;
        Iterator<CssRule> ruleIterator = new CssParser().createRuleIterator(cssStyle);
        while (ruleIterator.hasNext()) {
            CssRule rule = ruleIterator.next();
            if ("width".equals(rule.name)) {
                // $NON-NLS-1$
                width = rule.value;
            } else if ("height".equals(rule.name)) {
                // $NON-NLS-1$
                depth = rule.value;
            }
        }
        if (width != null) {
            Matcher matcher = PERCENTAGE.matcher(width);
            if (matcher.matches()) {
                // $NON-NLS-1$
                writer.writeAttribute("scale", matcher.group(1));
            } else {
                // $NON-NLS-1$
                writer.writeAttribute("width", width);
                if (depth != null) {
                    // $NON-NLS-1$
                    writer.writeAttribute("depth", depth);
                }
            }
        }
    }
    // imageobject
    writer.writeEndElement();
    // inlinemediaobject or mediaobject
    writer.writeEndElement();
}
Also used : CssRule(org.eclipse.mylyn.wikitext.parser.css.CssRule) Matcher(java.util.regex.Matcher) CssParser(org.eclipse.mylyn.wikitext.parser.css.CssParser)

Example 4 with CssParser

use of org.eclipse.mylyn.wikitext.parser.css.CssParser in project mylyn.docs by eclipse.

the class RemoveExcessiveStylesProcessor method process.

@Override
public void process(Document document) {
    Element body = document.body();
    CssParser cssParser = new CssParser();
    for (Element element : Selector.select("[style], font, span", body)) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        String style = element.attr("style");
        // $NON-NLS-1$
        String newStyle = "";
        List<CssRule> rules = null;
        if (style != null && style.length() > 0) {
            rules = cssParser.parseBlockContent(style);
            Iterator<CssRule> ruleIt = rules.iterator();
            while (ruleIt.hasNext()) {
                CssRule rule = ruleIt.next();
                if ("color".equals(rule.name)) {
                    // $NON-NLS-1$
                    if (!(rule.value.equalsIgnoreCase("black") || rule.value.equals("#010101"))) {
                        // $NON-NLS-1$//$NON-NLS-2$
                        continue;
                    }
                } else if ("font-weight".equals(rule.name)) {
                    // $NON-NLS-1$
                    if (rule.value.equalsIgnoreCase("bold") || rule.value.equalsIgnoreCase("bolder")) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        continue;
                    }
                } else if ("font-style".equals(rule.name)) {
                    // $NON-NLS-1$
                    if (rule.value.equalsIgnoreCase("bold") || rule.value.equalsIgnoreCase("italic")) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        continue;
                    }
                } else if ("text-decoration".equals(rule.name)) {
                    // $NON-NLS-1$
                    if (rule.value.equalsIgnoreCase("underline") || rule.value.equalsIgnoreCase("line-through")) {
                        // $NON-NLS-1$ //$NON-NLS-2$
                        continue;
                    }
                }
                ruleIt.remove();
            }
        }
        if ("font".equalsIgnoreCase(element.nodeName())) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            String color = element.attr("color");
            if (color != null && color.trim().length() > 0) {
                if (rules == null) {
                    rules = new ArrayList<CssRule>(1);
                }
                // $NON-NLS-1$
                rules.add(new CssRule("color", color.trim(), 0, 0, 0, 0));
            }
        }
        if (rules != null) {
            for (CssRule rule : rules) {
                // $NON-NLS-1$ //$NON-NLS-2$
                newStyle += rule.name + ": " + rule.value + ";";
            }
        }
        if (newStyle.length() > 0) {
            if ("font".equalsIgnoreCase(element.nodeName())) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                Element spanElement = document.createElement("span");
                for (Node child : new ArrayList<Node>(element.childNodes())) {
                    child.remove();
                    spanElement.appendChild(child);
                }
                element.before(spanElement);
                element.remove();
                element = spanElement;
            }
            // $NON-NLS-1$
            element.attr("style", newStyle);
        } else {
            // $NON-NLS-1$
            element.removeAttr("style");
            if (// $NON-NLS-1$//$NON-NLS-2$
            ("span".equalsIgnoreCase(element.nodeName()) && (element.attr("class").trim().isEmpty())) || "font".equalsIgnoreCase(element.nodeName())) {
                // $NON-NLS-1$
                removeElementPreserveChildren(element);
            }
        }
    }
}
Also used : CssRule(org.eclipse.mylyn.wikitext.parser.css.CssRule) Element(org.jsoup.nodes.Element) Node(org.jsoup.nodes.Node) ArrayList(java.util.ArrayList) CssParser(org.eclipse.mylyn.wikitext.parser.css.CssParser)

Example 5 with CssParser

use of org.eclipse.mylyn.wikitext.parser.css.CssParser in project mylyn.docs by eclipse.

the class XslfoDocumentBuilder method attributesFromCssStyles.

private Map<String, String> attributesFromCssStyles(String styles) {
    if (styles == null) {
        return Collections.emptyMap();
    }
    List<CssRule> rules = new CssParser().parseBlockContent(styles);
    if (rules.isEmpty()) {
        return Collections.emptyMap();
    }
    Map<String, String> mapping = new HashMap<String, String>();
    for (CssRule rule : rules) {
        if (CSS_RULE_VERTICAL_ALIGN.equals(rule.name)) {
            String cssValue = rule.value;
            if (cssValue.equals("super")) {
                // $NON-NLS-1$
                // $NON-NLS-1$ //$NON-NLS-2$
                mapping.put("font-size", "75%");
                // $NON-NLS-1$ //$NON-NLS-2$
                mapping.put("baseline-shift", "super");
            } else if (cssValue.equals("sub")) {
                // $NON-NLS-1$
                // $NON-NLS-1$ //$NON-NLS-2$
                mapping.put("font-size", "75%");
                // $NON-NLS-1$ //$NON-NLS-2$
                mapping.put("baseline-shift", "sub");
            } else if (cssValue.equals("top")) {
                // $NON-NLS-1$
                // $NON-NLS-1$ //$NON-NLS-2$
                mapping.put("display-align", "before");
            } else if (cssValue.equals("middle")) {
                // $NON-NLS-1$
                // $NON-NLS-1$ //$NON-NLS-2$
                mapping.put("display-align", "center");
            } else if (cssValue.equals("bottom")) {
                // $NON-NLS-1$
                // $NON-NLS-1$ //$NON-NLS-2$
                mapping.put("display-align", "after");
            }
        } else if (// 
        CSS_RULE_TEXT_DECORATION.equals(rule.name) || // 
        CSS_RULE_FONT_FAMILY.equals(rule.name) || // 
        CSS_RULE_FONT_SIZE.equals(rule.name) || // 
        CSS_RULE_FONT_WEIGHT.equals(rule.name) || // 
        CSS_RULE_FONT_STYLE.equals(rule.name) || // 
        CSS_RULE_BACKGROUND_COLOR.equals(rule.name) || CSS_RULE_COLOR.equals(rule.name)) {
            mapping.put(rule.name, rule.value);
        } else if (CSS_RULE_BORDER_STYLE.equals(rule.name)) {
            mapping.put(rule.name, rule.value);
        } else if (CSS_RULE_BORDER_WIDTH.equals(rule.name)) {
            mapping.put(rule.name, rule.value);
        } else if (CSS_RULE_BORDER_COLOR.equals(rule.name)) {
            mapping.put(rule.name, rule.value);
        } else if (CSS_RULE_TEXT_ALIGN.equals(rule.name)) {
            mapping.put(rule.name, rule.value);
        }
    }
    return mapping;
}
Also used : CssRule(org.eclipse.mylyn.wikitext.parser.css.CssRule) HashMap(java.util.HashMap) 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