summaryrefslogtreecommitdiff
path: root/mammoth/style_reader/__init__.py
blob: 41dcffa62047621baf648052e1e9060fe16ca5a1 (plain)
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
from parsimonious.grammar import Grammar

from .html_path_reader import read_html_path, grammar_text as html_path_grammar, read_html_path_node
from .document_matcher_reader import read_document_matcher, grammar_text as document_matcher_grammar, read_document_matcher_node
from ..styles import Style


def read_style(string):
    style_node = _grammar.parse(string)
    return _read_style_node(style_node)


def _read_style_node(style_node):
    document_matcher_node = style_node.children[0]
    html_path_node = style_node.children[4]
    
    document_matcher = read_document_matcher_node(document_matcher_node)
    html_path = read_html_path_node(html_path_node)
    
    return Style(document_matcher, html_path)
    


_grammar = Grammar("""
style = document_matcher whitespace "=>" whitespace html_path
""" + html_path_grammar + document_matcher_grammar
)