Пример #1
0
    def _get_page(self, path):
        """Construct a page and return its contents.

        Parameters
        ----------
        path: str
            The path of the page we want to construct.

        Returns
        -------
        (page_name, page_contents): (str, str)

        """
        path = path.strip('/')
        if path == '':
            locale, page = self.source.read_config().get(
                'general', 'defaultlocale'), ''
        elif '/' in path:
            locale, page = path.split('/', 1)
        else:
            locale, page = path, ''

        default_page = self.source.read_config().get('general', 'defaultpage')
        possible_pages = [page, '/'.join([page, default_page]).lstrip('/')]

        for page_format in converters.iterkeys():
            for p in possible_pages:
                if self.source.has_page(p, page_format):
                    return p, process_page(self.source, locale, p, page_format,
                                           self.full_url)

        if self.source.has_localizable_file(locale, page):
            return page, self.source.read_localizable_file(locale, page)

        return None, None
Пример #2
0
 def has_page(self, page, format=None):
     if format is None:
         from cms.converters import converters
         return any(
             self.has_page(page, format)
             for format in converters.iterkeys())
     else:
         return self.has_file(self.page_filename(page, format))
Пример #3
0
def get_page(path):
    path = path.strip('/')
    if path == '':
        path = source.read_config().get('general', 'defaultlocale')
    if '/' in path:
        locale, page = path.split('/', 1)
    else:
        locale, page = path, ''

    default_page = source.read_config().get('general', 'defaultpage')
    alternative_page = '/'.join([page, default_page]).lstrip('/')

    for format in converters.iterkeys():
        for p in (page, alternative_page):
            if source.has_page(p, format):
                return (p, process_page(source, locale, p, format, 'http://%s:%d' % (address, port)))
    if source.has_localizable_file(locale, page):
        return (page, source.read_localizable_file(locale, page))

    return (None, None)
Пример #4
0
def get_page(path):
    path = path.strip('/')
    if path == '':
        path = source.read_config().get('general', 'defaultlocale')
    if '/' in path:
        locale, page = path.split('/', 1)
    else:
        locale, page = path, ''

    default_page = source.read_config().get('general', 'defaultpage')
    alternative_page = '/'.join([page, default_page]).lstrip('/')

    for format in converters.iterkeys():
        for p in (page, alternative_page):
            if source.has_page(p, format):
                return (p,
                        process_page(source, locale, p, format,
                                     'http://%s:%d' % (address, port)))
    if source.has_localizable_file(locale, page):
        return (page, source.read_localizable_file(locale, page))

    return (None, None)
Пример #5
0
Файл: utils.py Проект: jaels/cms
def get_page_params(source, locale, page, format=None, site_url_override=None,
                    localized_string_callback=None):
    from cms.converters import converters

    # Guess page format if omitted, but default to Markdown for friendlier exceptions
    if format is None:
        for format in converters.iterkeys():
            if source.has_page(page, format):
                break
        else:
            format = 'md'

    params = {
        'source': source,
        'template': 'default',
        'locale': locale,
        'page': page,
        'config': source.read_config(),
        'localized_string_callback': localized_string_callback,
    }

    localefile = page
    if params['config'].has_option('locale_overrides', page):
        localefile = params['config'].get('locale_overrides', page)
    params['localedata'] = source.read_locale(params['locale'], localefile)

    if params['config'].has_option('general', 'siteurl'):
        if site_url_override:
            params['site_url'] = site_url_override
        else:
            params['site_url'] = params['config'].get('general', 'siteurl')

    data, filename = source.read_page(page, format)
    metadata, body = extract_page_metadata(data)
    params['pagedata'] = body, filename
    params.update(metadata)

    params['templatedata'] = source.read_template(params['template'])

    defaultlocale = params['config'].get('general', 'defaultlocale')
    params['defaultlocale'] = defaultlocale

    locales = [
        l
        for l in source.list_locales()
        if source.has_locale(l, localefile)
    ]
    if defaultlocale not in locales:
        locales.append(defaultlocale)
    locales.sort()
    params['available_locales'] = locales

    try:
        converter_class = converters[format]
    except KeyError:
        raise Exception('Page %s uses unknown format %s' % (page, format))

    converter = converter_class(body, filename, params)
    converted = converter()
    params['head'], params['body'] = split_head_body(converted)

    if converter.total_translations > 0:
        params['translation_ratio'] = (
            1 - float(converter.missing_translations) / converter.total_translations
        )
    else:
        params['translation_ratio'] = 1

    return params
Пример #6
0
def get_page_params(source, locale, page, format=None, site_url_override=None,
                    localized_string_callback=None):
    # Guess page format if omitted, but default to Markdown for friendlier exceptions
    if format is None:
        for format in converters.iterkeys():
            if source.has_page(page, format):
                break
        else:
            format = 'md'

    params = {
        'source': source,
        'template': 'default',
        'locale': locale,
        'page': page,
        'pagedata': source.read_page(page, format),
        'config': source.read_config(),
        'localized_string_callback': localized_string_callback,
    }

    localefile = page
    if params['config'].has_option('locale_overrides', page):
        localefile = params['config'].get('locale_overrides', page)
    params['localedata'] = source.read_locale(params['locale'], localefile)

    if params['config'].has_option('general', 'siteurl'):
        if site_url_override:
            params['site_url'] = site_url_override
        else:
            params['site_url'] = params['config'].get('general', 'siteurl')

    try:
        converter_class = converters[format]
    except KeyError:
        raise Exception('Page %s uses unknown format %s' % (page, format))

    converter = converter_class(params)

    # Note: The converter might change some parameters so we can only read in
    # template data here.
    params['templatedata'] = source.read_template(params['template'])

    defaultlocale = params['config'].get('general', 'defaultlocale')
    params['defaultlocale'] = defaultlocale

    locales = [
        l
        for l in source.list_locales()
        if source.has_locale(l, localefile)
    ]
    if defaultlocale not in locales:
        locales.append(defaultlocale)
    locales.sort()
    params['available_locales'] = locales

    params['head'], params['body'] = converter()
    if converter.total_translations > 0:
        params['translation_ratio'] = (
            1 - float(converter.missing_translations) / converter.total_translations
        )
    else:
        params['translation_ratio'] = 1

    return params