<commit_before>#!/usr/bin/env python
"""\
Usage: %prog [options] RULES THEME

  THEME is an html file.
  RULES is a file defining a set of xdv rules in css syntax, e.g:
    <rules xmlns="http://namespaces.plone.org/xdv">
        <copy content="//div[@id='content-wrapper']"
              theme="//div[@id='page-content']"/>
    </rules>\
"""
usage = __doc__

import re
import os.path
import sys
import logging
from lxml import etree
from optparse import OptionParser

from utils import namespaces
from cssrules import convert_css_selectors

logger = logging.getLogger('xdv')

HERE = os.path.dirname(__file__)

IMPORT_STYLESHEET = re.compile(r'''(@import\s+(?:url\(['"]?|['"]))(.+)(['"]?\)|['"])''', re.IGNORECASE)

COMPILER_PATH = os.path.join(HERE, 'compiler.xsl')

UPDATE_PATH = os.path.join(HERE, 'update-namespace.xsl')
update_transform = etree.XSLT(etree.parse(UPDATE_PATH))

def update_namespace(rules):
    """Convert old namespace to new namespace in place
    """
    if rules.xpath("//*[namespace-uri()='%s']" % namespaces['old']):
        logger.warning('The %s namespace is deprecated, use %s instead.' % (namespaces['old'], namespaces['xdv']))
        return update_transform(rules)
    else:
        return rules

class CompileResolver(etree.Resolver):
    def __init__(self, rules, extra=None):
        self.rules = rules
        self.extra = extra
        
    def resolve(self, url, pubid, context):
        if url == 'xdv:rules':
            return self.resolve_string(self.rules, context)
        if url == 'xdv:extra' and self.extra is not None:
            return self.resolve_string(self.extra, context)

def to_absolute(path, prefix):
    """Make a url/path into an absolute URL by applying the given prefix
    """
    # Absolute path or full url
    if path.startswith('/') or '://' in path:
        return path
    
    absolute = "%s/%s" % (prefix, path)
    if '://' in absolute:
        return absolute
    return os.path.normpath(absolute)

def apply_absolute_prefix(theme_doc, absolute_prefix):
    if absolute_prefix.endswith('/'):
        absolute_prefix = absolute_prefix[:-1]
    for node in theme_doc.xpath('*//style | *//script | *//img | *//link | *//input | *//comment() '):
        if node.tag in ('img', 'script', 'input',):
            src = node.get('src')<commit_msg>Fix potential Windows path problem<commit_after>#!/usr/bin/env python
"""\
Usage: %prog [options] RULES THEME

  THEME is an html file.
  RULES is a file defining a set of xdv rules in css syntax, e.g:
    <rules xmlns="http://namespaces.plone.org/xdv">
        <copy content="//div[@id='content-wrapper']"
              theme="//div[@id='page-content']"/>
    </rules>\
"""
usage = __doc__

import re
import os.path
import sys
import logging
from lxml import etree
from optparse import OptionParser

from utils import namespaces
from cssrules import convert_css_selectors

logger = logging.getLogger('xdv')

HERE = os.path.dirname(__file__)

IMPORT_STYLESHEET = re.compile(r'''(@import\s+(?:url\(['"]?|['"]))(.+)(['"]?\)|['"])''', re.IGNORECASE)

COMPILER_PATH = os.path.join(HERE, 'compiler.xsl')

UPDATE_PATH = os.path.join(HERE, 'update-namespace.xsl')
update_transform = etree.XSLT(etree.parse(UPDATE_PATH))

def update_namespace(rules):
    """Convert old namespace to new namespace in place
    """
    if rules.xpath("//*[namespace-uri()='%s']" % namespaces['old']):
        logger.warning('The %s namespace is deprecated, use %s instead.' % (namespaces['old'], namespaces['xdv']))
        return update_transform(rules)
    else:
        return rules

class CompileResolver(etree.Resolver):
    def __init__(self, rules, extra=None):
        self.rules = rules
        self.extra = extra
        
    def resolve(self, url, pubid, context):
        if url == 'xdv:rules':
            return self.resolve_string(self.rules, context)
        if url == 'xdv:extra' and self.extra is not None:
            return self.resolve_string(self.extra, context)

def to_absolute(path, prefix):
    """Make a url/path into an absolute URL by applying the given prefix
    """
    # Absolute path or full url
    if path.startswith('/') or '://' in path:
        return path
    
    absolute = "%s/%s" % (prefix, path)
    if '://' in absolute:
        return absolute
        
    normalized = os.path.normpath(absolute)
    if os.path.sep != '/':
        normalized = normalized.replace(os.path.sep, '/')
    return normalized    

def apply_absolute_prefix(theme_doc, absolute_prefix):
    if absolute_prefix.endswith('/'):
        absolute_prefix = absolute_prefix[:-1]
    for node in theme_doc.xpath('*//style | *//script | *//img | *//link | *//input | *//comment() '):
        if node.tag in ('img', 'script', 'input',):
            src = node.get('src')
{'fraction_non_alphanumeric': 0.1566366114489411, 'fraction_numerical': 0.0004193751310547285, 'mean_word_length': 3.1478260869565218, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7348829', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1517, 'n_words': 399}