如何在我的Qt应用程序中简单地解析类似(!)文件的CSS?

时间:2015-07-23 09:40:47

标签: css qt html-parsing qt5 qt5.4

我有一个* .css(层叠样式表)格式的文档,但它有自己的关键字。实际上它是一个个性化的CSS(我称之为* .pss),具有自己的标签和属性。我在这里摘录:

/* CSS like style sheet file *.pss */

@include "otherStyleSheet.pss";

/* comment */
[propertyID="1230000"] { 
  fillColor : #f3f1ed;
  minSize : 5;
  lineWidth : 3;
}

/* sphere */
[propertyID="124???|123000"] { 
  lineType : dotted;
}

/* square */
[propertyID="125???"] {
  lineType : thinline;    
}

/* ring */
[propertyID="133???"] {
  lineType : thickline; 
  [hasInnerRing=true] {
    innerLineType : thinline;
  }  
}

我想非常轻松地解析它,Qt已经有了一些可以使用的东西吗?什么是最简单的方法?

由于* .css有自己的关键字,我不会在CSS解析器中使用。

解析* .pss后我的进一步意图是将其属性存储在Model结构中。

2 个答案:

答案 0 :(得分:10)

Qt中没有任何公开内容。您当然可以自由使用Qt的私有CSS解析器 - 您可以复制并修改以满足您的需求。

请参阅qtbase/src/gui/text/qcssparser_p.h中的qtbase/src/gui/text

好消息是,对于上面显示的示例,修改将非常小。 Qt的CSS解析器已经支持@import,所以我们只有nested selector syntax的语法。如果没有该语法,您可以按原样使用QCss::Parser。解析器是以灵活的方式编写的,您无需担心正式的CSS关键字:它仍然允许您访问所有声明,无论它们是否从正式的CSS观点来看都是有意义的。

迭代解析树非常简单:

int main() {
   QCss::Parser parser(pss);
   QCss::StyleSheet styleSheet;
   if (!parser.parse(&styleSheet))
      return 1;
   for (auto rule : styleSheet.styleRules) {
      qDebug() << "** Rule **";
      for (auto sel : rule.selectors) {
        for (auto bSel : sel.basicSelectors)
           qDebug() << bSel;
      }
      for (auto decl : rule.declarations)
         qDebug() << decl;
   }
}

输出是我们所期望的:

** Rule **
BasicSelector "propertyID"="1230000"
Declaration "fillColor" = '#f3f1ed' % QColor(ARGB 1, 0.952941, 0.945098, 0.929412)
Declaration "minSize" = '5' % 5
Declaration "lineWidth" = '3'
** Rule **
BasicSelector "propertyID"="124???|123000"
Declaration "lineType" = 'dotted'
** Rule **
BasicSelector "propertyID"="125???"
Declaration "lineType" = 'thinline'
** Rule **
BasicSelector "propertyID"="133???"
Declaration "lineType" = 'thickline'

我们必须自己为QCss类实现调试流运算符:

QDebug operator<<(QDebug dbg, const QCss::AttributeSelector & sel) {
   QDebugStateSaver saver(dbg);
   dbg.noquote().nospace() << "\"" << sel.name << "\"";
   switch (sel.valueMatchCriterium) {
   case QCss::AttributeSelector::MatchEqual:
      dbg << "="; break;
   case QCss::AttributeSelector::MatchContains:
      dbg << "~="; break;
   case QCss::AttributeSelector::MatchBeginsWith:
      dbg << "^="; break;
   case QCss::AttributeSelector::NoMatch:
      break;
   }
   if (sel.valueMatchCriterium != QCss::AttributeSelector::NoMatch && !sel.value.isEmpty())
      dbg << "\"" << sel.value << "\"";
   return dbg;
}

QDebug operator<<(QDebug dbg, const QCss::BasicSelector & sel) {
   QDebugStateSaver saver(dbg);
   dbg.noquote().nospace() << "BasicSelector";
   if (!sel.elementName.isEmpty())
      dbg << " #" << sel.elementName;
   for (auto & id : sel.ids)
      dbg << " id:" << id;
   for (auto & aSel : sel.attributeSelectors)
      dbg << " " << aSel;
   return dbg;
}

在遍历声明时,QCss::parser已经为我们解释了一些标准值,例如:颜色,整数等。

QDebug operator<<(QDebug dbg, const QCss::Declaration & decl) {
   QDebugStateSaver saver(dbg);
   dbg.noquote().nospace() << "Declaration";
   dbg << " \"" << decl.d->property << "\" = ";
   bool first = true;
   for (auto value : decl.d->values) {
      if (!first) dbg << ", ";
      dbg << "\'" << value.toString() << "\'";
      first = false;
   }
   if (decl.d->property == "fillColor")
      dbg << " % " << decl.colorValue();
   else if (decl.d->property == "minSize") {
      int i;
      if (decl.intValue(&i)) dbg << " % " << i;
   }
   return dbg;
}

最后,样板和要解析的样式表:

// https://github.com/KubaO/stackoverflown/tree/master/questions/css-like-parser-31583622
#include <QtGui>
#include <private/qcssparser_p.h>

const char pss[] =
  "/* @include \"otherStyleSheet.pss\"; */ \
  [propertyID=\"1230000\"] {  \
    fillColor : #f3f1ed; \
    minSize : 5; \
    lineWidth : 3; \
  } \
   \
  /* sphere */ \
  [propertyID=\"124???|123000\"] {  \
    lineType : dotted; \
  } \
   \
  /* square */ \
  [propertyID=\"125???\"] { \
    lineType : thinline; \
  } \
   \
  /* ring */ \
  [propertyID=\"133???\"] { \
    lineType : thickline;  \
    /*[hasInnerRing=true] { \
      innerLineType : thinline; \
    }*/   \
  }";

可以通过修改解析器源来实现对嵌套选择器/规则的支持。使Parser::parseRuleset递归所需的更改非常小。我将把这作为读者的练习:)

总而言之,我认为重用现有的解析器比滚动自己的解析器容易得多,尤其是当您的用户不可避免地希望您支持越来越多的CSS规范时。

答案 1 :(得分:1)

好吧,我猜你不想做编写Object解析器的事情,你只需要重新发明JSON或YAML等。因此,最好的办法是使格式符合已知的配置或对象表示法语言,然后使用某些库解析它所使用的语言。通过非常小的修改,您在上面描述的格式可能会成为HOCON,这是一个非常好的JSON超集,并且语法更接近您所使用的语法:

https://github.com/typesafehub/config/blob/master/HOCON.md

然后您可以使用HOCON解析库解析它,瞧,您可以拥有内存中的对象,您可以按照自己喜欢的方式建模或存储。我相信Qt是基于C ++的吗?有一个C的hocon库,我不了解C ++,我猜你需要编写一个Qt插件来包装HOCON解析来自其他语言。

另一种选择是使用像这样的CSS-&gt;对象解析器: https://github.com/reworkcss/css

您可能需要根据需要进行分叉和修改。无论哪种方式,我猜测要集成到Qt应用程序中,您将需要一个插件来处理命令行进程或其他代码模块的调用。

相关问题