File: parsing.rst

package info (click to toggle)
python-tinycss 0.4-8
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 716 kB
  • sloc: python: 2,476; makefile: 7
file content (97 lines) | stat: -rw-r--r-- 2,499 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
Parsing with tinycss
====================

.. highlight:: python

Quickstart
----------

Import *tinycss*, make a parser object with the features you want,
and parse a stylesheet:

.. doctest::

    >>> import tinycss
    >>> parser = tinycss.make_parser('page3')
    >>> stylesheet = parser.parse_stylesheet_bytes(b'''@import "foo.css";
    ...     p.error { color: red }  @lorem-ipsum;
    ...     @page tables { size: landscape }''')
    >>> stylesheet.rules
    [<ImportRule 1:1 foo.css>, <RuleSet at 2:5 p.error>, <PageRule 3:5 ('tables', None)>]
    >>> stylesheet.errors
    [ParseError('Parse error at 2:29, unknown at-rule in stylesheet context: @lorem-ipsum',)]

You’ll get a :class:`~tinycss.css21.Stylesheet` object which contains
all the parsed content as well as a list of encountered errors.


Parsers
-------

Parsers are subclasses of :class:`tinycss.css21.CSS21Parser`. Various
subclasses add support for more syntax. You can choose which features to
enable by making a new parser class with multiple inheritance, but there
is also a convenience function to do that:

.. module:: tinycss

.. autofunction:: make_parser


.. module:: tinycss.css21
.. _parsing:

Parsing a stylesheet
~~~~~~~~~~~~~~~~~~~~

Parser classes have three different methods to parse CSS stylesheet,
depending on whether you have a file, a byte string, or an Unicode string.

.. autoclass:: CSS21Parser
    :members: parse_stylesheet_file, parse_stylesheet_bytes, parse_stylesheet


Parsing a ``style`` attribute
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. automethod:: CSS21Parser.parse_style_attr


Parsed objects
--------------

These data structures make up the results of the various parsing methods.

.. autoclass:: tinycss.parsing.ParseError()
.. autoclass:: Stylesheet()

.. note::
    All subsequent objects have :obj:`line` and :obj:`column` attributes (not
    repeated every time fore brevity) that indicate where in the CSS source
    this object was read.

.. autoclass:: RuleSet()
.. autoclass:: ImportRule()
.. autoclass:: MediaRule()
.. autoclass:: PageRule()
.. autoclass:: Declaration()


Tokens
------

Some parts of a stylesheet (such as selectors in CSS 2.1 or property values)
are not parsed by tinycss. They appear as tokens instead.

.. module:: tinycss.token_data

.. autoclass:: TokenList()
    :member-order: bysource
    :members:
.. autoclass:: Token()
    :members:
.. autoclass:: tinycss.speedups.CToken()
.. autoclass:: ContainerToken()
    :members:

.. autoclass:: FunctionToken()