Beispiel #1
0
    def watch(self):
        self.source = Directory(self.options['source'])
        self.destination = Directory(self.options['destination'])

        if not self.source.exists:
            raise OptionException('Source does not exist')
        elif self.source == self.destination:
            raise OptionException(
                'Source and destination must be different locations')
        elif self.destination.exists and not self.options['force']:
            raise OptionException(
                'Destination already exists',
                'to force generation, use the following flag',
                '  `-f` to EMPTY the destination')

        logger.info('>> Watching')
        logger.info('..  Press ctrl+c to stop')

        handler = EventHandler(self.source.path, self._regenerate)

        self.observer = Observer()
        self.observer.schedule(handler, self.source.path, True)
        self.observer.start()

        try:
            while True:
                sleep(1)
        except KeyboardInterrupt:
            self.observer.stop()

            print('')

        self.observer.join()
Beispiel #2
0
    def init(self):
        self.src = Directory(self._get_theme(self.opts['theme']))
        self.dest = Directory(self.opts['dest'])

        if not self.src.exists:
            raise OptionException('Theme not found.')
        elif self.dest.exists and not self.opts['force']:
            raise OptionException(
                'Destination already exists.',
                'the -f option must be used to force initialization by deleting the destination'
            )

        logger.info('>> Initializing')

        if self.opts['bare']:
            for d in [
                    '_assets/css', '_assets/images', '_assets/js',
                    '_templates', '_posts'
            ]:
                Directory(normpath(self.dest.path, d)).mk()

            File(normpath(self.dest.path, 'config.yml')).mk()
        else:
            self.src.cp(self.dest.path)

        logger.info('Completed in {0:.3f}s'.format(time() - self._start))
Beispiel #3
0
    def generate(self):
        self._render()

        logger.info('>> Generating')

        assets_src = Directory(normpath(self.src.path, '_assets'))
        assets_dest = Directory(
            normpath(self.dest.path, *self.config['assets_url'].split('/')))

        if self.dest.exists:
            if not self.opts['force']:
                raise OptionException(
                    'Destination already exists.',
                    'the -f option must be used to force generation by deleting the destination'
                )

            self.dest.rm()

        self.dest.mk()

        for page in self.pages:
            page.mk()

        if assets_src.exists:
            for asset in assets_src:
                asset.cp(asset.path.replace(assets_src.path, assets_dest.path))

        logger.info('Completed in {0:.3f}s'.format(time() - self._start))
Beispiel #4
0
    def initialize(self):
        Timer.start()

        self.source = Directory(self._get_theme(self.options['theme']))
        self.destination = Directory(self.options['destination'])

        if not self.source.exists:
            raise OptionException('Theme not found')
        elif self.destination.exists and not self.options['delete']:
            raise OptionException(
                'Destination already exists',
                'to force initialization, use the following flag',
                '  `-d` to DELETE the destination')

        logger.info('>> Initializing')

        if self.options['bare']:
            self.destination.rm()

            directories = ('_assets/css', '_assets/images', '_assets/js',
                           '_templates', '_posts')

            for d in directories:
                Directory(normpath(self.destination.path, d)).mk()

            File(normpath(self.destination.path, 'mynt.yml')).mk()
        else:
            self.source.cp(self.destination.path, False)

        logger.info('Completed in %.3fs', Timer.stop())
Beispiel #5
0
    def _generate(self):
        self._initialize()
        self._parse()
        self._render()

        logger.info('>> Generating')

        assets_src = Directory(normpath(self.src.path, '_assets'))
        assets_dest = Directory(
            normpath(self.dest.path, *self.config['assets_url'].split('/')))

        if self.dest.exists:
            if self.opts['force']:
                self.dest.empty()
            else:
                self.dest.rm()
        else:
            self.dest.mk()

        for page in self.pages:
            page.mk()

        assets_src.cp(assets_dest.path)

        for pattern in self.config['include']:
            for path in iglob(normpath(self.src.path, pattern)):
                dest = path.replace(self.src.path, self.dest.path)

                if op.isdir(path):
                    Directory(path).cp(dest, False)
                elif op.isfile(path):
                    File(path).cp(dest)
Beispiel #6
0
    def init(self):
        Timer.start()

        self.src = Directory(self._get_theme(self.opts['theme']))
        self.dest = Directory(self.opts['dest'])

        if not self.src.exists:
            raise OptionException('Theme not found.')
        elif self.dest.exists and not self.opts['force']:
            raise OptionException(
                'Destination already exists.',
                'the -f flag must be passed to force initialization by deleting the destination'
            )

        logger.info('>> Initializing')

        if self.opts['bare']:
            self.dest.rm()

            for d in ('_assets/css', '_assets/images', '_assets/js',
                      '_templates', '_posts'):
                Directory(normpath(self.dest.path, d)).mk()

            File(normpath(self.dest.path, 'config.yml')).mk()
        else:
            self.src.cp(self.dest.path, False)

        logger.info('Completed in %.3fs', Timer.stop())
