# Path: mdn/utils.py
# def is_new_id(_id):
#     """Detect if an ID signifies a new resource.
# 
#     New resource IDs are text strings prefixed with an underscore.
#     """
#     return isinstance(_id, text_type) and _id[0] == '_'
# 
# def normalize_name(name):
#     """Normalize a name for IDs, slugs."""
#     to_remove = ('<code>', '</code>', '&lt;', '&gt;')
#     normalized_name = name.lower()
#     for removal in to_remove:
#         normalized_name = normalized_name.replace(removal, '')
#     return normalized_name
# 
# def slugify(word, length=50, suffix=''):
#     """Create a slugged version of a word or phrase."""
#     raw = word.lower()
#     out = []
#     acceptable = string.ascii_lowercase + string.digits + '_-'
#     for c in raw:
#         if c in acceptable:
#             out.append(c)
#         else:
#             out.append('_')
#     slugged = ''.join(out)
#     while '__' in slugged:
#         slugged = slugged.replace('__', '_')
#     if slugged.endswith('_') and len(slugged) > 1:
#         slugged = slugged[:-1]
#     suffix = text_type(suffix) if suffix else ''
#     with_suffix = slugged[slice(length - len(suffix))] + suffix
#     return with_suffix


from collections import namedtuple
from webplatformcompat.models import (
    Browser, Feature, Section, Specification, Support, Version)
from .utils import is_new_id, normalize_name, slugify
        # Load existing browser data
        if self.browser_data is None:
            self.browser_data = {}
            for browser in Browser.objects.all():
                key = browser.name[locale]
                self.browser_data[key] = self.BrowserParams(
                    browser, browser.pk, key, browser.slug)

        # Expand to full name, handle common alternate names
        full_name = self.browser_name_fixes.get(name, name)

        # Select the Browser ID and slug
        if full_name not in self.browser_data:
            browser_id = '_' + full_name
            # TODO: unique slugify instead of browser_id
            self.browser_data[full_name] = self.BrowserParams(
                None, browser_id, full_name, browser_id)
        return self.browser_data[full_name]

    FeatureParams = namedtuple(
        'FeatureParams', ['feature', 'feature_id', 'slug'])

    def lookup_feature_params(self, parent_feature, name):
        """Get or create the feature ID and slug given a name.

        Return is a named tuple:
        * feature - A Feature if found, None if no existing feature
        * feature_id - The feature ID, prefixed with an underscore if new
        * slug - A unique slug for this feature
        """

# Path: mdn/utils.py
# def is_new_id(_id):
#     """Detect if an ID signifies a new resource.
# 
#     New resource IDs are text strings prefixed with an underscore.
#     """
#     return isinstance(_id, text_type) and _id[0] == '_'
# 
# def normalize_name(name):
#     """Normalize a name for IDs, slugs."""
#     to_remove = ('<code>', '</code>', '&lt;', '&gt;')
#     normalized_name = name.lower()
#     for removal in to_remove:
#         normalized_name = normalized_name.replace(removal, '')
#     return normalized_name
# 
# def slugify(word, length=50, suffix=''):
#     """Create a slugged version of a word or phrase."""
#     raw = word.lower()
#     out = []
#     acceptable = string.ascii_lowercase + string.digits + '_-'
#     for c in raw:
#         if c in acceptable:
#             out.append(c)
#         else:
#             out.append('_')
#     slugged = ''.join(out)
#     while '__' in slugged:
#         slugged = slugged.replace('__', '_')
#     if slugged.endswith('_') and len(slugged) > 1:
#         slugged = slugged[:-1]
#     suffix = text_type(suffix) if suffix else ''
#     with_suffix = slugged[slice(length - len(suffix))] + suffix
#     return with_suffix


# Path: mdn/data.py
from collections import namedtuple
from webplatformcompat.models import (
    Browser, Feature, Section, Specification, Support, Version)
from .utils import is_new_id, normalize_name, slugify

        # Load existing browser data
        if self.browser_data is None:
            self.browser_data = {}
            for browser in Browser.objects.all():
                key = browser.name[locale]
                self.browser_data[key] = self.BrowserParams(
                    browser, browser.pk, key, browser.slug)

        # Expand to full name, handle common alternate names
        full_name = self.browser_name_fixes.get(name, name)

        # Select the Browser ID and slug
        if full_name not in self.browser_data:
            browser_id = '_' + full_name
            # TODO: unique slugify instead of browser_id
            self.browser_data[full_name] = self.BrowserParams(
                None, browser_id, full_name, browser_id)
        return self.browser_data[full_name]

    FeatureParams = namedtuple(
        'FeatureParams', ['feature', 'feature_id', 'slug'])

    def lookup_feature_params(self, parent_feature, name):
        """Get or create the feature ID and slug given a name.

        Return is a named tuple:
        * feature - A Feature if found, None if no existing feature
        * feature_id - The feature ID, prefixed with an underscore if new
        * slug - A unique slug for this feature
        """

        nname = normalize_name(name)