示例#1
0
def parse_line(line, root=None):
    line = line.strip()
    chapter_regex = re.compile(" *[\*\-\+]? +\[(.*)\]\((.*)\)")
    affix_regex = re.compile(" *\[(.*)\]")

    if chapter_regex.match(line):
        title, path = re.match(" *[\*\-\+]? +\[(.*)\]\((.*)\)", line).groups()
        if line[0] in ['-', '*', '+']:
            return Chapter(title=title, path=path, root=root)
        else:
            logger.debug('Affix %s' % title)
            return Affix(title=title, path=path, root=root)
    elif affix_regex.match(line):
        # TODO: implement Affix
        pass
    else:
        logger.debug('Ignoring line: %s' % line)
    return None
示例#2
0
 def create_chapters(self, chapters=None):
     if not chapters:
         chapters = self.chapters
     logger.debug(chapters)
     for chapter in chapters:
         logger.debug('[%s]' % chapter.path)
         chapter_dir = os.path.join(self.config.root, os.path.dirname(chapter.path))
         if not os.path.exists(chapter_dir):
             logger.info('Creating %s' % chapter_dir)
             os.mkdir(chapter_dir)
         chapter_path = os.path.join(self.config.root, chapter.path)
         if not os.path.exists(chapter_path):
             logger.info('Creating %s' % chapter_path)
             with open(chapter_path, 'w') as f:
                 f.write("# %s\n\n" % chapter.title)
                 f.write("Content goes here.\n")
         if chapter.articles:
             self.create_chapters(chapter.articles)
示例#3
0
    def init(self):
        logger.info('Start Initializing ...')
        self.read_config()
        if not os.path.exists(self.config.root):
            logger.info('Creating Root Directory')
            os.mkdir(self.config.root)

        if not os.path.exists(self.config.build):
            logger.info('Creating Build Directory')
            os.mkdir(self.config.build)

        if not os.path.exists(self.config.summary_abs):
            logger.debug("Creating Default SUMMARY.md")
            with open(self.config.summary_abs, 'w') as f:
                f.write('# Summary\n')
                f.write('\n')
                f.write('- [Introduction](./README.md)\n')
        self.read_summary()
        self.create_chapters()
        logger.info('Initialization Finished')
示例#4
0
    def render(self):
        logger.debug('ODT Renderer Initialized')
        book_title = self.book.config.title or 'book'
        book_name = '%s.odt' % book_title
        book_path = os.path.join(self.destination, book_name)

        logger.debug(self.book.summary[0].content)
        result = self.engine.render(self.template,
                                    book=self.book,
                                    **self.book.config.variables)
        with open(book_path, 'wb') as f:
            f.write(result)
        logger.debug('ODT Renderer Finished')
        return True
示例#5
0
    def read_config(self):
        try:
            config_path = os.path.join(os.path.abspath(self.root), 'book.json')
            if os.path.exists(config_path):
                logger.debug('Reading book.json')
                with open(config_path, 'r') as config_file:
                    book_json = json.loads(config_file.read())
            else:
                logger.debug('Cannot find book.json File')
                return
        except Exception as e:
            # TODO: raise Exception as this should not happen
            logger.debug('Error Reading book.json File')
            raise IOError('Error Reading book.json Config File')

        self.title = book_json.get('title', '').strip()
        self.author = book_json.get('author', '').strip()
        self.description = book_json.get('description', '').strip()
        self.language = book_json.get('language', 'en').strip()
        self.direction = book_json.get('direction', 'ltr').strip()
        self.variables = book_json.get('variables', {})
        self.structure.update(book_json.get('structure', {}))