Beispiel #7
0
    def watch(self):
        self.src = Directory(self.opts['src'])
        self.dest = Directory(self.opts['dest'])

        if not self.src.exists:
            raise OptionException('Source must exist.')
        elif self.src == self.dest:
            raise OptionException('Source and destination must differ.')
        elif self.dest.exists and not self.opts['force']:
            raise OptionException(
                'Destination already exists.',
                'the -f flag must be passed to force watching by emptying the destination every time a change is made'
            )

        logger.info('>> Watching')
        logger.info('Press ctrl+c to stop.')

        self.observer = Observer()

        self.observer.schedule(EventHandler(self.src.path, self._regenerate),
                               self.src.path, True)
        self.observer.start()

        try:
            while True:
                sleep(1)
        except KeyboardInterrupt:
            self.observer.stop()

            print('')

        self.observer.join()
Beispiel #8
0
 def _parse(self):
     logger.info('>> Parsing')
     
     path = Directory(normpath(self.src.path, '_posts'))
     
     logger.debug('..  src: {0}'.format(path))
     
     for f in path:
         post = Post(f)
         
         content = self.parser.parse(self.renderer.from_string(post.bodymatter, post.frontmatter))
         excerpt = re.search(r'\A.*?(<p>.+?</p>)?', content, re.M | re.S).group(1)
         
         data = {
             'content': content,
             'date': post.date.strftime(self.config['date_format']).decode('utf-8'),
             'excerpt': excerpt,
             'tags': [],
             'timestamp': timegm(post.date.utctimetuple()),
             'url': self._get_post_url(post.date, post.slug)
         }
         
         data.update(post.frontmatter)
         data['tags'].sort(key = unicode.lower)
         
         self.posts.append(data)
         
         for tag in data['tags']:
             if tag not in self.tags:
                 self.tags[tag] = []
             
             self.tags[tag].append(data)
Beispiel #9
0
    def _generate(self):
        logger.debug('>> Initializing\n..  src:  %s\n..  dest: %s',
                     self.src.path, self.dest.path)

        self._update_config()

        if self.config['locale']:
            try:
                locale.setlocale(locale.LC_ALL,
                                 (self.config['locale'], 'utf-8'))
            except locale.Error:
                raise ConfigException(
                    'Locale not available.',
                    'run `locale -a` to see available locales')

        self.writer.register({'site': self.config})

        self._render()

        logger.info('>> Generating')

        assets_src = Directory(normpath(self.src.path, '_assets'))
        assets_dest = Directory(
            normpath(self.dest.path, *self.config['assets_url'].split('/')))

        if self.dest.exists:
            if self.opts['force']:
                self.dest.empty()
            else:
                self.dest.rm()
        else:
            self.dest.mk()

        for page in self.pages:
            page.mk()

        assets_src.cp(assets_dest.path)

        for pattern in self.config['include']:
            for path in iglob(normpath(self.src.path, pattern)):
                dest = path.replace(self.src.path, self.dest.path)

                if op.isdir(path):
                    Directory(path).cp(dest, False)
                elif op.isfile(path):
                    File(path).cp(dest)
Beispiel #10
0
    def generate(self):
        self.src = Directory(self.opts['src'])
        self.dest = Directory(self.opts['dest'])

        if not self.src.exists:
            raise OptionException('Source must exist.')
        elif self.src == self.dest:
            raise OptionException('Source and destination must differ.')
        elif self.src.path in ('/', '//') or self.dest.path in ('/', '//'):
            raise OptionException('Root is not a valid source or destination.')
        elif self.dest.exists and not (self.opts['force']
                                       or self.opts['clean']):
            raise OptionException(
                'Destination already exists.',
                'the -c or -f option must be used to force generation')

        self._generate()
Beispiel #11
0
    def __init__(self, name, src, config):
        self._pages = None

        self.name = name
        self.src = src
        self.path = Directory(normpath(self.src.path, '_containers',
                                       self.name))
        self.config = config
        self.data = Data([], OrderedDict(), OrderedDict())
