Beispiel #1
0
 def _update_config(self):
     self.config = deepcopy(self.defaults)
     
     logger.debug('>> Searching for config')
     
     for ext in ('.yml', '.yaml'):
         f = File(normpath(self.src.path, 'config' + ext))
         
         if f.exists:
             logger.debug('..  found: %s', f.path)
             
             try:
                 self.config.update(Config(f.content))
             except ConfigException as e:
                 raise ConfigException(e.message, 'src: {0}'.format(f.path))
             
             self.config['locale'] = self.opts.get('locale', self.config['locale'])
             
             self.config['assets_url'] = absurl(self.config['assets_url'], '')
             self.config['base_url'] = absurl(self.opts.get('base_url', self.config['base_url']), '')
             
             for setting in ('archives_url', 'posts_url', 'tags_url'):
                 self.config[setting] = absurl(self.config[setting])
             
             for setting in ('archives_url', 'assets_url', 'base_url', 'posts_url', 'tags_url'):
                 if re.search(r'(?:^\.{2}/|/\.{2}$|/\.{2}/)', self.config[setting]):
                     raise ConfigException('Invalid config setting.', 'setting: {0}'.format(setting), 'path traversal is not allowed')
             
             for pattern in self.config['include']:
                 if op.commonprefix((self.src.path, normpath(self.src.path, pattern))) != self.src.path:
                     raise ConfigException('Invalid include path.', 'path: {0}'.format(pattern), 'path traversal is not allowed')
             
             break
     else:
         logger.debug('..  no config file found')
Beispiel #2
0
    def _get_url(self, url='', absolute=False):
        parts = [self.globals['site']['base_url'], url]
        domain = self.globals['site']['domain']

        if absolute and domain:
            if not domain.startswith(('http://', 'https://')):
                domain = 'http://' + domain

            parts.insert(0, domain)

        return absurl(*parts)
Beispiel #3
0
 def _get_url(self, url = '', absolute = False):
     parts = [self.globals['site']['base_url'], url]
     domain = self.globals['site']['domain']
     
     if absolute and domain:
         if not domain.startswith(('http://', 'https://')):
             domain = 'http://' + domain
         
         parts.insert(0, domain)
     
     return absurl(*parts)
Beispiel #4
0
 def serve(self):
     self.src = Directory(self.opts['src'])
     base_url = absurl(self.opts['base_url'], '')
     
     if not self.src.exists:
         raise OptionException('Source must exist.')
     
     logger.info('>> Serving at 127.0.0.1:%s', self.opts['port'])
     logger.info('Press ctrl+c to stop.')
     
     cwd = getcwd()
     self.server = Server(('', self.opts['port']), base_url, RequestHandler)
     
     chdir(self.src.path)
     
     try:
         self.server.serve_forever()
     except KeyboardInterrupt:
         self.server.shutdown()
         chdir(cwd)
         
         print('')
Beispiel #5
0
    def serve(self):
        self.src = Directory(self.opts['src'])
        base_url = absurl(self.opts['base_url'], '')

        if not self.src.exists:
            raise OptionException('Source must exist.')

        logger.info('>> Serving at 127.0.0.1:{0}'.format(self.opts['port']))
        logger.info('Press ctrl+c to stop.')

        cwd = getcwd()
        self.server = Server(('', self.opts['port']), base_url, RequestHandler)

        chdir(self.src.path)

        try:
            self.server.serve_forever()
        except KeyboardInterrupt:
            self.server.shutdown()
            chdir(cwd)

            print('')
Beispiel #6
0
 def _get_asset(self, asset):
     return absurl(self.globals['site']['base_url'],
                   self.globals['site']['assets_url'], asset)
Beispiel #7
0
 def _get_asset(self, asset):
     return absurl(self.globals['site']['base_url'], self.globals['site']['assets_url'], asset)
Beispiel #8
0
    def _get_page_url(self, url, text):
        slug = slugify(text)

        return format_url(absurl(url, slug), url.endswith('/'))
Beispiel #9
0
    def _absolutize(self, html):
        def _replace(match):
            return self._get_url(match.group(1).replace(self.globals['site']['base_url'], '', 1), True)

        return sub(r'(?<==")({0}[^"]*)'.format(absurl(self.globals['site']['base_url']), ''), _replace, html)
Beispiel #10
0
 def _get_url(self, url = ''):
     return absurl(self.globals['site']['base_url'], url)
Beispiel #11
0
 def _get_url(self, url = ''):
     return absurl(self.globals['site']['base_url'], url)
Beispiel #12
0
 def _absolutize(self, html):
     def _replace(match):
         return self._get_url(match.group(1).replace(self.globals['site']['base_url'], '', 1), True)
     
     return sub(r'(?<==")({0}[^"]*)'.format(absurl(self.globals['site']['base_url']), ''), _replace, html)
Beispiel #13
0
    def _update_config(self):
        self.config = deepcopy(self.defaults)

        logger.debug('>> Searching for config')

        for ext in ('.yml', '.yaml'):
            f = File(normpath(self.src.path, 'config' + ext))

            if f.exists:
                logger.debug('..  found: %s', f.path)

                try:
                    self.config.update(Config(f.content))
                except ConfigException as e:
                    raise ConfigException(e.message, 'src: {0}'.format(f.path))

                self.config['locale'] = self.opts.get('locale',
                                                      self.config['locale'])

                self.config['assets_url'] = absurl(self.config['assets_url'],
                                                   '')
                self.config['base_url'] = absurl(
                    self.opts.get('base_url', self.config['base_url']), '')

                for setting in ('archives_url', 'posts_url', 'tags_url'):
                    self.config[setting] = absurl(self.config[setting])

                for setting in ('archives_url', 'assets_url', 'base_url',
                                'posts_url', 'tags_url'):
                    if re.search(r'(?:^\.{2}/|/\.{2}$|/\.{2}/)',
                                 self.config[setting]):
                        raise ConfigException('Invalid config setting.',
                                              'setting: {0}'.format(setting),
                                              'path traversal is not allowed')

                for name, config in self.config['containers'].iteritems():
                    try:
                        url = absurl(config['url'])
                    except KeyError:
                        raise ConfigException(
                            'Invalid config setting.',
                            'setting: containers:{0}'.format(name),
                            'url must be set for all containers')

                    if re.search(r'(?:^\.{2}/|/\.{2}$|/\.{2}/)', url):
                        raise ConfigException(
                            'Invalid config setting.',
                            'setting: containers:{0}:url'.format(name),
                            'path traversal is not allowed')

                    config.update(
                        (k, v) for k, v in self.container_defaults.iteritems()
                        if k not in config)
                    config['url'] = url

                for pattern in self.config['include']:
                    if op.commonprefix(
                        (self.src.path, normpath(self.src.path,
                                                 pattern))) != self.src.path:
                        raise ConfigException('Invalid include path.',
                                              'path: {0}'.format(pattern),
                                              'path traversal is not allowed')

                break
        else:
            logger.debug('..  no config file found')
Beispiel #14
0
 def _get_page_url(self, url, text):
     slug = slugify(text)
     
     return format_url(absurl(url, slug), url.endswith('/'))