Code Monkey home page Code Monkey logo

cssparser's Introduction

CSSParser - A Simple C++ CSS Parser ===================================

CSSParser is a free, open source, multi-platform simpole C++ based CSS Parser that only uses the std:: library.

It's code was initially extracted from CSSTidy and then reworked.

CSSTidy (https://github.com/csstidy-c/csstidy)

CSSTidy Portions Copyright:
   * Florian Schmitz <[email protected]>
   * Thierry Charbonnel
   * Will Mitchell <[email protected]>
   * Brett Zamir <[email protected]>
   * sined_ <[email protected]>
   * Dmitry Leskov <[email protected]>
   * Kevin Coyner <[email protected]>
   * Tuukka Pasanen <[email protected]>
   * Frank W. Bergmann <[email protected]>
   * Frank Dana <[email protected]>

   The CSSTidy codebase is itself is Available under the LGPL 2.1

Interface

The best way to understand the parser interface is to examine main.cpp which is excerpted below:

[CODE SNIPPETS]

    CSSParser csst;

    css_file = CSSUtils::file_get_contents(argv[1]);

    // valid css levels are "CSS1.0", "CSS2.0", "CSS2.1", "CSS3.0" 
    csst.set_level("CSS3.0");

    // do the actual parsing
    csst.parse_css(css_file);

    // check for any parse errors
    std::vector<std::string> errors = csst.get_parse_errors();

    // check for any parse warnings
    std::vector<std::string> warnings = csst.get_parse_warnings();

    // check for any parse information messages
    std::vector<std::string> infos = csst.get_parse_info();

    // get any @charset without having to walk the csstokens list
    std::string cset = csst.get_charset();

    // get all @import without having to walk the csstokens list
    std::vector<std::string> imports = csst.get_import();

    // get any @namespace without having to walk the csstokens list
    std::string ns = csst.get_namespace();

    // enum token_type
    // {
    //    CHARSET, IMPORT, NAMESP, AT_START, AT_END, SEL_START, SEL_END, PROPERTY, VALUE, COMMENT, CSS_END
    // };

    // struct token
    // {
    //    token_type type;
    //    int pos; // position in input string
    //    int line; // line number in input string
    //    std::string data;
    // };

    // now walk the sequence of parsed tokens
    // if you know the location of the token you want in the sequence (starting with 0)
    // simply pass start_ptr in get_next_token set to a valid starting point in the token sequence

    CSSParser::token atoken = csst.get_next_token();

    while(atoken.type != CSSParser::CSS_END) {

        std::string ttype = csst.get_type_name(atoken.type);

        std::cout << "Pos: " << atoken.pos
                  << " Line: " << atoken.line
                  << " Type: " << ttype
                  <<"  Data: " << atoken.data << std::endl;

        atoken = csst.get_next_token();
    }

    // serialize back to CSS
    std::string cssout;
    cssout = csst.serialize_css();

[/CODE SNIPPETS]

Build and Install

There is a very simplistic Linux/MacOS Makefile and a Windows Makefile using nmake

License

Like the CSSTidy codebase it was derived from, CSSParser is licensed under the LGPLv2.1 or later. The complete license is located in LICENSE.

cssparser's People

Contributors

kevinhendricks avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.