Exemplo n.º 1
0
    def on_pre_composition(self, director):
        if not self._config.parser.has_section('open_graph'):
            raise AbortError(
                _('An open_graph section is missing in the configuration file.'
                  ))

        if not self._config.parser.has_option('open_graph', 'default_image'):
            raise AbortError(
                _('A default image URL is missing in the configuration file.'))

        self._resolver = director.resolver
        self._url_resolver = URLResolver(
            self._config, self._config.parser.get('open_graph',
                                                  'default_image'))
Exemplo n.º 2
0
    def on_pre_composition(self, director):
        if not self._config.parser.has_section('twitter'):
            raise AbortError(
                _('An twitter section is missing in the configuration file.'))

        if not self._config.parser.has_option('twitter', 'default_image'):
            raise AbortError(
                _('A default image URL is missing in the configuration file.'))

        if not self._config.parser.has_option('twitter', 'site_username'):
            raise AbortError(
                _('A site username is missing in the configuration file.'))

        self._resolver = director.resolver
        self._url_resolver = URLResolver(
            self._config, self._config.parser.get('twitter', 'default_image'))
        self._site = self._config.parser.get('twitter', 'site_username')
Exemplo n.º 3
0
class TwitterExtension(Extension):
    """Inject Twitter metadata into the template context."""

    handle_frontmatter_loaded = True
    handle_pre_composition = True

    def on_pre_composition(self, director):
        if not self._config.parser.has_section('twitter'):
            raise AbortError(_(
                'An twitter section is missing in the configuration file.'))

        if not self._config.parser.has_option('twitter', 'default_image'):
            raise AbortError(_(
                'A default image URL is missing in the configuration file.'))

        if not self._config.parser.has_option('twitter', 'site_username'):
            raise AbortError(_(
                'A site username is missing in the configuration file.'))

        self._resolver = director.resolver
        self._url_resolver = URLResolver(
            self._config,
            self._config.parser.get('twitter', 'default_image'))
        self._site = self._config.parser.get('twitter', 'site_username')

    def on_frontmatter_loaded(self, source_file, frontmatter):
        if frontmatter.get('blog'):
            self.add_metadata(source_file, frontmatter)
        else:
            frontmatter['twitter_metadata'] = ''

    def add_metadata(self, source_file, frontmatter):
        metadata = [
            '<meta name="twitter:card" content="summary" />',
            '<meta name="twitter:site" content="{}" />'.format(self._site),
        ]

        url = self._resolver.as_url(source_file)
        image = self._url_resolver.resolve(url, frontmatter.get('image'))
        metadata.append(
            u'<meta name="twitter:image" content="{}" />'.format(image))

        title = frontmatter.get('title', '')
        title = title.replace('"', '\'')
        metadata.append(
            u'<meta name="twitter:title" content="{}" />'.format(title))

        if frontmatter.get('summary'):
            summary = frontmatter.get('summary', '')
            summary = summary.replace('"', '\'')
            metadata.append(
                u'<meta name="twitter:description" content="{}" />'.format(
                    summary))

        frontmatter['twitter_metadata'] = '\n'.join(metadata)
Exemplo n.º 4
0
class TwitterExtension(Extension):
    """Inject Twitter metadata into the template context."""

    handle_frontmatter_loaded = True
    handle_pre_composition = True

    def on_pre_composition(self, director):
        if not self._config.parser.has_section('twitter'):
            raise AbortError(
                _('An twitter section is missing in the configuration file.'))

        if not self._config.parser.has_option('twitter', 'default_image'):
            raise AbortError(
                _('A default image URL is missing in the configuration file.'))

        if not self._config.parser.has_option('twitter', 'site_username'):
            raise AbortError(
                _('A site username is missing in the configuration file.'))

        self._resolver = director.resolver
        self._url_resolver = URLResolver(
            self._config, self._config.parser.get('twitter', 'default_image'))
        self._site = self._config.parser.get('twitter', 'site_username')

    def on_frontmatter_loaded(self, source_file, frontmatter):
        if frontmatter.get('blog'):
            self.add_metadata(source_file, frontmatter)
        else:
            frontmatter['twitter_metadata'] = ''

    def add_metadata(self, source_file, frontmatter):
        metadata = [
            '<meta name="twitter:card" content="summary" />',
            '<meta name="twitter:site" content="{}" />'.format(self._site),
        ]

        url = self._resolver.as_url(source_file)
        image = self._url_resolver.resolve(url, frontmatter.get('image'))
        metadata.append(
            u'<meta name="twitter:image" content="{}" />'.format(image))

        title = frontmatter.get('title', '')
        title = title.replace('"', '\'')
        metadata.append(
            u'<meta name="twitter:title" content="{}" />'.format(title))

        if frontmatter.get('summary'):
            summary = frontmatter.get('summary', '')
            summary = summary.replace('"', '\'')
            metadata.append(
                u'<meta name="twitter:description" content="{}" />'.format(
                    summary))

        frontmatter['twitter_metadata'] = '\n'.join(metadata)
Exemplo n.º 5
0
    def on_pre_composition(self, director):
        if not self._config.parser.has_section('open_graph'):
            raise AbortError(_(
                'An open_graph section is missing in the configuration file.'))

        if not self._config.parser.has_option('open_graph', 'default_image'):
            raise AbortError(_(
                'A default image URL is missing in the configuration file.'))

        self._resolver = director.resolver
        self._url_resolver = URLResolver(
            self._config,
            self._config.parser.get('open_graph', 'default_image'))