Beispiel #12
0
    def generate(self):
        Timer.start()

        self.src = Directory(self.opts['src'])
        self.dest = Directory(self.opts['dest'])

        if not self.src.exists:
            raise OptionException('Source must exist.')
        elif self.src == self.dest:
            raise OptionException('Source and destination must differ.')
        elif self.dest.exists and not (self.opts['force']
                                       or self.opts['clean']):
            raise OptionException(
                'Destination already exists.',
                'the -c or -f flag must be passed to force generation by deleting or emptying the destination'
            )

        self._generate()

        logger.info('Completed in %.3fs', Timer.stop())
Beispiel #13
0
    def generate(self):
        Timer.start()

        self.source = Directory(self.options['source'])
        self.destination = Directory(self.options['destination'])

        if not self.source.exists:
            raise OptionException('Source must exist')
        elif self.source == self.destination:
            raise OptionException(
                'Source and destination must be different locations')
        elif self.destination.exists:
            if not (self.options['delete'] or self.options['force']):
                raise OptionException(
                    'Destination already exists',
                    'to force generation, use one of the following flags',
                    '  `-d` to DELETE the destination',
                    '  `-f` to EMPTY the destination')

        self._generate()

        logger.info('Completed in %.3fs', Timer.stop())
Beispiel #14
0
    def __init__(self, args=None):
        self._start = time()

        self.opts = self._get_opts(args)

        self.src = Directory(self.opts['src'])
        self.dest = Directory(self.opts['dest'])

        logger.setLevel(getattr(logging, self.opts['level'], logging.INFO))
        logger.debug('>> Initializing\n..  src:  {0}\n..  dest: {1}'.format(
            self.src, self.dest))

        if self.src == self.dest:
            raise OptionException('Source and destination must differ.')
        elif self.src.path in ('/', '//') or self.dest.path in ('/', '//'):
            raise OptionException('Root is not a valid source or destination.')

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

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

            if f.exists:
                logger.debug('..  found: {0}'.format(f.path))

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

                break
        else:
            logger.debug('..  no config file found')

        for opt in ('base_url', ):
            if opt in self.opts:
                self.config[opt] = self.opts[opt]

        self.renderer.register({'site': self.config})
Beispiel #15
0
    def _generate(self):
        logger.debug('>> Initializing\n..  src:  {0}\n..  dest: {1}'.format(
            self.src.path, self.dest.path))

        self._update_config()

        for opt in ('base_url', ):
            if opt in self.opts:
                self.config[opt] = self.opts[opt]

        self.renderer.register({'site': self.config})

        self._render()

        logger.info('>> Generating')

        assets_src = Directory(normpath(self.src.path, '_assets'))
        assets_dest = Directory(
            normpath(self.dest.path, *self.config['assets_url'].split('/')))

        if self.dest.exists:
            if self.opts['force']:
                self.dest.empty()
            else:
                self.dest.rm()
        else:
            self.dest.mk()

        for page in self.pages:
            page.mk()

        if assets_src.exists:
            for asset in assets_src:
                asset.cp(asset.path.replace(assets_src.path, assets_dest.path))

        logger.info('Completed in {0:.3f}s'.format(time() - self._start))
Beispiel #16
0
    def serve(self):
        self.src = Directory(self.opts['src'])
        base_url = Url.join(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 #17
0
    def serve(self):
        self.source = Directory(self.options['source'])

        if not self.source.exists:
            raise OptionException('Source directory does not exist')

        logger.info('>> Serving at 127.0.0.1:%s', self.options['port'])
        logger.info('..  Press ctrl+c to stop')

        address = ('', self.options['port'])
        base_url = URL.join(self.options['base_url'], '')
        cwd = getcwd()

        chdir(self.source.path)

        try:
            self.server = Server(address, base_url, RequestHandler)
            self.server.serve_forever()
        except KeyboardInterrupt:
            self.server.shutdown()
            chdir(cwd)

            print('')
Beispiel #18
0
 def __init__(self, src, site):
     super(Posts, self).__init__('posts', src, self._get_config(site))
     
     self.path = Directory(normpath(src.path, '_posts'))
Beispiel #19
0
 def __init__(self, name, src, config):
     super(Items, self).__init__(name, src, config)
     
     self.path = Directory(normpath(src.path, '_containers', self.name))
Beispiel #20
0
    def __init__(self, src, config):
        super(Posts, self).__init__('posts', src, config)

        self.path = Directory(normpath(self.src.path, '_posts'))

        self._update_config()
Beispiel #21
0
    def __init__(self, name, source, configuration):
        super().__init__(name, source, configuration)

        self.path = Directory(normpath(source.path, '_containers', self.name))
Beispiel #22
0
    def __init__(self, source, site):
        super().__init__('posts', source, self._get_configuration(site))

        self.path = Directory(normpath(source.path, '_posts'))