5

i'm merging 100's of HTML pages, all with embedded style elements in the head. Using BeautifulSoup to extract the contents of style but now left with the task for parsing the string into a dict {selector_str:properties_str}. Looking at tinycss and it gets me the selector '.c0' easily, but not the property string '{...}'.

Here's an example string

'.c0 { padding: 1px 0px 0px; font-size: 11px } .c1 { margin: 0px; font-size: 11px } .c2 { font-size: 11px } .c3 { font-size: 11px; font-style: italic; font-weight: bold } '

Suggestions? A regex hack welcome. This is the extent of the CSS. Class selectors .c0 to .c100(s) on every page and every page follows same pattern.

4
  • 1
    See pythonhosted.org/tinycss , pypi.python.org/pypi/cssutils , cthedot.de/cssutils , github.com/SimonSapin/tinycss , etc. Surely one of those must be suitable enough. Commented Aug 13, 2014 at 23:03
  • Thank you. I am in fact using the very nice tinycss library already. It gets me the selector easily, and it tokenizes the properties, it doesn't have a method for giving back the property set as a whole string. I might be missing something obvious. Which is one of the reasons I ask. Thanks again for the pointers. Commented Aug 13, 2014 at 23:13
  • Ahh, I thought the goal was just to parse the string. However, given a set of ordered property pairs, it seems like the rule as-a-string could be easily generated by a simple loop (or map transformation). Join each key and value with a ":", each pair with a ";" and wrap everything in "{}". I don't believe that any of these characters are allowed inside CSS properties so there should be no escaping concerns. (Although you might have to watch out how/if the *hack or _hack properties are parsed.) Commented Aug 13, 2014 at 23:18
  • 1
    I don't know if you can parse arbitrary CSS with RE (I think it's context-free), so it may be fraught with peril to try. If perfectly formed CSS is ever "malformed" for your program, that's bad. People have already developed Python CSS parsers anyways (as @user286 points out), so why not use them? Commented Aug 18, 2014 at 20:37

2 Answers 2

3

Something like this?

from collections import defaultdict

properties = defaultdict(str)

for item in example_str.split("}"):
    item_split = item.split("{")
    properties[item_split[0]] = "{" + item_split[1] + "}"
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for the nudge. I did go with a split solution. Split is tricky because you have to account for both sides of the split and that can mean an extra element in the list, as yours did at at the end. Mine comes at the beginning. I've not used defaultdict before and I've worked that into the solution now too. Thank you for that insight too.
0

Here's where i landed. Used BadKarma's strategy of cracking the string with a split.

from bs4 import BeautifulSoup
import re

class RichText(BeautifulSoup):
    """
    subclass BeautifulSoup
    add behavior for generating selectors and declaration_blocks from <style>
    """

    def __init__(self, html_page):
        super().__init__(html_page)

    @property
    def rules_as_str(self):
        return str(self.style.string)

    def rules(self):
        split_rules = re.split('(\.c[0-9]*)', self.rules_as_str)
        # side effect of split, first element is null
        assert(split_rules[0] == '')
        # enforce that it MUST be null, then pass over it
        for i in range(1, len(split_rules), 2):
            yield (split_rules[i].strip(), split_rules[i+1].strip())


if __name__ == '__main__':

    with open('rich-text.html', 'r') as f:
        html_file = f.read()

    rich_text = RichText(html_file)
    for selector, declaration_block in rich_text.rules():
        print(selector)
        print(declaration_block)

>>> with open("test.py") as f:
...     code = compile(f.read(), "test.py", 'exec')
...     exec(code)
... 
.c0
{ padding: 1px 0px 0px; font-size: 11px }
.c1
{ margin: 0px; font-size: 11px }
.c2
{ font-size: 11px }
.c3
{ font-size: 11px; font-style: italic; font-weight: bold }
>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.