Exemplo n.º 6
0
class OpenGraphExtension(Extension):
    """Inject Open Graph metadata into the template context."""

    handle_frontmatter_loaded = True
    handle_pre_composition = True

    def on_pre_composition(self, director):
        if not self._config.parser.has_section('open_graph'):
            raise AbortError(
                _('An open_graph section is missing in the configuration file.'
                  ))

        if not self._config.parser.has_option('open_graph', 'default_image'):
            raise AbortError(
                _('A default image URL is missing in the configuration file.'))

        self._resolver = director.resolver
        self._url_resolver = URLResolver(
            self._config, self._config.parser.get('open_graph',
                                                  'default_image'))

    def on_frontmatter_loaded(self, source_file, frontmatter):
        if frontmatter.get('blog'):
            self.add_blog_metadata(source_file, frontmatter)
        else:
            frontmatter['open_graph_metadata'] = ''

    def add_blog_metadata(self, source_file, frontmatter):
        url = self._resolver.as_url(source_file)
        metadata = [
            '<meta property="og:type" content="article" />',
            '<meta property="og:url" content="{}" />'.format(url),
        ]

        image = self._url_resolver.resolve(url, frontmatter.get('image'))
        metadata.append(
            u'<meta property="og:image" content="{}" />'.format(image))

        title = frontmatter.get('title', '')
        title = title.replace('"', '\'')
        metadata.append(
            u'<meta property="og:title" content="{}" />'.format(title))

        if frontmatter.get('summary'):
            summary = frontmatter.get('summary', '')
            summary = summary.replace('"', '\'')
            metadata.append(
                u'<meta property="og:description" content="{}" />'.format(
                    summary))

        frontmatter['open_graph_metadata'] = '\n'.join(metadata)
Exemplo n.º 7
0
class OpenGraphExtension(Extension):
    """Inject Open Graph metadata into the template context."""

    handle_frontmatter_loaded = True
    handle_pre_composition = True

    def on_pre_composition(self, director):
        if not self._config.parser.has_section('open_graph'):
            raise AbortError(_(
                'An open_graph section is missing in the configuration file.'))

        if not self._config.parser.has_option('open_graph', 'default_image'):
            raise AbortError(_(
                'A default image URL is missing in the configuration file.'))

        self._resolver = director.resolver
        self._url_resolver = URLResolver(
            self._config,
            self._config.parser.get('open_graph', 'default_image'))

    def on_frontmatter_loaded(self, source_file, frontmatter):
        if frontmatter.get('blog'):
            self.add_blog_metadata(source_file, frontmatter)
        else:
            frontmatter['open_graph_metadata'] = ''

    def add_blog_metadata(self, source_file, frontmatter):
        url = self._resolver.as_url(source_file)
        metadata = [
            '<meta property="og:type" content="article" />',
            '<meta property="og:url" content="{}" />'.format(url),
        ]

        image = self._url_resolver.resolve(url, frontmatter.get('image'))
        metadata.append(
            u'<meta property="og:image" content="{}" />'.format(image))

        title = frontmatter.get('title', '')
        title = title.replace('"', '\'')
        metadata.append(
            u'<meta property="og:title" content="{}" />'.format(title))

        if frontmatter.get('summary'):
            summary = frontmatter.get('summary', '')
            summary = summary.replace('"', '\'')
            metadata.append(
                u'<meta property="og:description" content="{}" />'.format(
                    summary))

        frontmatter['open_graph_metadata'] = '\n'.join(metadata)
Exemplo n.º 8
0
    def on_pre_composition(self, director):
        if not self._config.parser.has_section('twitter'):
            raise AbortError(_(
                'An twitter section is missing in the configuration file.'))

        if not self._config.parser.has_option('twitter', 'default_image'):
            raise AbortError(_(
                'A default image URL is missing in the configuration file.'))

        if not self._config.parser.has_option('twitter', 'site_username'):
            raise AbortError(_(
                'A site username is missing in the configuration file.'))

        self._resolver = director.resolver
        self._url_resolver = URLResolver(
            self._config,
            self._config.parser.get('twitter', 'default_image'))
        self._site = self._config.parser.get('twitter', 'site_username')
Exemplo n.º 9
0
 def test_use_default(self):
     config = self.factory.make_configuration()
     resolver = URLResolver(config, 'https://www.example.com/default.png')
     url = resolver.resolve('', '')
     self.assertEqual('https://www.example.com/default.png', url)
Exemplo n.º 10
0
 def test_use_absolute_image(self):
     config = self.factory.make_configuration()
     base_url = 'https://www.example.com/route/default.png'
     resolver = URLResolver(config, '')
     url = resolver.resolve(base_url, '/images/javascript.png')
     self.assertEqual('http://www.example.com/images/javascript.png', url)
Exemplo n.º 11
0
 def test_use_relative_image(self):
     config = self.factory.make_configuration()
     base_url = 'https://www.example.com/route/default.png'
     resolver = URLResolver(config, '')
     url = resolver.resolve(base_url, 'python.png')
     self.assertEqual('https://www.example.com/route/python.png', url)
Exemplo n.º 12
0
 def test_use_default(self):
     config = self.factory.make_configuration()
     resolver = URLResolver(config, 'https://www.example.com/default.png')
     url = resolver.resolve('', '')
     self.assertEqual('https://www.example.com/default.png', url)
Exemplo n.º 13
0
 def test_use_absolute_image(self):
     config = self.factory.make_configuration()
     base_url = 'https://www.example.com/route/default.png'
     resolver = URLResolver(config, '')
     url = resolver.resolve(base_url, '/images/javascript.png')
     self.assertEqual('http://www.example.com/images/javascript.png', url)
Exemplo n.º 14
0
 def test_use_relative_image(self):
     config = self.factory.make_configuration()
     base_url = 'https://www.example.com/route/default.png'
     resolver = URLResolver(config, '')
     url = resolver.resolve(base_url, 'python.png')
     self.assertEqual('https://www.example.com/route/python.png', url)